Tuesday 2 April 2013

Sending E- Mails in ASP.NET - Feedback E-mail


Sending E- Mails in ASP.NET - Feedback E-mail


Sending E-Mails through web sites can be seen in most of the web sites in nowadays. Here I am interested in creating a form and bind it to -Mail.

There can be many categories of sending E-Mail. Here I have discussed about 2 categories.


  1. Sending feedback to the web site owner.
  2. Acknowledgement E-Mail when a query or question is submitted through the web site. 




Feedback Email

First create a web form.

Form


Then create a Simple form as below.

Feedback Form


Code Snippet - ".aspx"
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="SendEmail.aspx.cs" Inherits="SendEmail"%>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head runat="server">
  7.     <title></title>
  8. </head>
  9. <body>
  10.     <form id="form1" runat="server">
  11.    
  12.     <table style="width:100%;">
  13.         <tr>
  14.             <td>
  15.                 <asp:Label ID="Label4" runat="server" style="font-weight: 700; color: #FF0000"
  16.                     Text="Contact Details"></asp:Label>
  17.             </td>
  18.             <td>
  19.                 &nbsp;</td>
  20.         </tr>
  21.         <tr>
  22.             <td>
  23.                 &nbsp;</td>
  24.             <td>
  25.                 &nbsp;</td>
  26.         </tr>
  27.         <tr>
  28.             <td>
  29.                 <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
  30.             </td>
  31.             <td>
  32.                 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
  33.             </td>
  34.         </tr>
  35.         <tr>
  36.             <td>
  37.                 <asp:Label ID="Label2" runat="server" Text="E-Mail Address"></asp:Label>
  38.             </td>
  39.             <td>
  40.                 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  41.             </td>
  42.         </tr>
  43.         <tr>
  44.             <td>
  45.                 <asp:Label ID="Label3" runat="server" Text="Comments"></asp:Label>
  46.             </td>
  47.             <td>
  48.                 <asp:TextBox ID="TextBox3" runat="server" Height="112px" TextMode="MultiLine"
  49.                     Width="284px"></asp:TextBox>
  50.             </td>
  51.         </tr>
  52.         <tr>
  53.             <td>
  54.                 <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
  55.                     Text="Send Feedback" />
  56.             </td>
  57.             <td>
  58.                 &nbsp;</td>
  59.         </tr>
  60.         <tr>
  61.             <td colspan="2">
  62.                 <asp:Label ID="Label5" runat="server"></asp:Label>
  63.             </td>
  64.         </tr>
  65.     </table>
  66.    
  67.     </form>
  68.     <p>
  69.         &nbsp;</p>
  70. </body>
  71. </html>


Next is to write the event for "Send Feedback" Button. 

Code Snippet - ".aspx.cs"
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Net.Mail;
  8. using System.Net.Security;
  9.  
  10. public partial class SendEmail : System.Web.UI.Page
  11. {
  12.     protected void Button1_Click(object sender, EventArgs e)
  13.     {
  14.         MailMessage mailMsg = new MailMessage();
  15.  
  16.         mailMsg.From = new MailAddress(TextBox2.Text);
  17.  
  18.         mailMsg.To.Add("admin@dotnettips4u.com");
  19.  
  20.         mailMsg.IsBodyHtml = true;
  21.  
  22.         mailMsg.Subject = "Contact Details";
  23.  
  24.         mailMsg.Body = "Contact Details" + "<b>Name:</b>" + TextBox1.Text + " <br/> <b>Email - address :</b>" + TextBox2.Text + "<br/> <b>Comments :</b>" + TextBox3.Text;
  25.  
  26.         SmtpClient smtp = new SmtpClient("smtp.gmail.com",587);
  27.  
  28.         mailMsg.Priority = MailPriority.Normal;
  29.  
  30.         smtp.Credentials = new System.Net.NetworkCredential("admin@dotnettips4u.com","yourPassword");
  31.  
  32.         smtp.Timeout = 25000;
  33.  
  34.         smtp.EnableSsl = true;
  35.  
  36.         smtp.Send(mailMsg);
  37.  
  38.         Label5.Text = "Thank you. Your contact details and feed back has been submitted.";
  39.  
  40.  
  41.     }
  42. }


Here the email will be sent to ""admin@dotnettips4u.com" as a Feedback.




Next I will show how to send an Acknowledgement E-Mail.

No comments:

Post a Comment