简体   繁体   English

如果地址是别名,如何使用ews-java-api识别收件人电子邮件?

[英]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. 我正在使用ews-java-api,用于处理传入到特定Exchange帐户的电子邮件,以便可以从电子邮件中提取关键信息(例如,主题,正文,收件人,发件人等)通过API调用转发到另一个系统。 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...". 例如,如果我从janedoe@mycompany.com向bobsmith@mycompany.com发送电子邮件,则可以从“ bobsmith”帐户中获取电子邮件,并读取主题,正文等。但是,如果Bob Smith具有别名,例如,“ hero@mycompany.com”转到他的bobsmith帐户,Jane Doe通过电子邮件将其发送到该地址,我只看到“ bobsmith@mycompany.com”为收件人,而不是“ 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. 我似乎在Exchange项目上找不到任何方法调用(即使将其转换为“ EmailMessage”类型,也可以让我获取“ to:”字段中使用的地址)。

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. 好的,感谢@diginoise,最终的解决方案是执行以下操作。 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. 我首先使用默认属性集并添加了mime内容,以便我的属性查询将包括mime内容。 I then add a regex to examine the mimecontent directly to get the alias that might have been used: 然后,我添加一个正则表达式直接检查mimecontent以获得可能已经使用的别名:

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. 如果您仅查找列出的第一个电子邮件地址,但不处理别名列表,则此方法有效,因此您需要修改模式并在“收件人:”行上搜索多个实例并将其提取出来,但这涵盖了如何获取实际的“发送到”地址而不是“接收者”地址的基础知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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