简体   繁体   中英

How do I identify the recipient email using ews-java-api if the address is an alias?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM