Monday, February 21, 2011

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


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.


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("from@gmail.com")
                                       ,new MailAddress("to@yahoo.com"));
            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("yourgmailaddress@gmail.com", "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");
 
        }
    }
}

No comments :

Post a Comment