Java---使用EWS讀取exchange郵件


第一步:

下載EWS 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)

(或者導入pom.xml):


4.0.0
com.yotoo
ReadEmail
war
1.0-SNAPSHOT
ReadEmail
http://maven.apache.org

<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>

第三步:

代碼示例:

 public class ReadExchangeMail{

    public static void main(String[] args){
        //使用exchange服務工具類創建服務
        //ExchangeMailUtil exchangeMailUtil = new ExchangeMailUtil(mailServer, user, password, readUrlPrefix);
        //ExchangeService service = exchangeMailUtil.getExchangeService();
        //創建exchange服務 ExchangeVersion.Exchange2007_SP1    (服務版本號)
          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(), itemView);
	ArrayList<Item> items = findResults.getItems();
       for(int i=0;i<items.size();i++){
	       EmailMessage message = EmailMessage.bind(service, items.get(i).getId());
                    message.load();
	        System.out.println(message.getSender());
	        System.out.println("Sub -->" +items.get(i).getSubject());
		System.out.println(“接收方:”+message.getReceivedBy());
		System.out.println("發送:"+message.getSender());
		System.out.println("發送人:"+message.getFrom());
		System.out.println("接收時間:"+items.get(i).getDateTimeReceived());
		System.out.println("是否已讀:"+message.getIsRead());
		System.out.println("郵件ID:"+items.get(i).getId());
      }
    }

}

:Java---關於如何使用EWS 寫個ExchangeMailUtil 詳見 :

   <a> https://www.cnblogs.com/itczybk/articles/11011744.html</a>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM