Exchange Web Services (EWS) API “To” header for alias

隐身守侯 提交于 2019-11-27 06:06:31

问题


I have an inbox set up in exchange, hello@mycompany.com

Additionally, there is an alias for this, news@mycompany.com, so all emails to the news address end up in the hello inbox.

Ideally, I want to be able to tell which alias an email has been sent to, using EWS.

When I send an email to news@mycompany.com, and examine the Internet headers of the message using Microsoft Outlook, the To: header reads To: Hello <news@mycompany.com> which is exactly what I want to see.

However, using EWS, when I look at the ToRecipients property of the message, the reported email address is always that of the primary SMTP address. Also the InternetMessageHeaders property of the Webservices.Data.Item does not contain the To: property. I also can't seem to see the correct address using EWSEditor to examine all the properties of the message.

The answer to this forum post seems to suggest that,

...The Information about the actual email address a message is sent to is stored in the recipients collection which you can't access (outside of exportmessage) in EWS...

How would I go about doing this programatically so I can find the correct To: address?


回答1:


This works for me:

    private static string GetToAddress()
    {
        ExchangeService exService = new ExchangeService();
        exService.Credentials = new NetworkCredential("username", "password", "domain");
        exService.Url = new Uri("https://youraddress/EWS/Exchange.asmx");

        ExtendedPropertyDefinition PR_TRANSPORT_MESSAGE_HEADERS = new ExtendedPropertyDefinition(0x007D,MapiPropertyType.String);
        PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties)
                                    {PR_TRANSPORT_MESSAGE_HEADERS, ItemSchema.MimeContent};

        FindItemsResults<Item> fiResults = exService.FindItems(WellKnownFolderName.Inbox, new ItemView(1));
        foreach (Item itItem in fiResults.Items)
        {
            itItem.Load(psPropSet);
            Object valHeaders;
            if (itItem.TryGetProperty(PR_TRANSPORT_MESSAGE_HEADERS, out valHeaders))
            {
                Regex regex = new Regex(@"To:.*<(.+)>");
                Match match = regex.Match(valHeaders.ToString());
                if (match.Groups.Count == 2)
                    return match.Groups[1].Value;
            }
            return ToAddress;
        }
        return "Cannot find ToAddress";
    }

The code is from: http://social.technet.microsoft.com/Forums/en-au/exchangesvrdevelopment/thread/1e5bbde0-218e-466e-afcc-cb60bc2ba692



来源:https://stackoverflow.com/questions/6213571/exchange-web-services-ews-api-to-header-for-alias

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!