Configure Asp.net website to send E-mails using SMTP server/Gmail account

Configure Asp.net website to send E-mails using SMTP server/Gmail account

Hi 🙂
we all are developing websites for ourself, college projects and clients and every website requires the ability to send e-mails on certain events like when a user wishes to contact you for more details on your work or is interested in your services.

It was bit dissicult to send e-mails in Classic ASP and asp.net 1.1 but since the release of Asp.net 2.0 framework sending e-mails is very easy and faster.

Requirements for sending E-mail’s

  • A website (yes, the one you are developing or modifying)
  • A SMTP Server to send your mails out.
  • Username, password to authenticate yourself to SMTP server


public static void SendMail(string MailTo, string MailFrom, string Subject, String MailBody)
{
System.Net.Mail.MailMessage objMailMessage = new System.Net.Mail.MailMessage(
MailFrom, MailTo);
objMailMessage.Subject = Subject;
objMailMessage.IsBodyHtml = true;
objMailMessage.ReplyTo =new MailAddress("username@domain.com");
objMailMessage.Body = MailBody;
SmtpClient objsmtp = new SmtpClient();
objsmtp.EnableSsl = true; // this statement is required in case you are planning to use Gmail account/Google apps account
objsmtp.Send(objMailMessage);
}

Next You need to use following function to send email to user or yourself. You just need to paas proper arguments and it will take care of rest.

After that place following code in your website’s web.config file.

<system.net>
<mailSettings>
<smtp from=”username@gmail.com”>
<network host=”smtp.gmail.com” port=”587″ password=”email-password” userName=”username@gmail.com” />
</smtp>
</mailSettings>
</system.net>

Default port for SMTP server is 25.
For Gmail/Google Apps accounts it is 587.
If above port numbers does not work for you, please ask you e-mail provider about the port number in use.

send-email1

I hope you find this post useful.
Thank you for reading.