Flylib.com

Books Software

 
 
 

Adding E-Mail Attachments

   

Adding E-Mail Attachments

Most e- mails contain attachments, so it is important that the SmtpMail object allow for these in some fashion. To create an attachment, the MailAttachment object enables a file attachment to be created; this attachment then can be added to the Ilist collection. The Ilist collection (found in the System.Collections namespace) handles all e-mail attachments.

Attaching a file to an e-mail can be done using one of two methods . First, you can issue a call to the Add() method of the Attachments property. The Add() method takes a single parameter, the pathname to the file. If a MailMessage object were already declared, the following code would be used to make a file attachment.

Visual Basic Example

MailObj.Attachmenets.Add(new MailAttachment("C:\temp\mail_example.aspx"))

C# Example

MailObj.Attachmenets.Add(new MailAttachment("C:\temp\mail_example.aspx"));

An alternative is to create the attachment, add it to the Ilist collection, and then add the Ilist collection to the MailMessage object. This method is useful if you are collecting the attachments in other locations in the code. The code for that process would look like the following:

Visual Basic Example

MailAttachment myAttachment = new MailAttachment("C:\temp\mail_example.aspx") 

Ilist myAttachmentList = mailObj.Attachments 

myAttachmentList.Add(myAttachment)

C# Example

MailAttachment myAttachment = 

   new MailAttachment("C:\temp\mail_example.aspx"); 

Ilist myAttachmentList = mailObj.Attachments; 

myAttachmentList.Add(myAttachment);

From these examples, you can see that using the MailAttachment object to add attachments is easy and straightforward.

   
   

Setting the E-Mail Format

One advantage of most e-mail readers is their capability to process HTML formatting within the message body. This enables an e-mail to present itself just like a Web page. The System.Web.Mail namespace contains a format object called MailFormat with two enumerated types: Text or HTML. To set the MailMessage format property just assign it a value from the MailFormat object as follows :

mailObj.BodyFormat = MailFormat.Text

or

mailObj.BodyFormat = MailFormat.HTML
   
   

Setting E-Mail Priority

The e-mail priority indicates the importance of an e-mail. If you are using Microsoft Outlook, then a high priority e-mail has a red exclamation sign beside the message and a low priority has a blue downward- facing arrow. If an e-mail has a normal priority, then nothing is used to indicate the message importance. Use the following syntax to assign a priority to an e-mail:

MailObj.Priority = MailPriority.High

You also can use Normal and Low priority enumerations of the MailPriority object.

   
   

Setting E-Mail Encodings for the E-Mail Body and Attachments

E-mail encoding is used to pass information from the sender to the recipient using a desired format. Typically ASCII encoding is used for all e- mails , but in case an alternative format is needed, the BodyEncoding property is available to change the encoding type. The Encoding object contains four enumerations of encoding. Different encoding schemes might be necessary for both older legacy systems, and when Unicode support for international characters is needed. Table 15.3 describes each of these.

Table 15.3. E-Mail Encoding Types

Encoding Type

Description

ASCII

Encodes characters as single 7-bit ASCII characters.

Unicode

Encodes characters as two consecutive bytes. Little-endian (code page 1200) and Big-endian (code page 1201) byte orders are supported.

UTF7

UCS Transformation Format (UTF), 7-bit form. Can also be accessed as code page 65000.

UTF8

UCS Transformation Format (UTF), 8-bit form. Can also be accessed as code page 65001.

To apply the encoding to the e-mail message body, the following syntax can be used:

mailObj.BodyEncoding = Encoding.ASCII