第一步,下載EWS JAVA API包 java
從以下路徑下載EWS API包:http://code.msdn.microsoft.com/Exchange-EWS-Java-API-12-1a5a1143 web
第二步,下載依賴包 apache
下載以下依賴包: 服務器
- 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
也能夠經過maven下載,EWSJavaAPI的jar包須要先手動安裝,POM.xml ui
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.yotoo</groupId> <artifactId>ReadEmail</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>ReadEmail</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <jdk.version>1.6</jdk.version> <mail.version>1.4.7</mail.version> <jsoup.version>1.7.3</jsoup.version> <junit.version>3.8.1</junit.version> </properties> <dependencies> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>${mail.version}</version> <scope>compile</scope> </dependency> <!-- jsoup HTML parser library --> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>${jsoup.version}</version> </dependency> <!-- Compiling the EWS Java API --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>jcifs</groupId> <artifactId>jcifs</artifactId> <version>1.3.17</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>microsoft.exchange.webservices</groupId> <artifactId>EWSJavaAPI</artifactId> <version>1.2</version> </dependency> <!-- Compiling the EWS Java API --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>ReadEmail</finalName> </build> </project>第三步,示例代碼
ReadMailViaEWS.java url
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<Item> 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()); } } }