Adding E-Mail Attachments
Most e-
Attaching a file to an e-mail can be done using one of two
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
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-
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-
Table 15.3. E-Mail Encoding Types
To apply the encoding to the e-mail message body, the following syntax can be used: mailObj.BodyEncoding = Encoding.ASCII |