Send Email using Gmail

This code sample lets you send email using Gmail account. The steps involved are explained.


public static void SendMail(string Host, int Port, string strUserName, string strPassword, string strFromName, string strFromEmail,
string strToName, string strToEmail, string strHeader, string strMessage, bool bAuthenticated)
{
if (strToName.Length == 0)
strToName = strToEmail;
if (strFromName.Length == 0)
strFromName = strFromEmail;
System.Net.Mail.MailMessage Mail = new System.Net.Mail.MailMessage();
Mail.
//System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = Host;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = Port.ToString();
if (bAuthenticated)
{
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = strUserName;
Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = strPassword;
}
Mail.To = strToEmail;
Mail.From = strFromEmail;
Mail.Subject = strHeader;
Mail.Body = strMessage;
Mail.BodyFormat = System.Web.Mail.MailFormat.Html;

System.Web.Mail.SmtpMail.SmtpServer = Host;
System.Web.Mail.SmtpMail.Send(Mail);
}



And call the Function as

SendMail("smtp.gmail.com",
465,
"danasegarane@gmail.com",
"YourGmail Password",
"Dana",
"danasegarane@gmail.com",
"Danasegarane",
"danasegarane@gmail.com",
"Hi Dana ",
"Hello How are u !",
true);


Comments

Author: Pradeep Iyer03 Nov 2008 Member Level: Gold   Points : 0

Hi,

Thanks for that !!

Regards
Pradeep

Author: D.Jeya kumar(JK)15 May 2009 Member Level: Gold   Points : 1

Hi,


This SMTP server is not working... Please test it once before posting...

Regards
JK

Author: Danasegarane.A15 May 2009 Member Level: Gold   Points : 1

I tested it previously... It was working..

Let me check it once again


Dana

Author: Muralidharan Rangaraju17 May 2009 Member Level: Gold   Points : 1

Hi Danasegarane,

For gmail SMTP server we can also use port number 587 instead of 465. In some networks 465 works and in some network 587 will work. For JK this may be a problem.

Regards,
Murali

Author: Sasikumar19 May 2009 Member Level: Gold   Points : 1

Hai Danasegarane.A,

This SMTP server is not working,
Please Check and test before posting the Resource

Regards
Sasikumar

Author: Danasegarane.A19 May 2009 Member Level: Gold   Points : 1

Hi Sasikumar,
I have never posted resource without properly checking. It was working well for me...

Check the Murli's comment



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: