May 19, 2012

Send SMTP mail using C# & .NET

Sending mail via web application or service, is now a days are very common. Most of user demands for email notifications or similar feature in their applications. There numerous of scripts are available to send mail. If you are using C# and .NET framework, and you want a code behind script to customise or create a dynamic mail using C# classes, you can use the following lines of code to generate and send SMTP mail.
using System.Net.Mail;

MailMessage mail = new MailMessage();
mail.From = new MailAddress("");
mail.Subject = "Test Email";
string Body = "
Hello Navin,
Your online transaction id and password are as:
User Name:
Password: user-name-password
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "";
smtp.Credentials = new System.Net.NetworkCredential("", "");
smtp.EnableSsl = true;
//smtp.Host = ConfigurationManager.AppSettings["SMTP"];