第一步,下载EWS JAVA API包
从如下路径下载EWS API包:http://code.msdn.microsoft.com/Exchange-EWS-Java-API-12-1a5a1143
第二步,下载依赖包
下载如下依赖包:
- Apache Commons HttpClient 3.1 (commons-httpclient-3.1.jar)
- Apache Commons Codec 1.4 (commons-codec-1.4.jar)
- Apache Commons Logging 1.1.1 (commons-codec-1.4.jar)
- JCIFS 1.3.15 (jcifs-1.3.15.jar)
也可以通过maven下载,EWSJavaAPI的jar包需要先手动安装,POM.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.yotoo
ReadEmail
war
1.0-SNAPSHOT
ReadEmail
http://maven.apache.org
UTF-8
1.6
1.4.7
1.7.3
3.8.1
javax.mail
${mail.version}
compile
org.jsoup
jsoup
${jsoup.version}
commons-httpclient
commons-httpclient
3.1
commons-codec
commons-codec
1.4
jcifs
jcifs
1.3.17
commons-logging
commons-logging
1.1.1
microsoft.exchange.webservices
EWSJavaAPI
1.2
junit
junit
${junit.version}
test
ReadEmail
第三步,示例代码
ReadMailViaEWS.java
public class ReadMailViaEWS {
public static void main(String[] args) throws Exception {
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
ExchangeCredentials credentials = new WebCredentials("用户名", "密码", "域");
service.setCredentials(credentials);
service.setUrl(new URI("https://"+"邮箱服务器地址"+"/EWS/Exchange.asmx"));
// Bind to the Inbox.
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
System.out.println(inbox.getDisplayName());
ItemView view = new ItemView(10);
FindItemsResults findResults = service.findItems(inbox.getId(), view);
for (Item item : findResults.getItems()) {
EmailMessage message = EmailMessage.bind(service, item.getId());
System.out.println(message.getSender());
System.out.println("Sub -->" + item.getSubject());
}
}
}