zoukankan      html  css  js  c++  java
  • 使用Exchange读取邮件、发送邮件

    问题

    由于公司邮箱并未开放POP3收信端口,但配置exchange后,手机邮件客户端可以收发邮件,因此决定使用exchange协议收发邮件。但测试过程中发现登录时反复提示“440 Login Timeout ”错误,用户名密码并没有出错。

    解决方案

    OWA(Outlook Web Access)提供的EWS(Exchange Web Service)的协议地址原来是https://Exchange邮件服务器名/EWS/Exchange.asmx,修改后即可登录了。

    代码

    官方文档地址
    项目中添加ews-java-api.jar后即可,maven项目中添加

    <dependency>
        <groupId>com.microsoft.ews-java-api</groupId>
        <artifactId>ews-java-api</artifactId>
        <version>2.0</version>
    </dependency>

    工具类代码如下:

    
    import microsoft.exchange.webservices.data.core.ExchangeService;
    import microsoft.exchange.webservices.data.core.PropertySet;
    import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
    import microsoft.exchange.webservices.data.core.enumeration.property.BodyType;
    import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
    import microsoft.exchange.webservices.data.core.enumeration.search.SortDirection;
    import microsoft.exchange.webservices.data.core.enumeration.service.ConflictResolutionMode;
    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.core.service.schema.ItemSchema;
    import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
    import microsoft.exchange.webservices.data.credential.WebCredentials;
    import microsoft.exchange.webservices.data.property.complex.Attachment;
    import microsoft.exchange.webservices.data.property.complex.FileAttachment;
    import microsoft.exchange.webservices.data.property.complex.MessageBody;
    import microsoft.exchange.webservices.data.search.FindItemsResults;
    import microsoft.exchange.webservices.data.search.ItemView;
    import microsoft.exchange.webservices.data.search.filter.SearchFilter;
    
    import java.io.File;
    import java.net.URI;
    import java.net.URISyntaxException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Exchange邮件服务工具类
     *
     * @author Yang Cheng
     * @date 2017-02-13
     */
    public class ExchangeMailUtil {
    
        private String mailServer;
        private String user;
        private String password;
        private String domain;
    
        public ExchangeMailUtil(String mailServer, String user, String password) {
            this.mailServer = mailServer;
            this.user = user;
            this.password = password;
        }
    
        public ExchangeMailUtil(String mailServer, String user, String password, String domain) {
            this.mailServer = mailServer;
            this.user = user;
            this.password = password;
            this.domain = domain;
        }
    
        public static void main(String[] args) throws Exception {
            // Outlook Web Access路径通常为/EWS/exchange.asmx
            ExchangeMailUtil mailUtil = new ExchangeMailUtil("https://mail.domain.com/EWS/exchange.asmx",
                    "xx@domain.com", "xx");
            // 接收邮件
            ArrayList<EmailMessage> mails = mailUtil.receive(10);
            for (EmailMessage mail : mails) {
                System.out.println("邮件标题: " + mail.getSubject());
                System.out.println("接收时间: " + mail.getDateTimeReceived());
                System.out.println("发送人: " + mail.getFrom().getName() + ", 地址: " + mail.getFrom().getAddress());
                System.out.println("已读:" + mail.getIsRead());
                // 更新已读
                if (!mail.getIsRead()) {
                    mail.setIsRead(true);
                    mail.update(ConflictResolutionMode.AlwaysOverwrite);
                }
                //System.out.println("邮件内容 :" + mail.getBody());
    
                // 处理附件
                List<Attachment> attachs = mail.getAttachments().getItems();
                try {
                    if (mail.getHasAttachments()) {
                        for (Attachment attach : attachs) {
                            if (attach instanceof FileAttachment) {
                                //接收邮件到临时目录
                                System.out.println(attach.getName());
                                //File tempZip = new File("/tmp", attach.getName());
                                //((FileAttachment) attach).load(tempZip.getPath());
                            }
                        }
                        //删除邮件
                        //mail.delete(DeleteMode.HardDelete);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                System.out.println();
            }
    
            // 发送邮件
            //mailUtil.send("Subject", new String[] { "xxx@domain.com" }, null, "content");
    
        }
    
        /**
         * 创建邮件服务
         *
         * @return 邮件服务
         */
        private ExchangeService getExchangeService() {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            //用户认证信息
            ExchangeCredentials credentials;
            if (domain == null) {
                credentials = new WebCredentials(user, password);
            } else {
                credentials = new WebCredentials(user, password, domain);
            }
            service.setCredentials(credentials);
            try {
                service.setUrl(new URI(mailServer));
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            return service;
        }
    
        /**
         * 收取邮件
         *
         * @param max          最大收取邮件数
         * @param searchFilter 收取邮件过滤规则
         * @return
         * @throws Exception
         */
        public ArrayList<EmailMessage> receive(int max, SearchFilter searchFilter) throws Exception {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
            ExchangeCredentials credentials = new WebCredentials(user, password);
            service.setCredentials(credentials);
            service.setUrl(new URI(mailServer));
            //绑定收件箱,同样可以绑定发件箱
            Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
            //获取文件总数量
            int count = inbox.getTotalCount();
            if (max > 0) {
                count = count > max ? max : count;
            }
            //循环获取邮箱邮件
            ItemView view = new ItemView(count);
            //按照时间顺序收取
            view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
            FindItemsResults<Item> findResults;
            if (searchFilter == null) {
                findResults = service.findItems(inbox.getId(), view);
            } else {
                // e.g. new SearchFilter.SearchFilterCollection(
                // LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "EWS"),
                // new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"))
                findResults = service.findItems(inbox.getId(), searchFilter, view);
            }
            service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
            ArrayList<EmailMessage> result = new ArrayList<>();
            for (Item item : findResults.getItems()) {
                EmailMessage message = (EmailMessage) item;
                result.add(message);
            }
            return result;
        }
    
        /**
         * 收取所有邮件
         *
         * @throws Exception
         */
        public ArrayList<EmailMessage> receive(int max) throws Exception {
            return receive(max, null);
        }
    
        /**
         * 收取邮件
         *
         * @throws Exception
         */
        public ArrayList<EmailMessage> receive() throws Exception {
            return receive(0, null);
        }
    
        /**
         * 发送带附件的mail
         *
         * @param subject         邮件标题
         * @param to              收件人列表
         * @param cc              抄送人列表
         * @param bodyText        邮件内容
         * @param attachmentPaths 附件地址列表
         * @throws Exception
         */
        public void send(String subject, String[] to, String[] cc, String bodyText, String[] attachmentPaths)
                throws Exception {
            ExchangeService service = getExchangeService();
    
            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();
        }
    
        /**
         * 发送不带附件的mail
         *
         * @param subject  邮件标题
         * @param to       收件人列表
         * @param cc       抄送人列表
         * @param bodyText 邮件内容
         * @throws Exception
         */
        public void send(String subject, String[] to, String[] cc, String bodyText) throws Exception {
            send(subject, to, cc, bodyText, null);
        }
    
    }
    
  • 相关阅读:
    【NOIP2018PJ正式赛】摆渡车
    【NOIP2018PJ正式赛】龙虎斗
    【NOIP2018PJ正式赛】标题统计
    高精度除单精度
    关于输出的东东
    高精度乘单精度
    【NOIP2012模拟10.26】电影票
    【NOIP2012模拟10.26】雕塑
    【NOIP2012模拟10.26】火炬手
    【NOIP2016提高A组模拟9.7】千帆渡
  • 原文地址:https://www.cnblogs.com/yangcheng33/p/6557311.html
Copyright © 2011-2022 走看看