How to Filter Email Messages from Exchange Mailbox inside Java Apps
Snippet Code
Rate this page :
[ 0 votes]
This technical tip shows how Java developers can access, filter messages from exchange mailbox inside Java application. The ExchangeClient class provides the listMessages() method which gets all messages from a mailbox. To get only messages which match some condition, use the overloaded ListMessages() method which takes the MailQuery class as an argument. The MailQuery class provides various properties for specifying conditions, for example date, subject, sender and recipient. In addition, the API also allows to apply case-sensitivity filters for retrieving emails from mailbox.
//The code examples below show how to connect to an IMAP mailbox and get messages that have the string "Newsletter" in the subject and were sent today.
ExchangeClient client = new ExchangeClient("http://MachineName/exchange/Username","username", "password", "domain");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Query building by means of ExchangeQueryBuilder class
ExchangeQueryBuilder builder = new ExchangeQueryBuilder();
// Subject contains "Newsletter"
builder.getSubject().contains("Newsletter");
// Emails that arrived today
try {builder.getInternalDate().on(sdf.parse("10/05/2015 10:00:00"));}
catch (ParseException e) {e.printStackTrace();
}
// Build the query
MailQuery query = builder.getQuery();
// Get list of messages
ExchangeMessageInfoCollection messages = client.listMessages(client.getMailboxInfo().getInboxUri(), query, false);
System.out.println("Imap: " + messages.size()+ " message(s) found.");
//IEWClient can also be used for this purpose as shown below:
// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
// Query building by means of ExchangeQueryBuilder class
ExchangeQueryBuilder builder = new ExchangeQueryBuilder();
// Subject contains "Newsletter"
builder.getSubject().contains("Newsletter");
// Emails that arrived today
try {builder.getInternalDate().on(sdf.parse("10/05/2015 10:00:00"));}
catch (ParseException e) {e.printStackTrace();
}
// Build the query
MailQuery query = builder.getQuery();
// Get list of messages
ExchangeMessageInfoCollection messages = client.listMessages(client.getMailboxInfo().getInboxUri(), query, false);
System.out.println("Imap: " + messages.size()+ " message(s) found.");
//Get Messages that Meet Specific Criteria
//Code Samples: Today's Date
// Emails that arrived today
MailQueryBuilder builder = new MailQueryBuilder();
builder.getInternalDate().on(new Date());
//Code Samples: Date Range
builder.getInternalDate().beforeOrEqual(new Date());
builder.getInternalDate().since(new Date(new Date().getTime() + TimeUnit.DAYS.toDays(1)));
//Code Samples: Specific Sender
// Get emails from specific sender
builder.getFrom().contains("saqib.razzaq@127.0.0.1");
//Code Samples: Specific Domain
// Get emails from specific domain
builder.getFrom().contains("SpecificHost.com");
//Code Samples: Specific Recipient
// Get emails sent to specific recipient
builder.getTo().contains("recipient");
//Code Samples: By MessageID
// Get email delivery notifications
builder.getMessageId().equals("MessageID");
//Code Samples: Combining Queries with AND
MailQueryBuilder builder = new MailQueryBuilder();
// Emails from specific host
builder.getFrom().contains("SpecificHost.com");
// AND all emails that arrived before today
builder.getInternalDate().before(new Date());
// AND all emails that arrived since 7 days ago
builder.getInternalDate().since(new Date(new Date().getTime() + TimeUnit.DAYS.toDays(-7)));
//Case-Sensitive Email Filtering
//IgnoreCase is True
MailQueryBuilder builder1 = new MailQueryBuilder();
builder1.getFrom().contains("tesT", true);
MailQuery query1 = builder1.getQuery();