首页 > 代码库 > 通过 EWS JAVA API读取exchange邮件

通过 EWS JAVA API读取exchange邮件

第一步,下载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

<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

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());
		}

	}

}