none
Determine attechments in mail item RRS feed

  • Question

  • Hi,

    I want to determine if there is files attached to a mailitem. Attachments.Count gives me both 1 and 2 even if there is no file attached to the mail. I guess it is something in the mail (embedded) that adds number to the Count property?

    Wednesday, January 11, 2017 6:54 AM

Answers

  • Hello Ollza,

    The message body may contain embedded images. They are stored as hidden attachments. That's why you see attachments.

    To detect hidden attachments (used in the message body) you can search for the "cid:" attribute inside the <img > tags.

    Sub ShowVisibleAttachmentCount()
        Const PR_ATTACH_CONTENT_ID As String = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
        Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"
    
        Dim m As MailItem
        Dim a As Attachment
        Dim pa As PropertyAccessor
        Dim c As Integer
        Dim cid as String
    
        Dim body As String
    
        c = 0
    
        Set m = Application.ActiveInspector.CurrentItem
        body = m.HTMLBody
    
        For Each a In m.Attachments
            Set pa = a.PropertyAccessor
            cid = pa.GetProperty(PR_ATTACH_CONTENT_ID)
    
            If Len(cid) > 0 Then
                If InStr(body, cid) Then
                Else
                    'In case that PR_ATTACHMENT_HIDDEN does not exists, 
                    'an error will occur. We simply ignore this error and
                    'treat it as false.
                    On Error Resume Next
                    If Not pa.GetProperty(PR_ATTACHMENT_HIDDEN) Then
                        c = c + 1
                    End If
                    On Error GoTo 0
                End If
            Else
                c = c + 1
            End If
        Next a
        MsgBox c
    End Sub

    Attachments referenced in the message body have the PR_ATTACH_CONTENT_ID property set on them, the DASL name is:

    "http://schemas.microsoft.com/mapi/proptag/0x3712001F"


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Proposed as answer by Chenchen LiModerator Thursday, January 12, 2017 2:41 AM
    • Marked as answer by Ollza Thursday, January 12, 2017 3:13 PM
    Wednesday, January 11, 2017 10:55 AM

All replies

  • Hello Ollza,

    The message body may contain embedded images. They are stored as hidden attachments. That's why you see attachments.

    To detect hidden attachments (used in the message body) you can search for the "cid:" attribute inside the <img > tags.

    Sub ShowVisibleAttachmentCount()
        Const PR_ATTACH_CONTENT_ID As String = "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
        Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"
    
        Dim m As MailItem
        Dim a As Attachment
        Dim pa As PropertyAccessor
        Dim c As Integer
        Dim cid as String
    
        Dim body As String
    
        c = 0
    
        Set m = Application.ActiveInspector.CurrentItem
        body = m.HTMLBody
    
        For Each a In m.Attachments
            Set pa = a.PropertyAccessor
            cid = pa.GetProperty(PR_ATTACH_CONTENT_ID)
    
            If Len(cid) > 0 Then
                If InStr(body, cid) Then
                Else
                    'In case that PR_ATTACHMENT_HIDDEN does not exists, 
                    'an error will occur. We simply ignore this error and
                    'treat it as false.
                    On Error Resume Next
                    If Not pa.GetProperty(PR_ATTACHMENT_HIDDEN) Then
                        c = c + 1
                    End If
                    On Error GoTo 0
                End If
            Else
                c = c + 1
            End If
        Next a
        MsgBox c
    End Sub

    Attachments referenced in the message body have the PR_ATTACH_CONTENT_ID property set on them, the DASL name is:

    "http://schemas.microsoft.com/mapi/proptag/0x3712001F"


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    • Proposed as answer by Chenchen LiModerator Thursday, January 12, 2017 2:41 AM
    • Marked as answer by Ollza Thursday, January 12, 2017 3:13 PM
    Wednesday, January 11, 2017 10:55 AM
  • PR_ATTACH_CONTENT_ID  test would not be 100% fool-proof test. Lotus Notes sets Content-ID MIME header on all attachments. It would only work if you look at the HTML body to see if any img tag refers to that attachment by the cid.

    Test for PR_ATTACHMENT_HIDDEN (DASL name http://schemas.microsoft.com/mapi/proptag/0x7FFE000B) on the attachment itself is a better test. You can also test if SmartNoAttach (DASL name http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B) is set on the message itself (Outlook hides the paperclip icon if that property is set).


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Wednesday, January 11, 2017 6:17 PM
  • Thanks. It seems to work if i check if the cid is emopty it is not embedded or hidden file and then i count it.
    for (int a = 1; a <= oMailItem.Attachments.Count; a++)
    {
    oOutlookPropertyAccessor = oMailItem.Attachments[a].PropertyAccessor;
    sCid = oOutlookPropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F");
       if (String.IsNullOrEmpty(sCid))
       {
          iFileAttachments++; 
       }                                
    }

    Thursday, January 12, 2017 3:13 PM
  • Once again, that check will fail for the messages sent through Lotus Notes or any other app that included Content-Id for all attachments.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Thursday, January 12, 2017 3:40 PM