shauli algawi
shauli algawi

Reputation: 71

Java EWS API Search mail by date

I'm building a web client mail which works with the EWS Java API 2.0.

I would like to get mail from inbox according to a specific date.

This is what i have already:

public void foo(String dateToCompare){

        try {

            DateTime dateTime = DateTime.parse("2016-02-01T08:00:00Z");

            ItemView view = new ItemView(ServiceConst.ListLimit);


            SearchFilter searchFilter = new SearchFilter.IsGreaterThan(EmailMessageSchema.DateTimeReceived, dateTime);
            FindItemsResults<Item> itemsList = this.exchangeService.findItems(WellKnownFolderName.Inbox,searchFilter, view);

        } catch (Exception e) {
//           TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

And this is the error i get:

The request failed. Values of type 'org.joda.time.DateTime' can't be used for the 'Value' attribute.

I understand that the joda.DateTime class is not valid for it , so what is the alternative?

Upvotes: 1

Views: 2192

Answers (2)

Tiago Medici
Tiago Medici

Reputation: 2194

Once you are sending the email, you can have already the ID of the message:

From the class :

microsoft.exchange.webservices.data.core.service.item.EmailMessage

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); service.setUrl(new URI(url));

ExchangeCredentials credentials = new WebCredentials(user, password, domain);
service.setCredentials(credentials);
service.setTraceEnabled(trace);


EmailMessage message = new EmailMessage(service);
message.save();
message.load();
String uniqueMessagID = message.getConversationId();
message.send();

the get conversation id gives an ID that's equals when reading it like above

Once you are searching for the read emails you use :

microsoft.exchange.webservices.data.search.FindItemsResults<Item>
microsoft.exchange.webservices.data.core.service.item.Item
microsoft.exchange.webservices.data.core.PropertySet
microsoft.exchange.webservices.data.core.enumeration.property.BasePropertySet
microsoft.exchange.webservices.data.core.ExchangeService
microsoft.exchange.webservices.data.core.service.item.EmailMessage

        ItemView view = new ItemView(numberOfItemResult);
        view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
        view.setPropertySet(new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived));

        FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox, new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "")), view);

        if (findResults != null) {

            PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties);
            itempropertyset.setRequestedBodyType(BodyType.Text);

            for (Item item : findResults) {
                if (item instanceof EmailMessage) {
                    item.load(itempropertyset);
                    EmailMessage emailMessage = ((EmailMessage) item);
                    emailMessage.getConversationId().getUniqueId();
                }

            }
        }

Upvotes: 1

Scala
Scala

Reputation: 11

You have to use java.util.Date class, not joda.DateTime class.

Here is an example to search based on received time as you want

Upvotes: 1

Related Questions