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.


Salvador Cazorla
Sorry but, for me, this code not working. Server return an authentication error
Shika Helmy
// Gmail Address from where you send the mail var fromAddress = "[email protected]"; // any address where the email will be sending var toAddress = YourEmail.Text.ToString(); //Password of your gmail address const string fromPassword = "pass"; // Passing the values and make a email formate to display string subject = YourSubject.Text.ToString(); string body = "From: " + YourName.Text + "\n"; body += "Email: " + YourEmail.Text + "\n"; body += "Subject: " + YourSubject.Text + "\n"; body += "Question: \n" + Comments.Text + "\n"; // smtp settings var smtp = new System.Net.Mail.SmtpClient(); { smtp.Host = "smtp.gmail.com"; smtp.Port = 587; smtp.EnableSsl = true; smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; smtp.Credentials = new NetworkCredential(fromAddress, fromPassword); smtp.Timeout = 20000; } // Passing values to smtp object smtp.Send(fromAddress, toAddress, subject, body);

Add Comment

All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.