Send emails in using Gmail | How to set Smtp setting in web.config file to send emails in using Gmail

Introduction: In previous articles i explained How to send mail with multiple attachments in with C#,Vb.Net and  How to send emails in using Gmail in  and How to send emails in | How to set Smtp setting in web.config file to send email in
Now in this article i am going to explain how to send emails using gmail account and setting the SMTP setting in web.config file. Sometimes it is required to send email from our website to other person. It’s very easy. Just follow the article.

Implementation: Let's create an application to understand.
  • In the design page (.aspx) design the page as:
<table style="width:100%;">
            Send To(Email Address)</td>
            <asp:TextBox ID="txtEmailId" runat="server" Columns="60"></asp:TextBox>
            <asp:TextBox ID="txtSubject" runat="server" Columns="60"></asp:TextBox>
            <asp:TextBox ID="txtBody" runat="server" Columns="60" Rows="5"
            <asp:Button ID="btnSendEmail" runat="server" onclick="btnSendEmail_Click"
                Text="Send Email" />
  • In the web.config file write as:
      <smtp deliveryMethod="Network" >
        <network defaultCredentials="false"

    <add key="EmailID" value=" YourGmailEmail"/>

C#.NET  Code to send mail using gmail in
  • Include following namespaces:
using System.Configuration;
using System.Net.Mail;

Now in the code behind file(.aspx.cs) write the code to send Email on send mail button as:

  protected void btnSendEmail_Click(object sender, EventArgs e)
        string fromEmail = ConfigurationManager.AppSettings["EmailID"].ToString();
        string toEmail = txtEmailId.Text.Trim();
        MailMessage message = new MailMessage(fromEmail, toEmail);
        message.Subject = txtSubject.Text.Trim();
        message.Body = txtBody.Text.Trim();
        SmtpClient smtp = new SmtpClient();       

VB.NET Code to send mail using gmail in
  • Import the following namespaces:
imports System.Configuration
imports System.Net.Mail
  •  Now in the code behind file(.aspx.vb) write the code to send Email on send mail button as:
Protected Sub btnSendEmail_Click(sender As Object, e As EventArgs)
       Dim fromEmail As String = ConfigurationManager.AppSettings("EmailID").ToString()
       Dim toEmail As String = txtEmailId.Text.Trim()
       Dim message As New MailMessage(fromEmail, toEmail)
       message.Subject = txtSubject.Text.Trim()
       message.Body = txtBody.Text.Trim()
       Dim smtp As New SmtpClient()
End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Next Post »


Click here for comments
October 29, 2013 ×

Unrecognized attribute 'enableSsl'. error in web config file.....please help ???

October 30, 2013 ×

Hello Rohit, are you exactly writing it as enableSsl="true" or not because it is case sensitive in web.conmfig file..
You can also put these two lines in the code file also:
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;


If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..