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

	}

}

转载于:https://my.oschina.net/yotoo/blog/288428

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值