Sending HTML Mail Using Gmail SMTP (using System.Net.Mail namespace)
INTRODUCTION: Sending emails requires SMTP server and hence usually you won’t be able to send emails from your local machine [In case you have SMTP server configured on your local machine, you can surely send emails using that!].
The other way out would be to use a third parties SMTP server to send Emails.
In this Article I am going to use Gmail’s SMTP to send Emails.
Using System.Net.Mail; //Include This NameSpace
MailMessage MyMailMessage = new MailMessage();
MyMailMessage.From = new MailAddress("cadbuaryguy@gmail.com");
MyMailMessage.To.Add("learninggeeks@yahoo.com");
MyMailMessage.Subject = "Feedback Form";
MyMailMessage.IsBodyHtml = true;
MyMailMessage.Body = "<table><tr><td>" + txtName.Text + txtEmail.Text + txtComments.Text + "</table></tr></td>";
SmtpClient SMTPServer = new SmtpClient("smtp.gmail.com");
SMTPServer.Port = 587;
SMTPServer.Credentials = new System.Net.NetworkCredential("cadbuaryguy@gmail.com", System.Configuration.ConfigurationSettings.AppSettings["pwd"].ToString());
SMTPServer.EnableSsl = true;
try
{
SMTPServer.Send(MyMailMessage);
Response.Redirect("Thankyou.aspx");
}
catch (Exception ex)
{
}