Tuesday, August 12, 2008

How to send mail through asp.net using smtp server?

Here is code for sending mail through SMTP server

SmtpClient objSmtpClient = new SmtpClient(ConfigurationManager.AppSettings["SmtpServer"].ToString());

// Here, Pass your SMTP Server Name, Default is 'localhost'
objSmtpClient.Host = "YourSmtpServer";
objSmtpClient.Port = 25;

MailMessage objMailMessage = new MailMessage();
MailAddress objMailAddress = new MailAddress(From);
objMailMessage.From = objMailAddress;
objMailMessage.To.Add(To);
objMailMessage.Subject = Subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.Body = "Test";
objMailMessage.Priority = MailPriority.High;
objSmtpClient.Credentials = new
System.Net.NetworkCredential("SmtpServerUserName","SmtpServerPassword");
objSmtpClient.Send(objMailMessage);