1

I'm working with the ews-java-api, which I'm using to process incoming emails to specific Exchange accounts, so that I can extract out key information from the email (ie, subject, body, recipient, sender, etc) to forward on to another system through an API call. I'm able to identify the recipient of the email, because it naturally matches the account I'm retrieving new emails from, but I can't seem to identify what alias the sender may have used to send the email.

For example, if I send an email from janedoe@mycompany.com to bobsmith@mycompany.com, I can then grab an email from the "bobsmith" account, and read the subject, body, etc. But if Bob Smith has an alias of, say, "hero@mycompany.com" which goes to his bobsmith account, and Jane Doe emails him to that address, I only see "bobsmith@mycompany.com" as the recipient, not "hero...". I can't seem to find any method calls on the Exchange item (even when cast as an "EmailMessage" type, that allows me to get the address used in the "to:" field.

Does anyone know how to obtain that alias on the received message?

spye
  • 21
  • 3
  • check this answer: http://stackoverflow.com/questions/6213571/exchange-web-services-ews-api-to-header-for-alias – diginoise Aug 03 '16 at 21:59
  • That's an excellent starting point... thank you for catching that, as I couldn't find that earlier. However, that's C# code, and a few of the items don't translate correctly to the ews-java-api library. I'm not that familiar with C#, so I'm not sure how to translate things like the propertySet syntax, or the tryGetProperty functions, as they don't seem to have functional Java equivalents. – spye Aug 04 '16 at 12:58
  • All you need is to inspect the appropriate headers, which the above example lists. Since headers are part of the protocol, as long as your java lib allows you to extract the headers, you should be fine (I think). – diginoise Aug 04 '16 at 13:49
  • Well, it took a fair bit to figure out, and between the link you posted and [this one](http://stackoverflow.com/questions/27507359/how-i-can-get-email-headers-programmatically-from-ms-exchange-server-in-java) it looks like the issue was related to adding ItemSchema.MimeContent to the PropertySet, and then using a regular expression to the find the alias in the "to" field. I'll update the question with that resolution, because although it's not overly clean, I was able to make it work. Thanks for pointing me in the right direction! – spye Aug 04 '16 at 18:55

1 Answers1

1

Okay, so, thanks to @diginoise, the resulting solution was to do the following. I didn't post code initially, but hopefully this will be helpful for anyone else searching for this same issue.

I started by using the default property set and added the mime content so that my property query would include mime content. I then add a regex to examine the mimecontent directly to get the alias that might have been used:

FindItemsResults<Item> findResults = ...; // This is several lines, but is well documented in the library

// Adding MimeContent to the set is key
PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent);
service.loadPropertiesForItems(findResults, propertySet);

for (Item item : findResults) {
    String messageContent = new String(((EmailMessage) item).getMimeContent().getContent());

    // find the alias used
    Pattern pattern = Pattern.compile("To: \"(.*)\" <(.*?)>");
    Matcher matcher = pattern.matcher(messageContent);
    if (matcher.find()) {
        System.out.println("Alias is: " + matcher.group(1));
    }
}

This works if you're only looking for the first email address listed, but won't handle a list of aliases, so you'd need to modify the Pattern and search for multiple instances on the "To:" line and extract them out, but this covers the basics of how to get the actual "sent to" address rather than the "received by" address.

spye
  • 21
  • 3