Can't get sender email address with EWS Java API
Asked Answered
M

2

11

I am using EWS Java API 1.1.5. I am trying to get the email sender as follows:

ItemView view = new ItemView(10);
FindItemsResults<Item> findResults = service.findItems(
    WellKnownFolderName.Inbox, view);
    for (Item item : findResults.getItems()) {
        if (item instanceof EmailMessage) {
            String senderEmail = ((EmailMessage) item).getSender().getAddress();
            System.out.println("Sender Email: " + senderEmail);
        }
    }
);

But it always returns null. I also tried the following:

String senderEmail = ((EmailMessage) item).getFrom().getAddress();

But it also returns null. I can see that the email contains sender like:

Test User <[email protected]> 

It appears in the message and also when replying to the email.

Please advise how to fix this issue.

Marguerita answered 5/11, 2012 at 11:49 Comment(1)
Using the .NET API, if you do not call Load (as per the Answer), the From and Sender for an internal e-mail are exchange addresses (RoutingType = "EX"). But if you call Load then they are SMTP addresses - yay.Gurkha
M
15

found the solution, i have to use item.load(); before getting the sender email address.

Marguerita answered 11/11, 2012 at 10:18 Comment(0)
K
0

While the above works perfectly, as an alternative, loading is not necessary if the search is done with the view of the properties, consider the code snippet

    ItemView view = new ItemView(100);
    view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
    view.setPropertySet(new PropertySet(BasePropertySet.IdOnly, 
                                        ItemSchema.Subject,
                                        ItemSchema.DateTimeReceived, 
                                        EmailMessageSchema.From));
    
    SearchFilterCollection searchCollection=new SearchFilter.SearchFilterCollection();
    searchCollection.add(new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, !unreadEmailOnly));
    
    FindItemsResults<Item> result=exchangeService.findItems(WellKnownFolderName.Inbox, 
                                                            searchCollection, 
                                                            view);

Subsequently, EmailMessage.getFrom().getAddress() can be called.

Kellyekellyn answered 19/7, 2020 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.