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

Introduction: In previous articles i explained How to send mail with multiple attachments in asp.net with C#,Vb.Net and  How to send emails in asp.net using Gmail in asp.net  and How to send emails in asp.net | How to set Smtp setting in web.config file to send email in asp.net.
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 asp.net application to understand.
  • In the design page (.aspx) design the page as:
<table style="width:100%;">
    <tr>
        <td>
            Send To(Email Address)</td>
        <td>
            <asp:TextBox ID="txtEmailId" runat="server" Columns="60"></asp:TextBox>
            </td>
    </tr>
    <tr>
        <td>
            Subject</td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server" Columns="60"></asp:TextBox>
            </td>
    </tr>
    <tr>
        <td>
            Body</td>
        <td>
            <asp:TextBox ID="txtBody" runat="server" Columns="60" Rows="5"
                TextMode="MultiLine"></asp:TextBox>
            </td>
    </tr>
    <tr>
        <td>
             </td>
        <td>
            <asp:Button ID="btnSendEmail" runat="server" onclick="btnSendEmail_Click"
                Text="Send Email" />
            </td>
    </tr>  
</table>
  • In the web.config file write as:
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" >
        <network defaultCredentials="false"
                 enableSsl="true"
                 host="smtp.gmail.com"
                 port="587"
                 userName="YourGmailEmail@gmail.com"
                 password="YourGmailPassword"/>
      </smtp>
    </mailSettings>

  </system.net>
  <appSettings>
    <add key="EmailID" value=" YourGmailEmail @gmail.com"/>
  </appSettings>

C#.NET  Code to send mail using gmail in asp.net
  • 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();       
        smtp.Send(message);
    }

VB.NET Code to send mail using gmail in asp.net
  • 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()
       smtp.Send(message)
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."
Previous
Next Post »

2 comments

Click here for comments
Unknown
admin
October 29, 2013 ×

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

Reply
avatar
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;

Reply
avatar

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..