Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday, 4 February 2015

Introduction
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. We can configure smtp details in webconfig, so we don’t need to write the code again. We can just use the setting from web config.


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 write the code in the code behind file(.aspx.cs)  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

0 comments:

Post a Comment