Hossein Zahed

Web Developer, Entrepreneur, Software Educator

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 = "[email protected]";   //Email Address of Sender
        to = "[email protected]";   //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", "[email protected]");
        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: 

Thank you Pep...

Pep Guardiola has announced to the president and squad that he will not be staying at FC Barcelona after June 30, when his present contract ends.

Guardiola is convinced that “I am leaving with the sensation of having done a good job and I’m proud of what I’ve done here. I have not forgotten anything that I was taught by my predecessors. And I am leaving at peace with myself”. 

TailspinSpyworks v0.9

Project Description

Tailspin Spyworks demonstrates how extraordinarily simple it is to create powerful, scalable applications for the .NET platform. It shows off how to use the great new features in ASP.NET 4 to build an online store, including shopping, checkout, and administration.

Overview

  • Demonstrates the new features in ASP.NET 4
  • Data Access via Linq to Entities
  • MasterPage and CSS based design
  • Modern ASP.NET Controls Use
  • Integrated AJAX Control Toolkit Editor
  • This tutorial requires Visual Studio 2010 and ASP.NET 4.0

PDF Help File: download 

Project File: download