Sending mail with Gmail Account using System.Net.Mail in ASP.NET
Any web application is in complete without mail functionality you should have to write send mail functionality. Like if there is shopping cart application for example then when a order created on the shopping cart you need to send an email to administrator of website for Order notification and for customer you need to send an email of receipt of order. So any web application is not complete without sending email. This post is also all about sending email. In post I will explain that how we can send emails from our Gmail Account without purchasing any smtp server etc.
There are some limitations for sending email from Gmail Account. Please note following things.
- Gmail will have fixed number of quota for sending emails per day. So you can not send more then that emails for the day.
- Your from email address always will be your account email address which you are using for sending email.
- You can not send an email to unlimited numbers of people. Gmail ant spamming policy will restrict this.
- Gmail provide both Popup and SMTP settings both should be active in your account where you testing. You can enable that via clicking on setting link in gmail account and go to Forwarding and POP/Imap.
So if you are using mail functionality for limited emails then Gmail is Best option. But if you are sending thousand of email daily then it will not be Good Idea.
Here is the code for sending mail from Gmail Account.
public partial class WebForm1 : System.Web.UI.Page
protected void Page_Load(object sender,System.EventArgs e)
MailMessage mailMessage = new MailMessage(new MailAddress("firstname.lastname@example.org")
mailMessage.Subject = "Sending mail through gmail account";
mailMessage.IsBodyHtml = true;
mailMessage.Body = "<B>Sending mail thorugh gmail from asp.net</B>";
System.Net.NetworkCredential networkCredentials = new
SmtpClient smtpClient = new SmtpClient();
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = networkCredentials;
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
Response.Write("Mail Successfully sent");
That’s run this application and you will get like below in your account.