简体   繁体   English

在VSTO中使用交换电子邮件搜索Outlook收件箱

[英]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. 我正在尝试在一个加载项中想通过电子邮件地址进行搜索,但是问题是由于Outlook连接到Exchange服务器,所以所有电子邮件地址都以Exchange格式显示。 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. 当然, SenderEmailAddress与实际的电子邮件地址不匹配,因为SenderEmailAddress为Exchange格式。

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). 大多数Exchange邮件还将发件人SMTP地址显示为单独的属性( PidTagSenderSmtpAddress使用OutlookSpy查看邮件:单击IMessage按钮)。

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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