Monday 1 April 2013

ASP.NET: Sending Emails


ASP.NET: Sending Emails

Introduction

Sending email currently is important for applications for attracting people to use applications/websites. Sending email through websites is a better way to not allow any third party into your application. Some features like Feedback, Newsletter, Messages, Contact Us etc. are similar to sending email. That is because these features in a website sends email to the specified person.

There is also the need to send emails in ASP.Net.

Requirement

To integrate a Send email feature in your website, the most important requirement is for Simple Mail Transfer Protocol (SMTP). The other side requires a good design to show whatever you want to send in the email and code to do it.
The following namespace is required to add Simple Mail Transfer Protocol (SMTP) :
using System.Net.Mail
Source code

Add the following code in the source code page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            color#A52A2A;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<table align=center>
<tr>
<td><span style="font-family:Verdanafont-size:12pxfont-weight:bold;color:Brown;">To:</span></td>
<td><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td><span style="font-family:Verdanafont-size:12pxfont-weight:bold;color:Brown;">CC:</span></td>
<td><asp:textbox id="txtEmail" Width="241" Runat="server"></asp:textbox></td>
</tr>
<tr>
<td class="auto-style1"><strong>Subject&nbsp;</strong></td>
                                 <td>
<asp:TextBox ID="TextBox1" runat="server" Width="241"></asp:TextBox>
                    </td>
                           </tr>
                           <tr>
                                 <td colSpan="2" ><span style="font-family:Verdanafont-size:12pxfont-weight:bold; color:Brown;">Message:</span></td>
                           </tr>
                           <tr>
                                 <td align="center" colSpan="2" width=100%><asp:textbox id="txtMessage"Width="100%" Runat="server" Height="99" TextMode="MultiLine" MaxLength="400"></asp:textbox></td>
                           </tr>
                           <tr>
                                 <td colSpan="2">&nbsp;</td>
                           </tr>
                           <tr>
                                 <td align=center>
                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send" />
                    </td>
                                 <td align=center>&nbsp;</td>
                           </tr>
                           <tr>
                                 <td colSpan="2"><asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label></td>
                           </tr>
                    </table>
   
    </div>
    </form>
</body>
</html>

Design

The Page will look as in the following image:

ASP1.jpg

Code behind

Add the following code in the code behind page in the C# language on the Button Click event:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(txtName.Text);
       mail.To.Add(txtEmail.Text);
        mail.From = new MailAddress("email address from where you want to send emails");
        mail.Subject = "testing";

        string Body = txtMessage.Text;
        mail.Body = Body;

        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"//Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential
             ("add email address""password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
    }
}
Run
Now save all your work or press Ctrl+S and hit "F5" to run the page, the page will open in the default browser and will look as in the following image:

ASP2.jpg

Enter the required details and hit the button/send mail, to send to the specified person in "To" and "CC".
 

No comments:

Post a Comment