posts - 52,  comments - 113,  trackbacks - 0
  Saturday, January 13, 2007

Ok, Google (Gmail) is hosting our emails through "domain sharing". Basically, any emails being sent to raylinder@glacsy.com goes to Gmail and Gmail hosts it. My concern was SMTP from our website in ASP.NET 2.0. Yes, sound overwhelmingly exciting doesn't it? Well, there's is a way, the LONG and easier way or the short and painful way. LONG and easier way is, you build a SMTP class/control and namespace it to something like Glacsy.Net.WebMail then create a the SMTP assembly to accept parameters like From, To, BCC, Subject, Body. Then every page you want to send email, just map it to the class. Long time to build it, easy to use. I'm not getting into the short and painful way, you can just use your imagination. Below is some code on your page (yes, finally I'm including code):

Glacsy.Net.WebMail.SMTP("welcome@glacsy.com", someone@their-email.com, "bcc@some-one-else.com", "My Subject", "My body or message...");

This is what you would use to pass a page to email. Below is the SMTP.cs code in the App_Code directory to use:

using System.Net; using System.Net.Mail; using System.Text; namespace Glacsy.Net { public class WebMail { public static void SMTP(string from, string to, string bcc, string subject, string body) { MailMessage mailMsg = new MailMessage(); mailMsg.From = new MailAddress(from); mailMsg.To.Add(to); mailMsg.Subject = subject; mailMsg.IsBodyHtml = true; mailMsg.BodyEncoding = Encoding.UTF8; mailMsg.Body = body; mailMsg.Priority = MailPriority.Normal; // Smtp configuration SmtpClient client = new SmtpClient(); client.Credentials = new NetworkCredential("youremail@gmail.com", "yourpassword"); client.Port = 587; //or use 465 client.Host = "smtp.gmail.com"; client.EnableSsl = true; object userState = mailMsg; try { //you can also call client.Send(msg) client.SendAsync(mailMsg, userState); } catch (SmtpException) { //Catch errors... } } } }

Now, this is the easiest and simplest way I could think for sending email via Gmail. If you have a better way, let me know and I'll test it out. The good thing is the speed of development this setup will provide in the long run. I may build and release a .dll file of this in the future for everyone. But for now, enjoy...