How to find sender of an attached email with ews
-
03-07-2021 - |
Question
In EWS, the EmailMessage
has a sender (emailMessage.getSender()
) and may has attachments (emailMessage.getAttachments()
).
These attachments can be FileAttachment
(for files) or ItemAttachment
(for attached email).
There is a lot of information on this item attachment such as :
- To : ((ItemAttachment) attachment).getItem().getDisplayTo();
- Subject : ((ItemAttachment) attachment).getItem().getSubject();
- see http://msdn.microsoft.com/en-us/library/exchange/aa580790(v=exchg.140).aspx
But how to find the sender of this attached email ?
Edit : Using EWS Java API 1.1.5 http://archive.msdn.microsoft.com/ewsjavaapi/Release/ProjectReleases.aspx?ReleaseId=5754
Solution 2
Following the SliverNinja response, I tried to retrieve the item as an email message.
Using EWS with Java, you have to cast the item ItemAttachment
as EmailMessage
like this:
Item item = ((ItemAttachment) attachment).getItem();
if (item instanceof EmailMessage) {
String sender = ((EmailMessage)item).getSender().getAddress();
}
The item can also be cast as Appointment or Contact or Task or ContactGroup.
Edit : Another way to get the sender is
((EmailMessage) item).getFrom().getAddress();
This looks like to do the same
OTHER TIPS
In c# - you could access the ItemAttachment.Message
and then Message.Sender
. Once you have the sender, you can retrieve Sender.Mailbox
for accessing the Mailbox.EmailAddress
. Maybe you can convert this to something similar for java.
ItemAttachment itemAttachment = attachment as ItemAttachment;
itemAttachment.Load();
Sender sender = itemAttachment.Message.Sender;
Mailbox mailbox = sender.Mailbox;
string email = mailbox.EmailAddress;