Thursday, May 28, 2009

Simplest way to send email using GMail in ASP.Net (C#)

///
/// This is the primery method try from the program
/// Latest & easiest method of sending an Email using ASP.NET
///

///
///
///
///
public static void sendMail(string to, string from, string subject, string body)
{
///Smtp config
SmtpClient client = new SmtpClient("smtp.gmail.com", 465);
// Edit password and username
client.Credentials = new NetworkCredential("kalit.sikka@gmail.com", "password");
client.EnableSsl = true;

///mail details
MailMessage msg = new MailMessage();


try
{

msg.From = new MailAddress(from);
msg.To.Add(to);
// msg.SubjectEncoding = System.Text.Encoding.UTF8;
msg.Subject = subject;
//msg.CC.Add();
msg.IsBodyHtml = true;
msg.BodyEncoding = System.Text.Encoding.UTF8;
msg.Body = body;
msg.Priority = MailPriority.Normal;

// Enable one of the following method.

client.Send(msg);

// or use the following alternative after enabling send mail asynchronous option in the global.asax

//object userState = msg;
//client.SendAsync(msg, userState);



}
catch (Exception exp)
{
///This runs the backup plan
SendMailAlt(to, from, subject, body);
}

}

1 comment:

 
Locations of visitors to this page