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.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;
“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 “
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.
“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.”
One reason why you might get this message is that you have not enabled SSL, so to resolve set EnableSsl = true.
Tags: .NETYou can comment below, or link to this permanent URL from your own site.