Thursday, September 15, 2011

Sending Email in asp.net, C#

You can write this code at the click event of a button in asp.net to automatically send an email whenever the button is clicked :- 

{
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = "10.9.210.53"; // IP address of an SMTP server
smtpClient.Port = 25; //Port No of the SMTP server

MailMessage objMail = new MailMessage("sender email id", "receiver's email id ");
objMail.IsBodyHtml = true;
objMail.Subject = " Set here the subject of mail";
objMail.Body = "Hi!! How are you ";
objMail.Priority = MailPriority.High; // Set if waant to send mail with high priority
NetworkCredential nc = new NetworkCredential("username of network admin", "password");
nc.Domain = "domain name";
smtpClient.Credentials = nc;
smtpClient.Send(objMail);
Response.Write("Email has been sent successfully.");
}