Search code examples
c#outlookvstoemail-bouncesmessageid

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

  • 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