Wednesday, October 11, 2006

Making and sending HTML email

@ The Process of Creating and Sending an E-mail Message: To create and send an e-mail message, follow these steps:

  1. Create a MailMessage object. MailMessage and other mail-related classes are in the System.Net.Mail namespace.
  2. If you did not specify the recipients in the MailMessage constructor, add them to the MailMessage object.
  3. If you need to provide multiple views (such as plain text and HTML), create AlternateView objects and add them to the MailMessage object.
  4. If necessary, create one or more Attachment objects and add them to the MailMessage object.
  5. Create an SmtpClient object, and specify the SMTP server.
  6. If the SMTP server requires clients to authenticate, add credentials to the SmtpClient object.
  7. Pass your MailMessage object to the SmtpClient.Send method. Alternatively, you can use SmtpClient.SendAsync to send the message asynchronously.

How to create a MailMessage

^ 一定要首先创建空的MailMessage,然后把MailMessage.ToMailMessage.FromMailMessage.SubjectMailMessage.Body四个property依次添加到MailMessage中。还可以添加MailMessage.CcMailMessage.Bcc

^ 因为每次发送邮件不一定会发给多少个人,所以写程序时不区分发给单人还是多人的情况。把所有得到的收件人地址都放进一个MailAddressCollection中。

@ BCC stands for "blind carbon copy".

@ The problem with BCC is that spam filters frequently block messages that do not have the recipient's email address in the From header, Therefore, if you use BCC, the message will very likely be filtered.

How to create HTML email?

@ To create an HTML email message, supply HTML-tagged content for MailMessage.Body and set MailMessage.IsBodyHtml attribute to True.
Dim OneMailMessage = New MailMessage
String OneMailMessageHtmlBody = "XHTML codes ..."
OneMailMessage.IsBodyHtml = True
OneMailMessage.Body = OneMailMessageHtmlBody

@ Most email clients will ignore the head section, and will ignore any client-side scripts, and will not automatically download images from Web sites.

@ To embed images into an HTML message so that they appear when the user clicks the message (without requiring the user to explicitly choose to download images): first creat an HTML message using AlternateView and then add images using LinkedResource classes.

^ 因为有的邮件客户端或者服务器会阻止图片内容,或者只支持普通文本格式的邮件,或者用户设定只接受文本邮件,所以需要提供alternate view以根据情况显示邮件内容。

e 4guysfromrolla.com Sending Email in ASP.NET 2.0
e 4guysfromrolla.com Sending Email in ASP.NET 2.0: HTML-Formatted Emails, Attachments, and Gracefully Handling SMTP Exceptions

e Complete FAQ for the System.Net.Mail namespace found in .NET 2.0