Sending mail with Gmail Account using System.Net.Mail in ASP.NET

Posted by Jalpesh P. Vadgama on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jalpesh P. Vadgama
Published on Wed, 29 Dec 2010 05:01:42 GMT Indexed on 2010/12/29 5:54 UTC
Read the original article Hit count: 1312

Filed under:
|
|
|

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.

  1. Gmail will have fixed number of quota for sending emails per day. So you can not send more then that emails for the day.
  2. Your from email address always will be your account email address which you are using for sending email.
  3. You can not send an email to unlimited numbers of people. Gmail ant spamming policy will restrict this.
  4. 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.

using System.Net.Mail;

namespace Experiement
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender,System.EventArgs e)
        {
            MailMessage mailMessage = new MailMessage(new MailAddress("[email protected]")
                                       ,new MailAddress("[email protected]"));
            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
            System.Net.NetworkCredential("[email protected]", "yourpassword");

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = networkCredentials;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.Send(mailMessage);

            Response.Write("Mail Successfully sent");

        }
    }
}

That’s run this application and you will get like below in your account.

SendMail

Technorati Tags: ,,
Shout it

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about c#.net