We use System.Net.Mail namespace to send email. Without going into details, lets have a simple working code to send emails through asp.net via two most popular smtp mail servers.
Gmail:
try
{
MailMessage mail = new MailMessage(); //using System.Net.Mail namespace
mail.To.Add("xyz@yahoo.com"); //Enter reciever's email address
mail.From = new MailAddress("abc@gmail.com"); //Enter sender's email address
mail.Subject = "Testing mail...";
mail.Body = @"Lets-code ! Lets-code to make it simpler";
mail.IsBodyHtml = true; //Body of mail supports html tags
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "pwd");
// Your gmail username and password
smtp.EnableSsl = true; //Gmail uses a encrypted connection
smtp.Send(mail);
Response.Write("Mail Sent Successfully");
}
catch(Exception ex)
{
Response.Write(ex.Message);
}