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

1

8

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?

Randi answered 2/6, 2011 at 10:53 Comment(6)
I've ran into the exact same problem, did you ever find a solution?Lustreware
@Lustreware , unfortunately not, we ended up adding a hashtag into the email subject line to tell our app how to process the email. e.g. a subject of "blah blah #news" would be processed as a news item.Randi
Dang, thanks anyway, if i find anything i'll let you know.Lustreware
Do you have any news on this ? Same problem here...Alcorn
@BoasEnkler unfortunately not, the project is sitting unused, so I've yet to have a chance to try anything else. Frank's solution looks to be worth trying though! Let us know if you find it works :)Randi
Using a hastag in the subject is prone to user error. This is very annoying as I need to know who the intended address was for, whilst having all of the emails arrive in one mailboxDrawstring
I
9

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

Isley answered 30/5, 2012 at 0:45 Comment(5)
Worked for me. I needed to add a bit more logic for parsing out the 'To:' header for multiple recipients and over multiple lines.Magnesium
Any experiences with this solution?Alcorn
I'm not sure why this isn't the answer. Worked exactly how I needed it to.Banking
ToAddress is not defined anywhere?Drawstring
This looks great, but I have a case where the alias address is not enclosed in angle brackets. Any idea how to account for both scenarios? That is, the To: email could look like this, 'To: User Name <[email protected]>', or like this, 'To: [email protected]'Eloquent

© 2022 - 2024 — McMap. All rights reserved.