Documente Academic
Documente Profesional
Documente Cultură
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);
<networkhost="somesmtpserver"port="25"userName="name"password="pass"defaultCreden
tials="true" />
</smtp>
</mailSettings>
</system.net>
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);
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);