8

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?

RYFN
  • 2,939
  • 1
  • 29
  • 40
  • I've ran into the exact same problem, did you ever find a solution? – HeavenCore May 18 '12 at 10:48
  • @HeavenCore , 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. – RYFN May 18 '12 at 10:57
  • 1
    Dang, thanks anyway, if i find anything i'll let you know. – HeavenCore May 18 '12 at 10:59
  • Do you have any news on this ? Same problem here... – Boas Enkler Dec 11 '12 at 17:17
  • 1
    @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 :) – RYFN Dec 12 '12 at 10:18
  • 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 mailbox – JsonStatham Sep 14 '16 at 12:29

1 Answers1

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

Frank Socha
  • 147
  • 2
  • 12
  • 1
    Worked for me. I needed to add a bit more logic for parsing out the 'To:' header for multiple recipients and over multiple lines. – Scott Oct 24 '12 at 19:10
  • Any experiences with this solution? – Boas Enkler Dec 11 '12 at 17:18
  • I'm not sure why this isn't the answer. Worked exactly how I needed it to. – Miles Feb 03 '15 at 17:47
  • 1
    ToAddress is not defined anywhere? – JsonStatham Sep 14 '16 at 12:20
  • 1
    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 ', or like this, 'To: username@contoso.com' – SeanOB Nov 18 '16 at 08:40