I Thought He Came With You is Robert Ellison’s blog about software, marketing, politics, photography and time lapse.

Sending email via GMail in C#/.NET using SmtpClient

Updated on Wednesday, February 22, 2017

Gmail Logo

I’ve stubbed my toe on this a couple of times, so here is the magic incantation:

using (SmtpClient smtp = new SmtpClient())
{
    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    smtp.UseDefaultCredentials = false;
    smtp.EnableSsl = true;
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587; 
    smtp.Credentials = new NetworkCredential("[email protected]""password");
    // send the email
}

Update 2015-02-11: A comment below from Shika Helmy suggests that adding a timeout might be helpful. Also note that if you have enabled two factor authentication for your Google Account you'll need to generate an app password in order to use basic authentication to the Gmail SMTP server.