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 :)

Previous Comments
-
Michael D. Hall Wednesday 25 February 2009 5:29 PM
-
Quinn Tuesday 10 March 2009 3:07 PM
-
Sudipta Mukherjee Sunday 29 March 2009 3:53 AM
-
kannadasan Monday 13 April 2009 10:35 AM
-
kannadasan Monday 13 April 2009 10:35 AM
-
anirudha Sunday 08 November 2009 4:34 AM