I ran into a little gotcha when setting up a new web application to send email through the Google Apps SMTP servers. When you read the Google support documentation on setting up a client to send email through their SMTP server you will see there are 2 options SSL, TSL with a different port for each.
When writing the C# code to send the email there is an option to on the .Net SmtpClient object called ‘EnableSsl’. You of course need to set this to true but the gotcha comes in when you setup the SMTP server and port in your web.config file. You would assume since the option ‘EnableSsl’ refers to ‘SSL’ you would need to use port 465 for SSL as outlined by Google in their documentation. But. you would be wrong to assume that and on every call to send an email within your application you will run into a ‘Time Out’.
I don’t know everything about .Net mail but apparently ‘EnableSsl’ doesn’t really mean SSL it means TSL. I say this because as soon as I changed the SMTP server’s port in my web.config from the SSL port of 465 to the TSL port of 587 as outlined by Google my mail sent without issue.
Hopefully this will save someone a lot of time.
Web.config Setup:
<system.net><mailSettings><smtp from="noreply@yourdomain.com"><network host="smtp.gmail.com" password="******" userName="noreply@yourdomain.com" port="587" /></smtp></mailSettings></system.net>C# Send Mail Code:
try
{MailMessage message = new MailMessage();
message.From = new MailAddress(tbxEmail.Text);
message.To.Add(new MailAddress(_supportEmail));
message.Subject = tbxSubject.Text;message.IsBodyHtml = false;
message.Priority = MailPriority.High;message.Body = tbxMessage.Text;SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);return true;}catch (Exception ex)
{ExceptionHandler handler = new ExceptionHandler();
handler.LogException(ex,SeverityLevel.Critical,"Contact Form Message could not be sent to support email",
GetType(),"SendMessageEmail");
}
Great post. I got some very useful information from it. I've been trying out Joomla lately and am having a great time experimenting with it. Have you used Joomla? Any tips for me? If you'd like to see my blog it's here. Thanks again for this blog – it is really well-done.
LikeLike
Its really a help to people with problems sending emails from google apps like me.
LikeLike
asa
LikeLike