exchange邮件

exchange邮件

正常发送邮件只需要集成javax.mail就可以了
javamail如果发送邮件自动保存到已发送邮件里面,是需要自己实现的。but pop3协议是实现不了的,必须要实现imap协议。
但在工作中遇到了使用的是微软的邮箱,只有pop3,没有imap。我又不能让他们去开启imap,只能自己解决。
然后看到了exchange,就尝试了下。

1、引入jar包

compile "com.microsoft.ews-java-api:ews-java-api:2.0"

2、实现client

import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.service.folder.Folder;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class ExchangeClient {

    private String hostname;
    private String username;
    private String password;
    private String domain;

    private ExchangeService service;

    public ExchangeClient(String hostname, String username, String password) {
        this.hostname = hostname;
        this.username = username;
        this.password = password;
        this.service = getExchangeService();
    }

    public ExchangeClient(String hostname, String username, String password, String domain) {
        this.hostname = hostname;
        this.username = username;
        this.password = password;
        this.domain = domain;
        this.service = getExchangeService();
    }

    private ExchangeService getExchangeService() {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
        //用户认证信息
        ExchangeCredentials credentials;
        if (domain == null) {
            credentials = new WebCredentials(username, password);
        } else {
            credentials = new WebCredentials(username, password, domain);
        }
        service.setCredentials(credentials);
        try {
            service.setUrl(new URI("https://" + hostname + "/ews/Exchange.asmx"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return service;
    }

    /**
     * 收取邮件
     *
     * @param max 最大收取邮件数
     * @throws Exception
     */
    public ArrayList<EmailMessage> receive(int max) throws Exception {
        //绑定收件箱,同样可以绑定发件箱
        Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
        //获取文件总数量
        int count = inbox.getTotalCount();
        if (max > 0) {
            count = count > max ? max : count;
        }
        //循环获取邮箱邮件
        ItemView view = new ItemView(count);
        FindItemsResults<Item> findResults = service.findItems(inbox.getId(), view);
        ArrayList<EmailMessage> result = new ArrayList<>();
        for (Item item : findResults.getItems()) {
            EmailMessage message = EmailMessage.bind(service, item.getId());
            result.add(message);
        }
        return result;
    }

    /**
     * 收取所有邮件
     *
     * @throws Exception
     */
    public ArrayList<EmailMessage> receive() throws Exception {
        return receive(0);
    }

    /**
     * 发送带附件的mail
     *
     * @param subject         邮件标题
     * @param to              收件人列表
     * @param cc              抄送人列表
     * @param bodyText        邮件内容
     * @param attachmentPaths 附件地址列表
     * @throws Exception
     */
    public void send(String subject, List<String> to, List<String> cc, String bodyText, List<String> attachmentPaths) throws Exception {
        EmailMessage msg = new EmailMessage(service);
        msg.setSubject(subject);
        MessageBody body = MessageBody.getMessageBodyFromText(bodyText);
//        body.setBodyType(BodyType.HTML);
        msg.setBody(body);
        for (String toPerson : to) {
            msg.getToRecipients().add(toPerson);
        }
        if (cc != null) {
            for (String ccPerson : cc) {
                msg.getCcRecipients().add(ccPerson);
            }
        }
        if (attachmentPaths != null) {
            for (String attachmentPath : attachmentPaths) {
                msg.getAttachments().addFileAttachment(attachmentPath);
            }
        }
//        msg.send();
        msg.sendAndSaveCopy();
    }

    /**
     * 发送不带抄送人的邮件
     *
     * @param subject  标题
     * @param to       收件人列表
     * @param bodyText 邮件内容
     * @throws Exception
     */
    public void send(String subject, List<String> to, String bodyText) throws Exception {
        send(subject, to, null, bodyText);
    }

    /**
     * 发送不带附件的mail
     *
     * @param subject  邮件标题
     * @param to       收件人列表
     * @param cc       抄送人列表
     * @param bodyText 邮件内容
     * @throws Exception
     */
    public void send(String subject, List<String> to, List<String> cc, String bodyText) throws Exception {
        send(subject, to, cc, bodyText, null);
    }
}

3、成功实现
虽然成功了,但一开始并没有看到有sendAndSaveCopy方法,想着自己去实现保存,绕了一大圈

		//仅仅发送
        msg.send();
        //发送并保存到发件箱
        msg.sendAndSaveCopy();
  public void send() throws Exception {
    internalSend(null, MessageDisposition.SendOnly);
  }
  
  public void sendAndSaveCopy() throws Exception {
    this.internalSend(new FolderId(WellKnownFolderName.SentItems),
        MessageDisposition.SendAndSaveCopy);
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值