Tuesday, July 19, 2011

Send Email In Asp.net Using Gmail

We can use Gmail smtp server to send email, using Asp.Net. Before we start developing Asp.net code, make sure POP is enabled in your Gmail's mail setting.






Once you done with this step SAVE your Gmail settings....

To do Email sending in Asp.net we need "System.Net.Mail" namespace.

public static Boolean SendEmail()
{
  MailMessage mail = new MailMessage();

mail.From = new MailAddress("YOUR FROM EMAIL ADDRESS");
mail.To.Add(new MailAddress("YOUR TO EMAIL ADDRESS"));            
mail.Bcc.Add(new MailAddress("YOUR BCC EMAIL"));
            
  mail.Subject = "Email using Gmail";

  string Body = "Put your Email Body TEXT here"; 
// Here you can put HTML string if you want email to be sent in HTML format.
  mail.Body = Body;

  mail.IsBodyHtml = true;
  SmtpClient smtp = new SmtpClient();
  smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
  smtp.Credentials = new System.Net.NetworkCredential
       ("Username@gmail.com","GmailPassword");
//Or your Smtp Email ID and Password
  smtp.EnableSsl = true;
  smtp.Send(mail);
}

This is simple function to send email in Asp.net using Gmail account.
You can also find Copy of this email in Your SENT items. Submit this story to DotNetKicks

Read more...