Stephen Hill's Bloggie Writing about my interests; Programming and Formula One.

Send Emails with .NET and Gmail

One year ago

I'm currently working on a new website called, Ford Motorsport, and I wanted the website to send emails to users when they sign up and do certain actions through the site, as one would expect from a community website.

I was originally using System.Web.Mail to send out emails but I recently found out that it was replaced in .Net 2.0 with System.Net.Mail and we're now on version 3.5. Yikes! I've chosen to use Gmail's SMPT service for now while the site is growing which will give me time to find a more suitable SMPT provider. Gmail's SMPT service requires an SSL connection over port 587 with authentication, which is much easier todo using the new namespace and classes.

So here's a snippet of code which will send a Transformers themed email using Gmail's SMPT service. You will need to import the System.Net and System.Net.Mail namespaces.

// You need to instantiate a new MailMessage class MailMessage oMail = new MailMessage(); // Set the To email address oMail.To.Add(new MailAddress("oprime@autobots.com", "Optimus Prime")); // Set the From email address oMail.From = new MailAddress("switwicky@gmail.com", "Sam Witwicky"); // Set the Subject oMail.Subject = "Test Email Through Gmail SMTP"; // You want to allow HTML content and then specify it oMail.IsBodyHtml = true; oMail.Body = "<p>Hi Prime,</p><p>Just sending you a test email from ASP.NET via Gmail.</p><p>Cheers Spark</p>"; // This is where you specify the SMTP server and we need to enable SSL on Port 587 SmtpClient Smtp = new SmtpClient("smtp.gmail.com", 587); Smtp.EnableSsl = true; // You need to specify your gmail or googlemail email address and password Smtp.Credentials = new NetworkCredential("switwicky@gmail.com", "LadiesMan217"); // Finally, the following method sends the email Smtp.Send(oMail);

I hope the above code snippet it useful :)

Photograph of the vehicles in Transformers

Previous Comments

  1. Name Michael D. Hall Wednesday 25 February 2009 5:29 PM

    SMTP

    I did this exact thing yesterday for my new site, http://www.nuscia.com (first drop published yesterday but ASP.NET MVC not configured on hosting provider yet so it don't work.). If you'd like to send the email as a Fire'n'Forget you can use use "smtp.SendAsync(message, new object());" but if you want to send it asynchronously from ASP.NET you can need to wrap it in a ThreadQueue like...

    ThreadQueue.QueueUserWorkItem(x=>{ ... all your logic here ... });

    Kicked for Transformers themed email.

  2. Name Quinn Tuesday 10 March 2009 3:07 PM

    Thanks!

  3. Name Sudipta Mukherjee Sunday 29 March 2009 3:53 AM

    Here is how you can send emails through gmail smtp.

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/92a6da04-afd5-4cb1-bb64-4be23a673a63/

  4. Name kannadasan Monday 13 April 2009 10:35 AM

    hai...kannadasaan

  5. Name kannadasan Monday 13 April 2009 10:35 AM

    hai...kannadasaan

  6. Name anirudha Sunday 08 November 2009 4:34 AM

    I Developed a web application in asp.net. in my application we send email using our application easily without login to site means to say easily compose email by my application. in my apps we send by a account on google , yahoo, hotmail , live. but my apps is not done because i enable a feature for aol user then aol user use my application for compose email

Leave a Comment