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.

   


Special Edition Using ASP. NET
Special Edition Using ASP.Net
ISBN: 0789725606
EAN: 2147483647
Year: 2002
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net