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 详见 :
https://www.cnblogs.com/itczybk/articles/11011744.html

猜你喜欢

转载自www.cnblogs.com/itczybk/p/11128469.html