Wednesday, December 29, 2010

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.

  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
Share:

2 comments:

  1. when i wrote this same code it doesnt work properly when i put break point and after debug the error occured in smtpclient.send(mailmessage) please forward correct answer to my problem and more questuion is when we send sms from our gmail or yahoo account is there any need of firewalls na please reply me to [email protected].

    I hope u sent reply for this thank u very much...

    ReplyDelete
  2. Hello Srinu

    What error you are getting. To check your accessibility of gmail you need to ping smtp.gmail.com.

    Please send me error then I can help you in better way.

    Regards,
    Jalpesh

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews