Moyosoft Products | Services | Download | Contact us    
Java Bridge to Exchange
import java.text.*;
import java.util.*;

import com.moyosoft.exchange.*;
import com.moyosoft.exchange.folder.*;
import com.moyosoft.exchange.item.*;
import com.moyosoft.exchange.mail.*;
import com.moyosoft.exchange.search.*;

public class FindMail
{
  public static void main(String[] argsthrows ExchangeServiceException
  {
    // To provide hostname and credentials information, use:
    // Exchange exchange = new Exchange("hostname", "username", "password");
    Exchange exchange = ExchangeConnectionDialog.display();
    
    if(exchange == null)
    {
      return;
    }

    // Get the Inbox folder
    ExchangeFolder inbox = exchange.getInboxFolder();

    // Create a restriction to match messages with a subject that
    // contains the strings "Java" or "Exchange":
    Restriction restriction = 
      If.Message.Subject.contains("Java").or().contains("Exchange");

    // Search the inbox and sort items by the reception date:
    ItemsCollection items = inbox.getItems().
      restrict(restriction).sortBy(ExchangeItemField.item_DateTimeReceived);
    
    System.out.println("Messages found: " + items.getCount());
    
    for(ExchangeItem item : items)
    {
      ExchangeMail mail = (ExchangeMailitem;

      System.out.println("Subject: " + mail.getSubject());
      System.out.println("From: " + mail.getFromName());
      System.out.println("Received: " + formatDateToString(mail.getDateTimeReceived()));
      System.out.println();
    }
  }

  private static String formatDateToString(Date date)
  {
    return new SimpleDateFormat().format(date);
  }
}