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, August 15, 2018

Gmail Logo

Here’s a quick code snippet that configures SmtpClient to send email using Gmail’s SMTP server:

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
}

This can be frustratingly difficult to get right so I’ll run through each setting quickly.

DeliveryMethod: Set to SmtpDeliveryMethod.Network, the alternatives are copying the email to a directory for pickup by a different application. We want to connect directly to Gmail.

UseDefaultCredentials: False, as we’ll be providing our own credentials later. Setting properties in the right order is apparently important for SmtpClient, although the Microsoft documentation doesn’t mention this. Make sure you set UseDefaultCredentials to false before setting the Credentials property.

EnableSsl: True. SSL or TLS is required.

Host: smtp.gmail.com for this example. Note that this server restricts you to sending 2,000 emails per day. There is a less restrictive option for G Suite customers (smtp-relay.gmail.com) and a more restrictive server that can only send messages to Gmail or G Suite addresses (aspmx.l.google.com).

Port: Google says to use 465 for SSL and 587 for TLS. I’ve found that 587 with EnableSSL set to true works fine.

Credentials: Your Gmail address and password in a NetworkCredential. If your account has 2 step (multi factor) authentication then this won’t work. You can generate an app password easily and use this instead of your regular password. It’s also possible to use OAuth.

In the comments below Shika Helmy suggests also setting the Timeout property to 20000.

You can now use smtp.Send() to send the email.

Because it’s likely to be your personal account and there is a 2,000 message cap on sending I’d only recommend using this for small scale projects. You don’t want to get your Gmail account blocked. For higher volume I’d look at using SendGrid or similar.

One last note - while I’ve used SmtpClient for all sorts of trivial email needs over the years the latest Microsoft documentation marks it obsolete and warns:

SmtpClient and its network of types are poorly designed, we strongly recommend you use https://github.com/jstedfast/MailKit and https://github.com/jstedfast/MimeKit instead

Hope this expanded article helps. I still come back here periodically to cut and paste rather than mess up the property order again...

Comments

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.