Quick Search

Send mail from ASP.NET using your gmail account

February 27th, 2010 by admin
Leave a reply »

 

This article demonstrate that how to send a mail in aspl.net using your gmail account. You can also use other mail account as follow these instructions.

However, one thing the function can’t do is SSL authentication. Lets add that:

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”;

If you have SSL authentication pass last parameter true. Which gmail for example uses so look at this example:

SendMail(“smtp.gmail.com”,
465,
“account@gmail.com”,
“”,
“Your name”,
“account@gmail.com”,
“Stefan Receiver”,
“receive@whatever.com”,
“Test”,
“Hello there Steff!”,
true);

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

Posted in Email

You can follow any responses to this entry through the RSS 2.0 Feed. You can leave a response , or trackback from your own site.

11 commentsAdd your comment
  1. You are a very capable person!

  2. Jason says:

    Thanks for sharing the details. I found the information really helpful.

  3. Pebbles says:

    I’m really proud of you

  4. Thank you , I wish that the world could be a better place.

  5. Really good blogging site, keep myself through checking it, I\\\’m actually serious to learn more about it.

  6. You can tally me in for a Digg. Thanks for posting this on your site!

  7. I just required to say that I found your website via Goolge and I am glad I did. Carry on the very good work and I will be sure to bookmark you for when I have far more free of charge time away from the books. Thanks!

  8. Appreciate the advice! I?ll give it a try.

  9. Eila Krikwen says:

    I would like to begin by stating, thank you for supplying me with the information I’ve been looking for. I’ve been surfing the net for three hours looking for it and wish I would have located your site sooner. Not only did I find what I was searching for, but found answers to questions I never thought to ask. Thank you for such a wonderful web-site!

  10. Luke Avner says:

    Wherever could it be, i want to read much more about this post, thanks.

 

Trackbacks /
Pingbacks

  1. Yahoo News

Leave a Reply

SEO Company India

search engine marketing company India that offers affordable seo services