Sunteți pe pagina 1din 4

Sending Email In ASP.NET 2.

0
In ASP.NET, sending emails has become simpler. The classes required to send an email are
contained in the System.Net.Mail. The steps required to send an email from ASP.NET are as
follows :
Step 1: Declare the System.Net.Mail namespace
using System.Net.Mail;
Step 2: Create a MailMessage object. This class contains the actual message you want to
send. There are four overloaded constructors provided in this class. We will be using
public MailMessage ( string from, string to, string subject, string body )
The constructor of this MailMessage class is used to specify the sender email, receiver email,
subject, body.
MailMessage message = new MailMessage
("abc@somedomain.com","administrator@anotherdomain.com","Testing","This is a
test mail");
Step 3: To add an attachment to the message, use the Attachment class.
string fileAttach = Server.MapPath("myEmails") + "\\Mypic.jpg";
Attachment attach = new Attachment(fileAttach);
message.Attachments.Add(attach);
Step 4: After creating a message, use the SmtpClient to send the email to the specified SMTP
server. I will be using ‘localhost’ as the SMTP server.
SmtpClient client = new SmtpClient("localhost");
client.Send(message);

Additionally, if required, you


client.Timeout = 500;
// Pass the credentials if the server requires the client to authenticate before it will
send e-mail on the client's behalf.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
That’s it. It’s that simple.
To configure SMTP configuration data for ASP.NET, you would add the following tags to your
web.config file.
<system.net>
<mailSettings>
<smtpfrom="abc@somedomain.com">

<networkhost="somesmtpserver"port="25"userName="name"password="pass"defaultCreden
tials="true" />
</smtp>
</mailSettings>
</system.net>

Sending Email with ASP.NET


Something found on every web page today is an email form of some sort. In all probability this
will not change anytime soon, therefore I will demonstrate today how to send email via
ASP.NET: from plain to HTML mail and attachments.
The use of the source code in this article requires an installation of Microsoft's .NET Framework
SDK on a Web server. I also assume a certain familiarity of the reader with the C#
programming language.
The quickest way
There always is a way that can be classified under the heading 'Quick and Dirty'. And I again
want to begin with this way because we can test the server configuration quite easily without
having to take any side effects into consideration (SimpleMail.aspx).
<% @Page Language="C#" %>
<% @Import Namespace="System.Web.Mail" %>
<%
string strTo = "christophw@sleeper.Dev.AlfaSierraPapa.Com";
string strFrom = "webmaster@aspheute.com";
string strSubject = "Hi Chris";
SmtpMail.Send(strFrom, strTo, strSubject,
"A real nice body text here");

Response.Write("Email was queued to disk");


%>
If everything went well, the result looks like the following screenshot:

What actually happens in the script? The complete email support resides in the
System.Web.Mail namespace. In this namespace we find the class SmtpMail, whose static
Send method can accept four parameters:
SmtpMail.Send(From, To, Subject, BodyText);
As I said, it is very simple, but it has one thing in common with the more functional email
sending options: the SMTP Server must be the locally installed SMTP Service of the IIS - no
other can be used. In this respect the email support of ASP.NET is identical with the CDONTS
of ASP 3.0.
Sending HTML Email
But now we leave this all too simple method behind and look at a very functional object: the
MailMessage class. This class 'encapsulates' everything one can wish for in an email - the
following example demonstrates the use with a short HTML email
(SimpleMailMessage.aspx).
<% @Page Language="C#" %>
<% @Import Namespace="System.Web.Mail" %>
<%
MailMessage msgMail = new MailMessage();

msgMail.To = "christophw@sleeper.Dev.AlfaSierraPapa.Com";
msgMail.Cc = "webmaster@sleeper.Dev.AlfaSierraPapa.Com";
msgMail.From = "webmaster@aspheute.com";
msgMail.Subject = "Hi Chris, another mail";

msgMail.BodyFormat = MailFormat.Html;
string strBody = "<html><body><b>Hello World</b>" +
" <font color=\"red\">ASP.NET</font></body></html>";
msgMail.Body = strBody;

SmtpMail.Send(msgMail);

Response.Write("Email was queued to disk");


%>
The code looks much better now and above all, there are significantly more options than there
are in the first Send method. First we set the To, From and Subject properties of the
MailMessage then we set the BodyFormat to the value Html out of the MailFormat
enumeration. And then we already are set to send a HTML email.
Worth mentioning is the fact that all overloads of the Send method do not have return values
about the success of dispatching the email message. The reason for this is that the emails
simply are written into the Pickup folder of the Inetpub directory from where they are read
and then sent by the SMTP Service. Failed emails (dispatching errors) also are written into
files, this time however in the Badmail folder.
As an addendum, I want to show a screenshot of the email:

Sending Attachments
With certain email components, sending attachments can be a bit of an adventure. With .Net,
it is just another easy to use component: MailAttachment. The following code demonstrates
how an attachment can be added to the MailMessage (MailAttachment.aspx).
<% @Page Language="C#" %>
<% @Import Namespace="System.Web.Mail" %>
<%
MailMessage msgMail = new MailMessage();

msgMail.To = "christophw@sleeper.Dev.AlfaSierraPapa.Com";
msgMail.From = "webmaster@aspheute.com";
msgMail.Subject = "Attachment Test";

msgMail.BodyFormat = MailFormat.Text;
msgMail.Body = "Check out the attachment!";
msgMail.Attachments.Add(new MailAttachment("c:\\temp\\annual-report.pdf"));

SmtpMail.Send(msgMail);

Response.Write("Email was queued to disk");


%>
The line
msgMail.Attachments.Add(new MailAttachment("c:\\temp\\annual-report.pdf"));
might also be programmed as follows
MailAttachment maAttach = new MailAttachment("c:\\temp\\annual-report.pdf");
IList msgAttachments = msgMail.Attachments;
msgAttachments.Add(maAttach);
The point I want to make is that the attachments are handled through an IList interface (to be
found in the System.Collections namespace) - and this supplies the Add method. This IList
interface can also be used to enumerate or delete attachments. An additional remark has to be
made about the constructor of MailAttachment: the encoding type can be set (this is probably
rarely needed).
I still owe you a screenshot - this is what the attachment looks like in Outlook Express. Small
programming effort, big effect (when something meaningful is sent).

S-ar putea să vă placă și