Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JWebDAV for Exchange
>
Tutorial
> Search for messages received in last 10 minutes
The following example shows you how to search for messages received in last 10 minutes.
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.TimeZone; import com.independentsoft.webdav.exchange.Condition; import com.independentsoft.webdav.exchange.ContentClassType; import com.independentsoft.webdav.exchange.From; import com.independentsoft.webdav.exchange.LogicalOperator; import com.independentsoft.webdav.exchange.Mailbox; import com.independentsoft.webdav.exchange.MessagePropertyName; import com.independentsoft.webdav.exchange.MultiStatus; import com.independentsoft.webdav.exchange.Operator; import com.independentsoft.webdav.exchange.Property; import com.independentsoft.webdav.exchange.PropertyName; import com.independentsoft.webdav.exchange.SearchResult; import com.independentsoft.webdav.exchange.SearchResultRecord; import com.independentsoft.webdav.exchange.Select; import com.independentsoft.webdav.exchange.SqlQuery; import com.independentsoft.webdav.exchange.WebdavClient; import com.independentsoft.webdav.exchange.WebdavException; import com.independentsoft.webdav.exchange.Where; public class Example { public static void main(final String[] args) { try { WebdavClient client = new WebdavClient("https://myserver/exchange/emailaddress", "username", "password"); Mailbox myMailbox = client.getMailbox("https://myserver/exchange/emailaddress"); List
propertyNames = new ArrayList
(); propertyNames.add(MessagePropertyName.CONTENT_CLASS); propertyNames.add(MessagePropertyName.SUBJECT); propertyNames.add(MessagePropertyName.BODY); propertyNames.add(MessagePropertyName.FROM_EMAIL); propertyNames.add(MessagePropertyName.DATE_RECEIVED); Select select = new Select(propertyNames); From from = new From(myMailbox.getInbox()); Where where = new Where(); Calendar localCalendar = Calendar.getInstance(); localCalendar.add(Calendar.MINUTE, -15); Date time = localCalendar.getTime(); Condition condition1 = new Condition(MessagePropertyName.CONTENT_CLASS, Operator.EQUALS, ContentClassType.MESSAGE); Condition condition2 = new Condition(MessagePropertyName.DATE_RECEIVED, Operator.GREATER_THEN_OR_EQUALS, time); where.add(condition1); where.add(LogicalOperator.AND); where.add(condition2); SqlQuery sqlQuery = new SqlQuery(select, from, where); MultiStatus multiStatus = client.search(sqlQuery); SearchResult searchResult = new SearchResult(multiStatus, propertyNames); SearchResultRecord[] allRecords = searchResult.getRecords(); for (int i = 0; i < allRecords.length; i++) { System.out.println("Message URL=" + allRecords[i].getUrl()); Property[] property = allRecords[i].getProperties(); System.out.println("ContentClass=" + property[0].getValue()); System.out.println("Subject=" + property[1].getValue()); System.out.println("Body=" + property[2].getValue()); System.out.println("FromEmail=" + property[3].getValue()); System.out.println("DateReceived=" + toLocalTime(property[4].getValue())); } } catch (WebdavException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } static Date toLocalTime(String utcTime) throws ParseException { if (utcTime != null && utcTime.length() > 0) { String format = "yyyy-MM-dd'T'HH:mm:ss.sss'Z'"; DateFormat dateFormat = new SimpleDateFormat(format, Locale.US); Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); dateFormat.setCalendar(utcCalendar); return dateFormat.parse(utcTime); } else { return null; } } }