使用Exchange获取邮件内容

本篇文章是利用Exchange协议来获取已发送邮件的内容。
1.依赖:

        <!--Exchange的api接口依赖-->
        <dependency>
            <groupId>com.microsoft.ews-java-api</groupId>
            <artifactId>ews-java-api</artifactId>
            <version>2.0</version>
        </dependency>
        <!--slf4j-api-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.32</version>
        </dependency>

2.具体代码:

public class EWSUtils {
    private static Logger log = LoggerFactory.getLogger(EWSUtils.class);

    public static ExchangeService getExchangeService(String username, String password, String domain) throws Exception {
        log.info("Begin to connect to server " + username);
        ExchangeService exService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        //参数是⽤户名,密码,域
        ExchangeCredentials credentials = new WebCredentials(username, password);
        //exService.setTraceEnabled(true);
        exService.setCredentials(credentials);
        //给出Exchange Server的URL http://xxxxxxx
        exService.setUrl(new URI("https://" + domain + "/ews/Exchange.asmx"));
        return exService;
    }
    /**
     * 功能:获取邮件内容并保存到⽂件夹中
     *
     * @param username
     * @param password
     * @param domain
     * @param viewCount
     * @param beginDate
     * @throws Exception
     */
    public static boolean readMailByEWS(
            String username,
            String password,
            String domain,
            int viewCount,
            Date beginDate,
            Date endDate
    ) throws Exception {
        ExchangeService exService = getExchangeService(username, password, domain);
        //绑定邮箱
        Folder inbox = Folder.bind(exService, WellKnownFolderName.Inbox);
        ItemView view = new ItemView(viewCount);
        //view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
        view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
        view.setPropertySet(new PropertySet(ItemSchema.DateTimeReceived));
        //创建过滤器
        SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(
                LogicalOperator.And,
                new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, beginDate),
                new SearchFilter.IsLessThanOrEqualTo(ItemSchema.DateTimeReceived, endDate)
        );
        FindItemsResults<Item> findResults = exService.findItems(inbox.getId(), searchFilter, view);
        if (findResults != null && findResults.getTotalCount() > 0) {
            exService.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
            for (Item item : findResults.getItems()) {
                EmailMessage msg = EmailMessage.bind(exService, item.getId());
                System.out.println("邮件内容 :" + msg.getBody());
            }
        }
        throw new Exception("邮件正文找不到!");
    }
}
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值