I will demonstrate today how to send email via ASP.NET: from plain to HTML mail and attachments.
Sending Simple Mail:
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 Everybody"; SmtpMail.Send(strFrom, strTo, strSubject, "A real nice body text here"); Response.Write("Email was queued to disk"); %>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);
Sending HTML E-Mail:
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 Everybody, 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.
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);
I hope this might help you.
Thanks
No comments:
Post a Comment