Set the attachment data, content-type and file name from an existing file.
Use this method to quickly an easily build a MailAttachment object from a file. The Data property is set to a byte array containg the file data, the Type property is calculated from the file name, and the Name property is set to the file name. Note that all these properties can be modified after calling the Load method.
| Exception Type | Condition |
|---|---|
| ArgumentNullException | filePath is a null reference (Nothing in Visual Basic). |
| ArgumentException | filePath contains invalid characters, is empty, or contains only white spaces. |
| FileNotFoundException | filePath cannot be found. |
| SecurityException | The caller does not have the required permission. |
| DirectoryNotFoundException | filePath is invalid, such as being on an unmapped drive. |
| UnauthorizedAccessException | The access requested is not permitted by the operating system for the specified filePath. |
| PathTooLongException | The specified filePath exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters. |
| IOException | An I/O error occurs. |
| OutOfMemoryException | filePath content length exceeds the int.MaxValue value. |
This sample shows how to load a MailAttachment object from a file
Mail oMail = new Mail(); MailAttachment oAttachment = new MailAttachment(); oAttachment.Load(@"c:\documentations\product_specs.pdf"); oMail.Attachments.Add(oAttachment);
MailAttachment Class | MailMill Namespace | Data | Type | Name