Attaching a File or an Outlook Item to an Outlook Message
Attaching a file to a Microsoft Outlook mail item is one of the most popular customer actions in Outlook.
Naturally, many power users also ask how to do this programmatically. The Outlook object model provides the Attachment object, Attachments.Add method, and an Attachments property for each of the Outlook item objects (for example, mail, appointment, contact, meeting, and task items), to support adding an attachment to an Outlook item. You can attach a copy of a file, an Outlook item like a message or contact, or an Object Linking and Embedding (OLE) object to an Outlook item. In particular, if you are attaching a file to a mail item that uses Rich Text Format (RTF), you can also specify the location to place the attachment in the body of the mail message, and the display name of the attachment.
Ken Getz, a Microsoft MVP and an expert in a multitude of technologies including Microsoft Office, Microsoft Visual Studio, Visual Basic, and C#, recently contributed a set of four code samples to illustrate a few tasks around attachments.
- How to: Attach a File to an Outlook Email Message illustrates the basic attachment task of programmatically attaching one or more files to an outgoing email message in Outlook. In order to send a mail message from a given SMTP email address, you also need to identify the corresponding Outlook account. This topic also shows the secondary tasks of finding the corresponding Account object given the SMTP address of the sender, creating the mail message, setting the recipients, subject, and body properties of the mail message, and finally sending the message that contains the attachments.
- How to: Attach an Outlook Contact Item to an Email Message shows another basic attachment task of attaching an Outlook contact item to an outgoing mail message. This topic includes a secondary task of creating a contact item before attaching it and sending it along with the mail message.
- How to: Limit the Size of an Attachment to an Outlook Email Message shows a practical task of how to programmatically check the total size of attachment files before actually sending a mail message. The topic shows a custom event handler for the Application.ItemSend event. The custom event handler uses the Size property of each attachment to sum up the total size of all attachments, compares the total size with the size limit, and cancels the send event if the total size exceeds the limit.
- How to: Modify an Attachment of an Outlook Email Message is a slightly more advanced topic that shows how to use the MAPI property PidTagAttachDataBinary and the PropertyAccessor object to programmatically modify an attachment file before sending it along with the mail message. This technique is useful if, for example, you want to systematically convert text file attachments to upper case, before actually sending the mail message. This topic also makes use of the Application.ItemSend event.
The sample code in these topics is presented in both C# and Visual Basic. Hope you can adapt the code to suit your scenarios.