I wrote the following piece of code for sending an email. First, I wanted to try it with gmail.
Using this is pretty simple:Code:public class EmailSender { public static void SendEmail( string from, string to, string smtpaddress, int port, string username, string password, string title, string body) { try { SmtpClient client = new SmtpClient(smtpaddress, port); client.Credentials = new System.Net.NetworkCredential(username, password); client.EnableSsl = true; client.UseDefaultCredentials = false; MailMessage mail = new MailMessage(); mail.To.Add(to); mail.From = new MailAddress(from); mail.Subject = title; mail.Body = body; mail.IsBodyHtml = true; client.Send(mail); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
This doesn't work. I get a SmtpException, "The operation has timed out."Code:class Program { public static void Main(string[] args) { EmailSender.SendEmail( "[email protected]", "[email protected]", "smtp.gmail.com", 465, "john.doe", "password", "test", "<p>This is a test email!</p>"); } }
I also tried it like this:
Same problem. If I change the port to 587 (http://mail.google.com/support/bin/a...n&answer=13287), I get the following exception:Code:class Program { public static void Main(string[] args) { EmailSender.SendEmail( "<[email protected]>", "<[email protected]>", "smtp.gmail.com", 465, "john.doe", "password", "test", "<p>This is a test email!</p>"); } }
Anyone has suggestions what could be wrong?The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Thank you.




Marius Bancila
Reply With Quote
