Send emails using gmail in asp.net C# with sample code

We recently publish article "How to send email using asp.net C#". In our last article we have describe how to use System.Web.Mail namespace to send email using asp.net C# with your own SMTP Server. If you don't have your own hosted email server then you can use free email server Gmail, yahoo or other. Gmail is most popular mail service used over worldwide. You can use google SMTP Server credentials to send email from your asp.net web application. You must provide your gmail username and password while declaring SMTP server setting.
We are doing same thing what we have did in our last article. We will use System.Web.Mail namespace and reference that in our application.We will create MailMessage object for setting mail properties.When we are using gmail credentials in addition we will configure our smtp client settings. In which we will config SMTP Host, SMTP Port, gmail username and password SMTP credentials etc.

What to Configure to send emails using Gmail in asp.net C#

Declare smtp configuration.
SMTP HOST - smtp.gmail.com
SMTP PORT - 587
SMTP Credentials - gmail username, gmail password
EnableSsl property - set true if you want secure connection
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Send Mail using Asp.net - Dotnetspan.com</title>
</head>
<body>
    <asp:Label ID="lblmessage" runat="server" Visible="false"></asp:Label>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td colspan="2"><h2>Send Mail using Asp.net - Dotnetspan.com</h2></td>
        </tr>
        <tr>
            <td>From:</td>
        <td>
            <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td>To</td>  
        <td> <asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Subject</td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td>Message</td>
        <td>
            <asp:TextBox ID="txtBody" TextMode="MultiLine" Rows="10" Columns="20" runat="server"></asp:TextBox>
        </td>
        </tr>
        <tr>
            <td></td>
        <td><asp:Button ID="btnSend" Text="Send" runat="server" OnClick="btnSend_Click" /></td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
 protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(txtTo.Text.ToString());
            mailMessage.From = new MailAddress(txtFrom.Text.Trim());
            mailMessage.Body = txtBody.Text.Trim();
            System.Net.Mail.SmtpClient smptClient = new System.Net.Mail.SmtpClient();
            smptClient.Host = "smtp.gmail.com";
            smptClient.Port=587;
            smptClient.Credentials = new System.Net.NetworkCredential("your gmail username", "your gmail password");
            smptClient.EnableSsl = true;
            smptClient.Send(mailMessage);

            lblmessage.Visible = true;
            lblmessage.Text = "Email Sent Successfully";
            lblmessage.ForeColor = System.Drawing.Color.Green;
        }
        catch
        {
            lblmessage.Visible = true;
            lblmessage.Text = "Error Occur while processing your request.";
            lblmessage.ForeColor = System.Drawing.Color.Red;
        }
    }
Note : when we are using third party SMTP Host, it will take time to connect and send email depend on internet speed.
Download Source Code

Search Terms: Send email using email in asp.net, using gmail send email in asp.net, send email using gmail SMTP credentials, Send email using gmail credentials.