Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exchange Web Services (EWS) API "To" header for alias

I have an inbox set up in exchange, [email protected]

Additionally, there is an alias for this, [email protected], 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 [email protected], and examine the Internet headers of the message using Microsoft Outlook, the To: header reads To: Hello <[email protected]> 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?

like image 696
RYFN Avatar asked Jun 02 '11 10:06

RYFN


People also ask

Is the EWS API deprecated?

Today we are announcing the deprecation of the 25 least used APIs of EWS for Exchange Online (as determined by the call volume into the service). We are retiring these APIs to begin the process of reducing the surface area of the EWS protocol for maintenance and security purposes.

What is Exchange EWS used for?

Exchange Web Services (EWS) is an application program interface (API) that allows programmers to access Microsoft Exchange items such as calendars, contacts and email.


1 Answers

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

like image 174
Frank Socha Avatar answered Sep 24 '22 01:09

Frank Socha