ASP.net: Sending Application Mail through Google Apps (GMail)

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");
            }

Published by

Tim Clark

Experienced Business Owner, Chief Information Officer, Vice President, Chief Software Architect, Application Architect, Project Manager, Software Developer, Senior Web Developer, Graphic Designer & 3D Modeler, University Instructor, University Program Chair, Academic Director. Specialties: Ruby, Ruby on Rails, JavaScript, JQuery, AJAX, Node.js, React.js, Angular.js, MySQL, PostgreSQL, MongoDB, SQL Server, Responsive Design, HTML5, XHTML, CSS3, C#, ASP.net, Project Management, System Design/Architecture, Web Design, Web Development, Adobe CS6 (Photoshop, Illustrator)

3 thoughts on “ASP.net: Sending Application Mail through Google Apps (GMail)

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 )

Facebook photo

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

Connecting to %s