How to send email with vs2005?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I can use outlook2003 to send email,but I cann't use this code below to send
email.
Please help me to test this code and instruct me how to solve this problem
in detail.



software environment: VS2005 + XP.-- I have disabled firewall
hardware enviornmnet:telcom's modem connects hub,hub connects two
computers.。-- I also tried to connect computer to modem directly,but I can
surf internet, not send email with code.

=============================
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 465;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("uid", "pwd");
//I also try to use full email address to replace uid. It didn't work.
client.DeliveryMethod = SmtpDeliveryMethod.Network;

System.Net.Mail.MailMessage message = new
MailMessage("(e-mail address removed)", "(e-mail address removed)", "Subject",

"Body");
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHtml = true;

try
{
client.Send(message);
Response.Write("Email successfully sent.");
}
catch (Exception ex)
{
Response.Write("Send Email Failed." + ex.ToString()); ;
}
===============
 
Is your logon name "uid" and your password "pwd"? If not, change these values
to your logon id and password. That should get things running.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
It happend below!

"Send Email Failed.System.Net.Mail.SmtpException: The operation has timed
out. at System.Net.Mail.SmtpClient.Send(MailMessage message) at
MainPage.Button1_Click(Object sender, EventArgs e) in c:\Programming in
Class\ThreePages\MainPage.aspx.cs:line 94 "
 
"Send Email Failed.System.Net.Mail.SmtpException: The operation has timed
out. at System.Net.Mail.SmtpClient.Send(MailMessage message) at
MainPage.Button1_Click(Object sender, EventArgs e) in c:\Programming in
Class\ThreePages\MainPage.aspx.cs:line 94 "

Indulge me - what does line 94 contain...? I'm assuming it's:

client.Send(message);
 
Thank you very much.

Your code is really helpfull. It worked with port 587, not 465 on my pc. One
more thing, could you tell me what the problem is with my code?
 
If I use gmail as smtp server, what the different between port 587 and port
465 is?

I just check gmail online help, it recommend that we use port 465. Why? Waht
is the reason you used port 587?

Thanks in advance!
 
I have no idea.

Experimentation showed that for 1.1 .. one of those ports worked.
and for 2.0, only the other one worked.
????

Huh? I'm with you, but I have no idea.
 
Not really, unless I was sitting next to you (or at least on your network).

I've found smtp email sending is experimentation sometimes.

Thus why I built the "smarter email configuration" model.

Because I'd get different behavior

at work
at home
using dialup somewhere.

Sometimes its an art, not a science.
 
Thank you very much and have a nice day!



sloan said:
I have no idea.

Experimentation showed that for 1.1 .. one of those ports worked.
and for 2.0, only the other one worked.
????

Huh? I'm with you, but I have no idea.
 
re:
!> Experimentation showed that for 1.1 .. one of those ports worked.
!> and for 2.0, only the other one worked.

Check for code differences.

The port used depends on your ISP's ( Gmail, in this case ) server's settings,
and not on any code you write. The port is determined by your ISP, not by your code.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
Juan


I agree with you.. except for the gmail smtp server.

1.1 framework likes port 465
2.0 Framework, my tests show that gmail likes port 587

I can't explain only, I can only say that was how it played out.
 
587 is the assigned SMTP port for applications which support TLS.
465 is the assigned SMTP port for applications which support SSL.

The Transport Layer Security (TLS) 1.0 protocol is disabled in the .NET Framework 1.1.
By default, only the Secure Sockets Layer (SSL) 3.0 protocol is enabled in .Net 1.1.

That means that, if you use the .Net Framework 1.1 to send mail, you *must* use port 465.

By default, TLS 1.0 and SSL 3.0 are *both* enabled in the Microsoft .NET Framework 2.0.

That means that, if you use the .Net Framework 2.0 to send mail,
you can use port 465 *or* you can use port 587, depending on the security protocol you enable.

The port you need to use *really* depends on which security layer your application uses.

re:
I can't explain only, I can only say that was how it played out.

The above is the technical explanation. :-)



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
Aha,

Thanks for the explanation.




Juan T. Llibre said:
587 is the assigned SMTP port for applications which support TLS.
465 is the assigned SMTP port for applications which support SSL.

The Transport Layer Security (TLS) 1.0 protocol is disabled in the .NET Framework 1.1.
By default, only the Secure Sockets Layer (SSL) 3.0 protocol is enabled in ..Net 1.1.

That means that, if you use the .Net Framework 1.1 to send mail, you *must* use port 465.

By default, TLS 1.0 and SSL 3.0 are *both* enabled in the Microsoft .NET Framework 2.0.

That means that, if you use the .Net Framework 2.0 to send mail,
you can use port 465 *or* you can use port 587, depending on the security protocol you enable.

The port you need to use *really* depends on which security layer your application uses.

re:

The above is the technical explanation. :-)



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
Back
Top