We will have requirements to send email from our asp.net applications. This article will help us understand some of the available techniques we can use to send email from asp.net applications. With the introduction of .Netframework 2.0, the classes for sending email are packed in System.Net.Mail namespace as opposed to System.Web.Mail namespace in 1.x framework.
This article will focus on following concept Simplest way of
sending email Writing HTML Email Creating Email with attachment.
Import the namespace, and create instances of required classes
We are going to use the SmtpMail
and MailMessage classes,
which belong to the System.Web.Mail
namespace. In order to use them, we will need to import the System.Web.Mail namespace like this:
<%@ Import
Namespace="System.Web.Mail" %>
Exploring the Classes in the System.Net.Mail
Namespace
There are 16 different classes in the System.Net.Mail
namespace, all related to send email to a specified Simple Mail Transfer Protocol (SMTP)
server for delivery. The two core classes in this namespace are:
MailMessage- represents an email message; has properties likeFrom,To,Subject,Body, and so on.SmtpClient- sends a specifiedMailMessageinstance to a specified SMTP server.
When sending an email from an ASP.NET 2.0 page you will, typically:
- Create a
MailMessageobject - Assign its properties
- Create an instance of the
SmtpClientclass - Specify details about the
SMTP server to use (if they're not already specified within
Web.config) - Send the
MailMessagevia theSmtpClientobject'sSendmethod
Steps 1 and 2 may be bypassed as the SmtpClient
class's Send method can
accept either a MailMessage
object or four strings, representing the from, to, subject, and body contents
of the email message.
The System.Net.Mail namespace's other classes allow for more advanced email functionality. There are classes that can be used to add attachments to an email message, to embed objects within an email, to specify SMTP server authentication information, and Exception-derived classes for handling SMTP-specific exceptions.
One of the most common functionalities used in Web
development is sending email from a Web page.
Using the code
The HTML Design contains provision to enter sender�s name, email id and his comments. On click of the send email button the details will be sent to the specified email .
The Send mail functionality is similar to Dotnet 1.1 except for few changes
- System.Net.Mail.SmtpClient is used instead of System.Web.Mail.SmtpMail (obsolete in Dotnet 2.0).
- System.Net.MailMessage Class is used instead of System.Web.Mail.MailMessage (obsolete in Dotnet 2.0)
- The System.Net.MailMessage class collects From address as MailAddress object.
- The System.Net.MailMessage class collects To, CC, Bcc addresses as MailAddressCollection.
- MailMessage Body Format is replaced by IsBodyHtml
.ASPX CODE:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="sendmail.aspx.cs"
Inherits="sapnamalik_sendmail"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled
Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table id="tblemail" runat="server" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<fieldset>
<legend style="border: none;
font-weight: bold; color: #660099; font-size: 12pt;
font-family:
Segoe UI">Send Your Email </legend>
<table id="tblbody" runat="server" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td colspan="2" align="center">
Fields
marked with an asterisk (<asp:Label ID="Label1" runat="server" CssClass="redtext"
Text="*"></asp:Label>)are
mandatory
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblmail"
CssClass="redtext"
runat="server"></asp:Label></td>
</tr>
<tr>
<td width="10%" align="right">
<asp:Label ID="lblto" runat="server"
Text="To:"></asp:Label>
<asp:Label ID="lblredstar1"
runat="server"
CssClass="redtext"
Text="*"></asp:Label>
</td>
<td align="left"
width="90%">
<asp:TextBox ID="txtemailto"
Width="50%"
runat="server"
TabIndex="1"></asp:TextBox>


Chandu KumawatPosted Oct 12, 2015, 7:16 AM
good 1