This brief article demonstrates the simplest way and step by step instructions on how to send email from aspx page using a gmail account. After reading this you'll be able to send HTML formatted email messages by using Asp.Net and C#.

In order to send an email from gmail account programmatically, you have to include the following namespace:

using System.Net.Mail; // contains classes for SMTP settings, email sending etc

Let's take a look at the soure of aspx page. It has only three fields and a button to send email

<form id="form1" runat="server">
    <asp:Label ID="lblEmailAddress" runat="server" Text="Email Address:"></asp:Label>
    <asp:TextBox ID="txtEmailAddress" runat="server" Width="300px"></asp:TextBox>
    <br />
    <asp:Label ID="lblEmailSubject" runat="server" Text="Subject of Email:"></asp:Label>
    <asp:TextBox ID="txtEmailSubject" runat="server" Width="300px"></asp:TextBox>
    <br />
    <asp:Label ID="lblEmailMessageBody" runat="server" Text="Email Message Body:"></asp:Label>
    <asp:TextBox ID="txtEmailMessageBody" runat="server" Rows="5" TextMode="MultiLine"
        Width="300px"></asp:TextBox>
    <br />
    <asp:Button ID="btnSendEmail" runat="server" OnClick="btnSendEmail_Click" Text="Send Email" />
    <asp:Label ID="lblEmailStatus" runat="server"></asp:Label>
</form>

Email Address: the address to which the email is to be sent
Subject of Email: The subject line of email and
Email Message Body: As we want to send HTML instead of simple plain text so whatever is placed in this field, will be formatted using html styles.

Pretty simple ... ok let's move to server side code:

protected void btnSendEmail_Click(object sender, EventArgs e)
{
    lblEmailStatus.Text = "";
    if (SendEmails())
        lblEmailStatus.Text = "Your Email has been sent";
    else
        lblEmailStatus.Text = "Error: Sending Email";
}

This is the event handler of Send Email button. It simply calls SendEmails() function and sets the status of label whether the email is sent successfully or not. Now lets look at SendEmail function:


private bool SendEmails()
{
    try
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtEmailAddress.Text);
        mail.From = new MailAddress("[email protected]");
        mail.Subject = "Sending Email from aspx: " + this.txtEmailSubject.Text;
        string Body = "<div style='font-family:Comic Sans MS; color:Navy; font-size:small'>" +
        "<br/>" +
        "<b>Email Date and Time:</b> " + DateTime.Now.ToString() + "" +
        "<br/>" +
        "<br/>" +
        "See the Following Email Message:" +
        "<br/>" +
        "<br/>" +
        "<b>" + this.txtEmailMessageBody.Text + "</b>" +
        "<br/>" +
        "<hr/>" +
        "</div>";
        mail.Body = Body;
        mail.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;
        smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "email password");
        smtp.EnableSsl = true;
        smtp.Send(mail);

        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

First we will build the actual email message i.e. MailMessage, then configure the SmtpClient and send the email using that SmtpClient. One important thing: Gmail is using 587 of the host smtp.gmail.com

Rest the code is pretty simple and neat ... let me know if you have any questions regarding Sending email.

Another same tutorial, How to Send Email Using Gmail in ASP.NET