How To: Get a list of Outlook attachments
As a developer, sometimes you may want to know what is attached to the e-mail you have selected or opened in Outlook. Today I will show you how to iterate over the attachments collection and get the required information about attached items, such as file name, display name and type.
The Outlook Object Model provides the Attachments property for its Outlook items. So items like AppointmentItem, ContactItem, DocumentItem, JournalItem etc. have it. This property returns a collection of attachments which is represented in Outlook by the Attachments class. The key method we are currently interested in is the Item method. It accepts the parameter (stands for the object number in the collection) and returns an Attachment object.
The Attachment class provides you with such properties as the FileName, DisplayName, Type, Size and etc. Outlook 2007 introduced the following additions to the list of Attachment properties: BlockLevel that checks attachment file extension and lets us know if there is any restriction on it, PropertyAccessor that returns PropertyAccessor object, which in its turn supports creating, getting, setting, and deleting properties. And Outlook 2010 brought the GetTemporaryFilePath method to the Attachment class. This new method provides a full path to the attached file located in the temporary files folder. Please use the VBA Object Browser to get more information about these properties, methods and their parameters.
The code listed below iterates over all attached items and then shows a summary to the user using a message box. To get the code running you just need to pass an instance of the MailItem class to the GetAttachmentsInfo method.
C# and Add-in Express:
using System.Text; // ... private void GetAttachmentsInfo(Outlook.MailItem email) { StringBuilder attachmentInfo = new StringBuilder(); Outlook.Attachments mailAttachments = email.Attachments; if (mailAttachments != null) { for (int i = 1; i < = mailAttachments.Count; i++) { Outlook.Attachment currentAttachment = mailAttachments.Item(i); if (currentAttachment != null) { attachmentInfo.AppendFormat( "#{0}\n\rFile name: {1}\n\rDisplay Name: {2}\n\rType: {3}\n\n\r", i, currentAttachment.FileName, currentAttachment.DisplayName, currentAttachment.Type); Marshal.ReleaseComObject(currentAttachment); } } if (attachmentInfo.Length > 0) System.Windows.Forms.MessageBox.Show( attachmentInfo.ToString(), "E-mail attachments"); Marshal.ReleaseComObject(mailAttachments); } }
VB.NET and Add-in Express:
Imports System.Text '... Private Sub GetAttachmentsInfo(email As Outlook.MailItem) Dim attachmentInfo As StringBuilder = New StringBuilder() Dim mailAttachments As Outlook.Attachments = email.Attachments If Not IsNothing(mailAttachments) Then For i As Integer = 1 To mailAttachments.Count Dim currentAttachment As Outlook.Attachment = mailAttachments.Item(i) If Not IsNothing(currentAttachment) Then attachmentInfo.AppendFormat("#{0}", i) attachmentInfo.AppendLine() attachmentInfo.AppendFormat("File Name: {0}", currentAttachment.FileName) attachmentInfo.AppendLine() attachmentInfo.AppendFormat("Diplay Name: {0}", currentAttachment.DisplayName) attachmentInfo.AppendLine() attachmentInfo.AppendFormat("Type: {0}", currentAttachment.Type) attachmentInfo.AppendLine() attachmentInfo.AppendLine() Marshal.ReleaseComObject(currentAttachment) End If Next If attachmentInfo.Length > 0 Then System.Windows.Forms.MessageBox.Show( _ attachmentInfo.ToString(), "E-mail attachments") Marshal.ReleaseComObject(mailAttachments) End If End If End Sub
C# and VSTO:
using System.Text; using System.Runtime.InteropServices; //... private void GetAttachmentsInfo(Outlook.MailItem email) { StringBuilder attachmentInfo = new StringBuilder(); Outlook.Attachments mailAttachments = email.Attachments; if (mailAttachments != null) { for (int i = 1; i < = mailAttachments.Count; i++) { Outlook.Attachment currentAttachment = mailAttachments[i]; if (currentAttachment != null) { attachmentInfo.AppendFormat( "#{0}\n\rFile name: {1}\n\rDisplay Name: {2}\n\rType: {3}\n\n\r", i, currentAttachment.FileName, currentAttachment.DisplayName, currentAttachment.Type); Marshal.ReleaseComObject(currentAttachment); } } if (attachmentInfo.Length > 0) System.Windows.Forms.MessageBox.Show( attachmentInfo.ToString(), "E-mail attachments"); Marshal.ReleaseComObject(mailAttachments); } }
VB.NET and VSTO:
Imports System.Text Imports System.Runtime.InteropServices '... Private Sub GetAttachmentsInfo(email As Outlook.MailItem) Dim attachmentInfo As StringBuilder = New StringBuilder() Dim mailAttachments As Outlook.Attachments = email.Attachments If Not IsNothing(mailAttachments) Then For i As Integer = 1 To mailAttachments.Count Dim currentAttachment As Outlook.Attachment = mailAttachments.Item(i) If Not IsNothing(currentAttachment) Then attachmentInfo.AppendFormat("#{0}", i) attachmentInfo.AppendLine() attachmentInfo.AppendFormat("File Name: {0}", currentAttachment.FileName) attachmentInfo.AppendLine() attachmentInfo.AppendFormat("Diplay Name: {0}", currentAttachment.DisplayName) attachmentInfo.AppendLine() attachmentInfo.AppendFormat("Type: {0}", currentAttachment.Type) attachmentInfo.AppendLine() attachmentInfo.AppendLine() Marshal.ReleaseComObject(currentAttachment) End If Next If attachmentInfo.Length > 0 Then System.Windows.Forms.MessageBox.Show( _ attachmentInfo.ToString(), "E-mail attachments") Marshal.ReleaseComObject(mailAttachments) End If End If End Sub
See you on our forums and in the e-mail support!
12 Comments
Hallo,
habe das Problem, dass ich ein von Sharepoint downgeloadedes File (backend, object byte) in einem MAPI Folder speichern und anzeigen möchte (Outlook 2010). Zum Beispiel ein WordDocument.
MailItems, TaskItems etc zu erstellen ist kein Problem, aber wie sieht das ganze mit DocumentItems aus?
LG
Alex
Hello Alex,
Did you try to drag and drop Word documents into an Outlook folder? Is it what you are looking for?
hi,
How can i differentiate in line image from attachments?
Can you please how to do?
Hi Naresh,
To determine if an attachment is embedded, you try to get the PR_ATTACH_CONTENT_ID (PR_ATTACH_CONTENT_ID_W) MAPI property. If that property exists, then the attachment is embedded. To get this MAPI property, two methods are used:
– in Outlook 2000-2003, you use Extended MAPI
– in Outlook 2007-2010, you use PropertyAccessor object; this allows supporting Outlook 2010 64-bit in the same add-in
Please take a look at the similar forum thread at https://www.add-in-express.com/forum/read.php?FID=5&TID=10590 for more information about this. Also you can find there a sample add-in project in VB.NET (here is the direct link https://www.add-in-express.com/files/projects_pub/adx-ol-get-embedded-attachment-vb.zip).
I use Windows 7 64bit, Outlook 2010, and I am looking for a powershell script to download embedded pictures and attachments from Outlook email messages.
Right now the only option is open every message and copy every picture. The email messages are in Outlook currently but I can save these off to a folder if needed.
Can someone point me in the right direction?
https://social.msdn.microsoft.com/Forums/vstudio/en-US/6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64/how-to-embed-image-in-html-body-in-c-into-outlook-mail?forum=vsto
Hi,
PowerShell scripts are beyond our area of expertise but if they can communicate with ActiveX objects, then you can use the Outlook Object Model’s methods, properties and objects to access mail items and their attachments.
Hello,
How can i write code in Delphi for checking any attached files –> If there is any excel files *.xls with any text “cost” in the file name, then stop the command “Send” in Outlook mail and display message box “Please double check your files before sending mail again”. The command “Send” only can send email when don’t have any text “cost” at the file name excel in attached file mail Outlook.
Thank you.
Thai
Hello Thai,
Intercept the Application.ItemSend event (see https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/application-itemsend-event-outlook), retrieve the item being sent, cast the item to MailItem, scan MailItem.Attachments collection, if it contains a required file, cancel the event. Find a bit more info at https://www.add-in-express.com/forum/read.php?FID=1&TID=14087.
is there c# version for
https://www.add-in-express.com/files/projects_pub/adx-ol-get-embedded-attachment-vb.zip).
?
Hello Dvtasith,
There’s no C# version of that project. I wouldn’t recommend using that project unless you need to support pre-2007 Outlook versions.
To support Outlook 2007+, I would use the PropertyAccessor object to retrieve MAPI properties described at https://social.msdn.microsoft.com/Forums/vstudio/en-US/08c45486-cd48-4836-be90-d61883e92824/is-there-a-way-to-tell-the-difference-between-an-attached-file-and-an-image-that-is-embedded-in-an?forum=vsto to solve your task. If you encounter a scenario where your attachments aren’t identified as required, google for more suggestions from Ken Slovak or Dmitry Streblechenko.
Note that the project you refer to uses 32bit declarations while the PropertyAccessor object is bitness-independent; PropertyAccessor is only available in Outlook 2007 or higher.
How can I tell from the Attachment object whether the attachment is inline or not? Tks
Hello John,
Check what what Ken Slovak writes at https://social.msdn.microsoft.com/Forums/es-ES/d125cb20-389d-4f73-834e-ca9fd590ce7f/how-to-determine-the-type-of-the-outlook-attachment?forum=vsto.