Tuesday, August 16, 2011

How to send email from Gmail in asp.net?

If you don't have an smtp server for sending email then Gmail can be a cool alternative for you. Sending email from smtp.gmail.com using system.net.mail requires a secured connection. So you have to set SmtpClient's EnableSsl property to true. In this example, we are not writing anything in web.config file. Instead we are writing those configuration information on code behind.

Code for sending email from gmail:


using System;
using System.Net.Mail;
using System.Text;
 
public partial class Mailer : System.Web.UI.Page
{
 
    private MailMessage BuildMail()
    {
        string from, to, bcc, cc, subject, body;
        from = "uniquesaiful@gmail.com";   //Email Address of Sender
        to = "tips.asp.net@gmail.com";   //Email Address of Receiver
        bcc = "";
        cc = "";
        subject = "This is a test email. I am just checking whether email client is working properly.";
 
        StringBuilder sb = new StringBuilder();
        sb.Append("Hi Scott,<br/>");
        sb.Append("This is a test email. We are testing out email client. Please don't mind.<br/>");
        sb.Append("We are sorry for this unexpected mail to your mail box.<br/>");
        sb.Append("<br/>");
        sb.Append("Thanking you<br/>");
        sb.Append("Tips.Asp.Net");
        
        body =sb.ToString() ;
 
        MailMessage mail = new MailMessage();
        mail.From = new MailAddress(from);
        mail.To.Add(new MailAddress(to));
 
        if (!string.IsNullOrEmpty(bcc))
        {
            mail.Bcc.Add(new MailAddress(bcc));
        }
        if (!string.IsNullOrEmpty(cc))
        {
            mail.CC.Add(new MailAddress(cc));
        }
 
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = true;
 
        return mail;
    }
 
    private void SendEMail(MailMessage mail)
    {
        SmtpClient client = new SmtpClient();
        client.Host = "smtp.gmail.com";
        client.Port = 587;
        client.EnableSsl = true;
        client.Credentials = new System.Net.NetworkCredential("uniquesaiful""ami@janina.com");
        try
        {
            client.Send(mail);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        MailMessage mail = BuildMail();
        SendEMail(mail);
    }
}
 
 Following picture is shows the sent email in my inbox:
 
 
 
 

29 comments:

  1. nice article. if you are maintaining any template for email.. this may help you.
    http://aspnettutorialonline.blogspot.com/2012/05/email-template-from-flat-file-or.html

    ReplyDelete
  2. Nice! I was looking for this code. Thanks, guys!

    ReplyDelete
  3. yep.. this works.. thanks

    ReplyDelete
  4. Thanks a lot for all ur effort in putting together such vast information on ASP.NET.

    It really helped me revise the entire asp.net in few hrs.
    usb drive recovery

    ReplyDelete
  5. Thanks a lot. I just tested now and it worked well.

    ReplyDelete
  6. Very cool.. Thanks I really needed this solution.

    ReplyDelete
  7. very nice.. Thanks!
    Have a look at this link http://www.etechpulse.com/2012/09/how-to-send-email-in-aspnet-using-c.html

    ReplyDelete
  8. Hi,this is sharan plz send me your contact no

    ReplyDelete