send email using gmail credentials in asp.net

we often require email sending functionality in our web application.
If we don’t have smtp server for sending mails then we can send mails by using gmail smtp server.

Here I will explain how to send email using gmail credentials in asp.net.

protected void btnSubmit_Click(object sender, EventArgs e)
{
  try
  {
    MailMessage Msg = new MailMessage();
    Msg.From = new MailAddress("fromemailaddress");
    Msg.To.Add("toemailaddress");
    Msg.Subject = "Define your subject line";
    Msg.Body = "Content of body";

    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials=new System.Net.NetworkCredential("gmailID","gmailpassword");
    smtp.EnableSsl = true;
    smtp.Send(Msg);
  }
  catch (Exception ex)
  {
    //catch exception here
  }
}

Note:You need to add System.Net.Mail namespace on your page.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>