Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to send email with vs2005?

5 views
Skip to first unread message

Chef

unread,
Mar 15, 2007, 12:43:00 PM3/15/07
to
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("u...@hotmail.com", "u...@hotmail.com", "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()); ;
}
===============

Cowboy (Gregory A. Beamer) - MVP

unread,
Mar 15, 2007, 12:49:07 PM3/15/07
to
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!
***************************

Mark Rae

unread,
Mar 15, 2007, 12:51:40 PM3/15/07
to
"Chef" <Ch...@discussions.microsoft.com> wrote in message
news:7A8A6D47-6E48-4767...@microsoft.com...

> but I cann't use this code below to send email.

What happens when you try...?


Chef

unread,
Mar 15, 2007, 1:05:05 PM3/15/07
to
I used my real user name and password to replace "uid" and "pwd" in the code.

Chef

unread,
Mar 15, 2007, 1:08:16 PM3/15/07
to
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 "

Mark Rae

unread,
Mar 15, 2007, 1:52:57 PM3/15/07
to
"Chef" <Ch...@discussions.microsoft.com> wrote in message
news:FFFFBF05-2723-4296...@microsoft.com...

> "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);


Chef

unread,
Mar 15, 2007, 2:03:16 PM3/15/07
to
client.Send(message); //line 94

sloan

unread,
Mar 15, 2007, 2:26:25 PM3/15/07
to

I have downloadable examples here:

2/8/2006
Smarter Email/Smtp setup with DotNet Configuration Sections (1.1 and 2.0)
http://sholliday.spaces.live.com/blog/


"Chef" <Ch...@discussions.microsoft.com> wrote in message

news:7A8A6D47-6E48-4767...@microsoft.com...
> 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

Chef

unread,
Mar 15, 2007, 3:01:16 PM3/15/07
to
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?

Chef

unread,
Mar 15, 2007, 3:13:08 PM3/15/07
to
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!

sloan

unread,
Mar 15, 2007, 3:26:49 PM3/15/07
to

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.


"Chef" <Ch...@discussions.microsoft.com> wrote in message

news:21B6B10F-FC02-42D8...@microsoft.com...

sloan

unread,
Mar 15, 2007, 3:28:28 PM3/15/07
to

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.


"Chef" <Ch...@discussions.microsoft.com> wrote in message

news:31199C3A-DA3C-4BF7...@microsoft.com...

Chef

unread,
Mar 15, 2007, 3:35:16 PM3/15/07
to
Thank you very much and have a nice day!

Juan T. Llibre

unread,
Mar 15, 2007, 3:39:53 PM3/15/07
to
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/
===================================
"sloan" <sl...@ipass.net> wrote in message news:uTSFifzZ...@TK2MSFTNGP03.phx.gbl...

sloan

unread,
Mar 15, 2007, 3:49:19 PM3/15/07
to
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.

"Juan T. Llibre" <nomail...@nowhere.com> wrote in message
news:%23l7jvmz...@TK2MSFTNGP04.phx.gbl...

Juan T. Llibre

unread,
Mar 15, 2007, 4:20:49 PM3/15/07
to
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/
===================================

"sloan" <sl...@ipass.net> wrote in message news:%23dX3Gsz...@TK2MSFTNGP03.phx.gbl...

sloan

unread,
Mar 15, 2007, 5:06:06 PM3/15/07
to
Aha,

Thanks for the explanation.


"Juan T. Llibre" <nomail...@nowhere.com> wrote in message

news:%23jYQn9z...@TK2MSFTNGP04.phx.gbl...

0 new messages