Send emails using gmail in C# with sample code

We recently publish article "How to send email using C#". In our last article we have describe how to use System.Web.Mail namespace to send email using 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 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 C#

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

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <title>Send Mail using -</title>
    <asp:Label ID="lblmessage" runat="server" Visible="false"></asp:Label>
    <form id="form1" runat="server">
            <td colspan="2"><h2>Send Mail using -</h2></td>
            <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
        <td> <asp:TextBox ID="txtTo" runat="server"></asp:TextBox></td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
            <asp:TextBox ID="txtBody" TextMode="MultiLine" Rows="10" Columns="20" runat="server"></asp:TextBox>
        <td><asp:Button ID="btnSend" Text="Send" runat="server" OnClick="btnSend_Click" /></td>
 protected void btnSend_Click(object sender, EventArgs e)
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(txtFrom.Text.Trim());
            mailMessage.Body = txtBody.Text.Trim();
            System.Net.Mail.SmtpClient smptClient = new System.Net.Mail.SmtpClient();
            smptClient.Host = "";
            smptClient.Credentials = new System.Net.NetworkCredential("your gmail username", "your gmail password");
            smptClient.EnableSsl = true;

            lblmessage.Visible = true;
            lblmessage.Text = "Email Sent Successfully";
            lblmessage.ForeColor = System.Drawing.Color.Green;
            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, using gmail send email in, send email using gmail SMTP credentials, Send email using gmail credentials.


Popular posts from this blog

12 opensource and free MVC gridview contols

Scheduling background jobs using Hangfire in ASP.NET Core

Difference ExecuteNonQuery vs ExecuteScalar vs ExecuteReader in