Monday, November 24, 2008

Sending Mail with ASP.Net

Sending Email with ASP.NET
Well Today’s website is almost incomplete without the Email feature as any kind of website either informative and a database driven or etc everyone require the email sending and may require the email communication. So, therefore I will demonstrate how to send email via ASP.NET with HTML mail and attachments.
We require an installation of Microsoft's .NET Framework SDK on a Web server.
Prerequisite ::-> C# programming.
The Smooth 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 = "Pradeep.bisht@XYZ.com";
string strFrom = "webmaster@XYZ.com";
string strSubject = "Hi Crazy Programmer";

SmtpMail.Send(strFrom, strTo, strSubject,
"Dear Mr.XXX you seems to be awesome developer,WelCome to XYZ ");
Response.Write("Email was stacked");

What exactly is going ?, The Complete email is supported by the namespace System.Web.Mail.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 = "pradeep@XYZ.com";
msgMail.Cc = "webmaster@XYZ.com";
msgMail.From = "webmaster@XYZ.com";
msgMail.Subject = "Hi Pradeep, Nextmail";

msgMail.BodyFormat = MailFormat.Html;
string strBody = "Hello World" +
" ASP.NET";
msgMail.Body = strBody;

SmtpMail.Send(msgMail);

Response.Write("Email was stacked");




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.

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 = "pradeep@XYZ.com";
msgMail.From = "webmaster@XYZ.com";
msgMail.Subject = "Test Attachment ";

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

SmtpMail.Send(msgMail);

Response.Write("Email was stacked ");




The line


msgMail.Attachments.Add(new MailAttachment("c:\\temp\\weekly-report.pdf"));

might also be programmed as follows

MailAttachment maAttach = new MailAttachment("c:\\temp\\weekly-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).