Eugene Astafiev

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

Post a comment

Have any questions? Ask us right now!