Wednesday, 2 November 2011

Email Demo (simple email sending application)

----------------------copy code and paste it to Default.aspx page-------------------


  <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

  <html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
       <title>Send Email by .Net 2.0</title>
</head>
  <body>
      <form id="form1" runat="server">
       <div>
          <h2 style="background-color:Brown; color:Wheat; font-family:Verdana; font-size:14px" align=center>Please enter the following requested
                   information below to send us your comments.</h2>
               <table align=center>
                   <tr>
                       <td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">Your Name:</span></td>
                       <td><asp:textbox id="txtName" Width="241" Runat="server"></asp:textbox></td>
                   </tr>
                   <tr>
                       <td><span style="font-family:Verdana; font-size:12px; font-weight:bold;color:Brown;">Your Email Address:</span></td>
                      <td><asp:textbox id="txtEmail" Width="241" Runat="server"></asp:textbox></td>
                   </tr>
                   <tr>
                       <td colSpan="2" ><span style="font-family:Verdana; font-size:12px; font-weight:bold; color:Brown;">Your Comment:</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"> </td>
                   </tr>
                   <tr>
                       <td align=center><asp:button id="btnSendmail" Runat="server" Text="Send Mail" OnClick="btnSendmail_Click"></asp:button></td>
                     <td align=center><asp:button id="btnReset" Runat="server" Text="Reset" OnClick="btnReset_Click"></asp:button></td>
                  </tr>
                   <tr>
                       <td colSpan="2"><asp:label id="lblStatus" Runat="server" EnableViewState="False"></asp:label></td>
                  </tr>
               </table>
      </div>
       </form>
   </body>
   </html>



------------------Cody code below and past it 2 Default.aspx.cs page-----------------------
  using System;
   using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Net.Mail;
   
   public partial class _Default : System.Web.UI.Page
  {
       #region  "Send email"
       protected void btnSendmail_Click(object sender, EventArgs e)
       {
          // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0
           // System.Net.Mail.SmtpClient is the alternate class for this in 2.0
           SmtpClient smtpClient = new SmtpClient();
          MailMessage message = new MailMessage();
 
          try
           {
               MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text);
  
               // You can specify the host name or ipaddress of your server
               // Default in IIS will be localhost
               smtpClient.Host = "exch001";
  
               //Default port will be 25
               smtpClient.Port = 25;
  
               //From address will be given as a MailAddress Object
               message.From = fromAddress;
  
               // To address collection of MailAddress
               message.To.Add("swapnil.mahanagre@annetsite.com");
               message.Subject = "Feedback";
  
               // CC and BCC optional
               // MailAddressCollection class is used to send the email to various users
               // You can specify Address as new MailAddress("admin1@yoursite.com")
               //message.CC.Add("admin1@yoursite.com");
               //message.CC.Add("admin2@yoursite.com");
  
               // You can specify Address directly as string
             //  message.Bcc.Add(new MailAddress("admin3@yoursite.com"));
             //message.Bcc.Add(new MailAddress("admin4@yoursite.com"));
               //Body can be Html or text format
               //Specify true if it  is html message
               message.IsBodyHtml = false;
  
               // Message body content
               message.Body = txtMessage.Text;
           
               // Send SMTP mail
               smtpClient.Send(message);
  
               lblStatus.Text = "Email successfully sent.";
           }
           catch (Exception ex)
           {
               lblStatus.Text = "Send Email Failed.<br>" + ex.Message;
           }
       }
       #endregion
  
       #region "Reset"
       protected void btnReset_Click(object sender, EventArgs e)
       {
           txtName.Text = "";
           txtMessage.Text = "";
           txtEmail.Text = "";
       }
       #endregion
   }

No comments:

Post a Comment