'VSTO - Outlook how to trace email from Message ID

I'm developing a VSTO add-in for Outlook, Now I get a bounce back email, which included message ID. How can I trace the original email from this message ID in VSTO program?



Solution 1:[1]

You can search for PR_INTERNET_MESSAGE_ID property value. The DASL name of the property is http://schemas.microsoft.com/mapi/proptag/0x1035001F.

Use the Find/FindNext or Restrict methods of the Items class. The Restrict method is an alternative to using the Find method or FindNext method to iterate over specific items within a collection. The Find or FindNext methods are faster than filtering if there are a small number of items. The Restrict method is significantly faster if there is a large number of items in the collection, especially if only a few items in a large collection are expected to be found.

But if you need to find items from multiple folders I'd recommend using the AdvancedSearch method instead:

Public m_SearchComplete As Boolean  

Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)  
    If SearchObject.Tag = "MySearch" Then  
        m_SearchComplete = True  
    End If  
End Sub  

Sub TestSearchForMultipleFolders()  
    Dim Scope As String  
    Dim Filter As String  
    Dim MySearch As Outlook.Search  
    Dim MyTable As Outlook.Table  
    Dim nextRow As Outlook.Row  
    m_SearchComplete = False  
    'Establish scope for multiple folders  
    Scope = "'" & Application.Session.GetDefaultFolder( _  
    olFolderInbox).FolderPath _  
    & "','" & Application.Session.GetDefaultFolder( _  
    olFolderSentMail).FolderPath & "'"  
    'Establish filter  
    If Application.Session.DefaultStore.IsInstantSearchEnabled Then  
        Filter = Chr(34) & "http://schemas.microsoft.com/mapi/proptag/0x1035001F" _  
        & Chr(34) & " ci_phrasematch 'MesssageID'"  
    Else  
        Filter = Chr(34) & "http://schemas.microsoft.com/mapi/proptag/0x1035001F" _  
        & Chr(34) & " like '%MessageID%'"  
    End If  
    Set MySearch = Application.AdvancedSearch( _  
    Scope, Filter, True, "MySearch")  
    While m_SearchComplete <> True  
        DoEvents  
    Wend  
    Set MyTable = MySearch.GetTable  
    Do Until MyTable.EndOfTable  
        Set nextRow = MyTable.GetNextRow()  
        Debug.Print nextRow("Subject")  
    Loop  
End Sub

Solution 2:[2]

As Eugene mentioned, you need to search for the sent message based on the PR_INTERNET_MESSAGE_ID property. No reason to use Items.Restrict or even Find/FindNext - since you are expecting a single match (unless something is really wrong with your mailbox), a single call to Items.Find is all your need.

Also keep in mind that in the cached Exchange mode PR_INTERNET_MESSAGE_ID will not be set on the items in the Sent Items folder. To work round that, you'd need to open the Sent Items folder in the online mode (you can do that using Extended MAPI in C++/Delphi or Redemption (I am its author) in any language.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eugene Astafiev
Solution 2