HOME

Thursday, February 23, 2012

Send mail to gmail from C# and Asp.Net

---------------Code in .CS ---------------

using System.Net.Mail;
using System.Configuration;


-----------------------------------------

MailMessage mail = new MailMessage();
mail.To.Add("xxx@gmail.com"t);  // To Mail ID
mail.From = new MailAddress("yyy@gmail.com");  // From Mail ID
mail.Subject = "Welcome To NL SOFTTECH SERVICES!!";
string Body = "body content";
mail.Body = Body;
mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient();
smtp.Host = ConfigurationManager.AppSettings["SMTP"];
smtp.Credentials = new      System.Net.NetworkCredential(ConfigurationManager.AppSettings["FROMEMAIL"], ConfigurationManager.AppSettings["FROMPWD"]);
smtp.EnableSsl = true;
smtp.Send(mail);




--------------webconfig file-------------------------

<appSettings>
    <add key="SMTP" value="smtp.gmail.com"/>
    <add key="FROMEMAIL" value="yyy@gmail.com"/>
    <add key="FROMPWD" value="Password of yyy@gmail.com"/>
  </appSettings>

No comments:

Post a Comment