Pages

Wednesday, March 18, 2009

send mail using gmail in asp.net

If we don’t have SMTP server for sending mails at that time we can send mails by using gmail smtp server in asp.net. I will show to how to implement mail sending concept using Gmail credentials in asp.net. To implement this concept first you need to enable POP enable in your Gmail account for sending emails.


// **
CALL FUNCTION FOR SEND MAIL
**//
SendMail("smtp.gmail.com",
465,
"testemail@gmail.com",
"test[wd",
"Rajesh Prajapati",
"testemail@gmail.com",
"Rajesh",
"testemail@yahoo.co.in",
"Test",
"Hello!",
true);

//**
FUNCTION FOR SENDING MAIL USING GMAIL ACCOUNT

**//
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);
}

No comments:

ShareThis

Welcome

Welcome to Rajesh Prajapati, asp.net blog.
Here you can find some useful code and information about asp.net., c#, VB.net, SQL Server, Web Service, Web Designing etc