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.

Advertisement
Explore posts in the same categories: .NET

2 Comments on “Sending email through GMail using SmtpClient”


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


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 )

Connecting to %s


Follow

Get every new post delivered to your Inbox.