Tuesday, December 1, 2009

Send Mail using Gmail SMTP account

Using SMTP and Gmail in ASP.NET 2.0...

public static void SendMail(string sHost, int nPort, string sUserName, string sPassword, string sFromName,string sFromEmail,

string sToName, string sToEmail, string sHeader, string sMessage,
bool fSSL)

{

if (sToName.Length == 0)

sToName = sToEmail;

if (sFromName.Length == 0)

sFromName = sFromEmail;

System.Web.Mail.MailMessage Mail = new
System.Web.Mail.MailMessage();

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"]
= sHost;


Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"]
= 2;


Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"]
= nPort.ToString();


if (fSSL)

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"]
= "true";


if (sUserName.Length == 0)

{

//Ingen auth

}

else

{

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"]
= sUserName;

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"]
= sPassword;

}

Mail.To = sToEmail;

Mail.From = sFromEmail;

Mail.Subject = sHeader;

Mail.Body = sMessage;

Mail.BodyFormat = System.Web.Mail.MailFormat.Html;

System.Web.Mail.SmtpMail.SmtpServer = sHost;

System.Web.Mail.SmtpMail.Send(Mail);

}

Now, the secret is the last parameter, bool fSSL. If true then we set the magic CDO field cdo/configuration/smtpusessl to true:

if ( fSSL )

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

And that my folks is all we need. Specify true as last parameter and you will have
SSL authentication. Which gmail for example uses so look at this example:

SendMail("smtp.gmail.com",

465,//or use 587
"account@gmail.com",

"<accountpassword>",

"Your name",

"account@gmail.com",

"Receiver NAME",

"receive@whatever.com",

"Subject Message",

"Body Message",

true);

So secret to get mail working against gmail is: port 465 - server smtp.gmail.com
and ssl = true.

__________________________________________________

Example 2:

Google (Gmail) is hosting our emails through "domain sharing". Basically, any emails being
sent to
yourMail@gmail.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 .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):

SMTP("yuorMail@gmail.com", "toMail@domain.com", "", "sub message",
"body 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:

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("YourMail@gmail.com", "password");

//client.UseDefaultCredentials = true;
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 ex)

{

//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.

_____________________________________________

Example 3:

public void SendMail()

{

//Create Mail Message Object with content that you want to send with mail.

System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("youtMail@gmail.com", "toMail@domain.com", "This is the mail subject", "Just wanted to say Hello");

MyMailMessage.IsBodyHtml = false;

//Proper Authentication Details need to be passed when sending email from gmail

System.Net.NetworkCredential mailAuthentication = new

System.Net.NetworkCredential("YourMail@gmail.com", "password");

//Smtp Mail server of Gmail is "smpt.gmail.com" and it uses port no. 587

//For different server like yahoo this details changes and you can get it from respective server.

System.Net.Mail.SmtpClient mailClient = new
System.Net.Mail.SmtpClient("smtp.gmail.com", 587);

//mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;

//Enable SSL

mailClient.EnableSsl = true;

mailClient.UseDefaultCredentials = false ;

mailClient.Credentials = mailAuthentication;

mailClient.Send(MyMailMessage);
}

No comments:

Post a Comment