I Thought He Came With You is Robert Ellison’s blog about software, marketing, politics, photography, time lapse and the occasional well deserved rant. Follow along with a monthly email, RSS or on Facebook. About 7,250,102,795 people have not visited yet so it might be your first time here. Suggested reading: Got It, or roll the dice.

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

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.