Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to send an email through code. Everything seems to be right, but its timing out. Here is my code:

C#
MailAddress fromAddress = new MailAddress("testmail@interwebs.com");
            MailAddress toAddress = new MailAddress("domlennonza@gmail.com");
            string subject = "Test Subject";
            string body = "Test Message";

            SmtpClient client = new SmtpClient
            {
                Host = "mail.mymailserver.co.za",
                UseDefaultCredentials = false,
                Credentials = new NetworkCredential("username", "password")
            };

            using (MailMessage mm = new MailMessage(fromAddress, toAddress))
            {
                mm.Subject = subject;
                mm.Body = body;
                client.Send(mm);
            }
Posted
Comments
Kenneth Haugland 29-Jul-12 9:05am    
I think you forgot to specify the port number to use...
DominicZA 29-Jul-12 9:08am    
If I specify the port, I specify 23, and then it fails to find the server.

Standard SMTP port is 25, not 23. In case of gmail you have to use nonstandard setup plus TLS/SSL secured tcp channel: http://support.google.com/mail/bin/answer.py?hl=en&answer=13287[^]

Here is an SSL example: http://stackoverflow.com/questions/1011245/how-can-i-send-emails-through-ssl-smtp-with-the-net-framework[^]
 
Share this answer
 
v2
Comments
Mehdi Gholam 29-Jul-12 9:13am    
Just posted the same, 5'ed
pasztorpisti 29-Jul-12 9:18am    
Thank you!
DominicZA 29-Jul-12 9:34am    
I checked with my service provider and they said to use port 587, now I am getting an error saying "Smtp server returned an invalid response"? Any tips?
pasztorpisti 29-Jul-12 9:52am    
Have you tried the example? Its also with gmail...
DominicZA 29-Jul-12 9:55am    
I am not using gmail!
SMTP send port is 25 not 23, email hosting servers might use 587 or 2525 also check with your email host for this.
 
Share this answer
 
Comments
pasztorpisti 29-Jul-12 9:19am    
This time I given just a 4 because you forgot to mention TLS/SSL.
DominicZA 29-Jul-12 9:34am    
I checked with my service provider and they said to use port 587, now I am getting an error saying "Smtp server returned an invalid response"? Any tips?
Mehdi Gholam 29-Jul-12 9:44am    
Set EnableSsl = true also.
DominicZA 29-Jul-12 9:46am    
No, I dont have SSL enabled!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900