简体   繁体   中英

Search outlook inbox with exchange email in VSTO

I'm trying to write an add-in where I'd like to search by email address, but the problem is since my outlook is connected to a Exchange server all the email addresses are shown in the Exchange format. What I'm trying to achieve is search by the email itself like you would in this example code -

MAPIFolder inboxFolder = Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
Items emails = inboxFolder.Items.Restrict($"[SenderEmailAddress] = abc@xyz.com");

Of course SenderEmailAddress doesn't match the actual email address because SenderEmailAddress is in Exchange format.

Currently I'm having to go through all emails and then reserve lookup the actual email address using this piece of code, but its obviously time consuming, hence my question about if there is a workaround.

private static string GetSenderEmailAddress(MailItem mail)
{
    AddressEntry sender = mail.Sender;
    string SenderEmailAddress = "";

    if (sender.AddressEntryUserType == OlAddressEntryUserType.olExchangeUserAddressEntry || sender.AddressEntryUserType == OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
    {
        ExchangeUser exchUser = sender.GetExchangeUser();
        if (exchUser != null)
        {
            SenderEmailAddress = exchUser.PrimarySmtpAddress;
        }
     }
     else
     {
            SenderEmailAddress = mail.SenderEmailAddress;
     }

     return SenderEmailAddress;
}

Most Exchange messages also expose the sender SMTP address as a separate property ( PidTagSenderSmtpAddress - take a look at a message with OutlookSpy : click IMessage button).

You can modify your search query to the following:

@SQL=(SenderEmailAddress = 'abc@xyz.com') or ("http://schemas.microsoft.com/mapi/proptag/0x5D01001F" = 'abc@xyz.com')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM