Sean Shydow
Sean Shydow

Reputation: 69

Getting an exception from sending an email from gmail

I am trying to email people using gmail. I am not certain why this is not working correctly. Below is the screenshot of the error thrown.

Exception when emailing

Below here is the code for setting up my mail client.

string body = File.ReadAllText(@"C:\Data\MailTemplates\welcome.html");
        MailMessage message = new MailMessage();
        SmtpClient client = new SmtpClient("smtp.gmail.com");

        client.Port = 587;
        client.Credentials = new System.Net.NetworkCredential("sean.shydow", "password");
        client.EnableSsl = true;
        message.From = new MailAddress("[email protected]");
        message.IsBodyHtml = true;
        ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
            { return true; };
        message.To.Add("[email protected]");
        message.Subject = subject.Text;
        message.SubjectEncoding = System.Text.Encoding.Unicode;
        message.Body = body;
        message.BodyEncoding = System.Text.Encoding.Unicode;

Upvotes: 0

Views: 152

Answers (1)

Adam Wright
Adam Wright

Reputation: 49386

Port 587 is for TLS. As you're using SSL, try port 465 (see http://mail.google.com/support/bin/answer.py?answer=13287 for more).

Also confirm that your machine can actually connect to the Gmail SMTP ports. Some ISPs block them to avoid botted machines becoming spam factories. Try telnet smtp.google.com PORT for PORTs 25, 465, and 587,. If you get a timeout, then you're likely firewalled from connecting to these ports outside a certain IP range.

Upvotes: 2

Related Questions