Sending email through GMail using SmtpClient

Ok so I just signed up for Google Apps primarily because I wanted to get off of GoDaddy’s email offering.  I chose Google Apps Standard because 1) it is free and 2) because Google is awesome.  Oh btw Microsoft I did try to get setup on Office Live Small Business, however, you required me to host my web site there in order to have email.  I use Azure for hosting so it sure would be nice if you could give us Small Business Azure users a hook up on Office Live Small Business for hosting email without a web site.  Anyway  I’m not made, I’m cool with Google.

So down to the code that works to send email from Google.

MailMessage message = new MailMessage();
message.From = new MailAddress(you@yourDomain.xxx);
message.To.Add(“yourFriend@someDomain.com”);
message.Subject = “TEST Message”;
message.IsBodyHtml = false;  // Could be true to.
message.Body = “TEST”;

SmtpClient smtp = new SmtpClient(“smtp.gmail.com”);
smtp.Credentials = new NetworkCredential(“you@yourDomain.xxx“, “your password”);
smtp.EnableSsl = true;
smtp.Port = 587;

smtp.Send(message);

Potential Issues

Error Message
“System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at “

Resolution
This could be a few different things.  First make sure that your user name and password are correct.  The second major thing and what I got burned on was setting the property “UseDefaultCredentials”.  I explicitly set this property to false after I set the Credentials property.  Well that is bad because, the credentials get set to null at that time.  So make sure at the time you call Send that the credentials are set.

Error Message
“The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Must issue a STARTTLS command first.”

Resolution
One reason why you might get this message is that you have not enabled SSL, so to resolve set EnableSsl = true.

Explore posts in the same categories: .NET

Tags:

You can comment below, or link to this permanent URL from your own site.

4 Comments on “Sending email through GMail using SmtpClient”


  1. I have not words to discribe how much happy I am to found your blog

  2. ayanda Says:

    hello i used the above code but it not working out it gives me an error says “FAILURE SENDING MAIL” please help desparete student

    • Tom Krueger Says:

      Hmm. Not sure, it could be a variety of things. That is not a very specific error message it gave. If it is a new email account, make sure to log into it once first manually and send an email from it. If you don’t get it, I could try to help out. As a first step, What email provider are you sending through? Gmail, Yahoo, etc? If you are using Gmail or Yahoo, feel free to post your code without credentials and I can try it.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: