xinyb
7 天以前 f632e7d7c25d8a86b1f6718f467bfef0d3c09058
src/com/yc/crm/mail/action/MailAccount.java
@@ -61,6 +61,10 @@
                    msg.setFail("请输入邮箱密码");
                    return msg;
                }
                account.setReceiveEmail(account.getEmail());
                account.setReceivePassword(account.getPassword());
                account.setSmtpEmail(account.getEmail());
                account.setSmtpPassword(account.getPassword());
            }
            if (account.getMailType().equals(1) || account.getMailType().equals(2)) {
                if (StringUtils.isBlank(account.getReceiveProtocol())) {
@@ -82,12 +86,6 @@
                if (account.getSmtpPort().equals(0)) {
                    msg.setFail("请输入发邮件服务器端口");
                    return msg;
                }
                if (account.getMailType().equals(1)) {
                    account.setReceiveEmail(account.getEmail());
                    account.setReceivePassword(account.getPassword());
                    account.setSmtpEmail(account.getEmail());
                    account.setSmtpPassword(account.getPassword());
                }
                if (account.getMailType().equals(2)) {
                    if (StringUtils.isBlank(account.getReceiveEmail())) {
@@ -131,6 +129,7 @@
            mailAccountIfc.addEmailAccount(account);//添加新的邮箱配置
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
                map.put("code", 0);
                map.put("companyId", companyId);
                map.put("companyName", companyName);
                map.put("userCode", userCode);
@@ -138,6 +137,7 @@
                map.put("email", account.getEmail());
                msg.setSuccess("已保存", map);
            } else {
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
@@ -189,11 +189,13 @@
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
                mailAccountIfc.updateAliasEmail(aliasEmail, accountId);
                map.put("code", 0);
                map.put("userCode", userCode);
                map.put("aliasEmail", aliasEmail);
                map.put("accountId", accountId);
                msg.setSuccess("已保存", map);
            } else {
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
@@ -286,6 +288,7 @@
            String error = emailDetection(account);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
                map.put("code", 0);
                map.put("userCode", userCode);
                map.put("userName", userName);
                map.put("email", account.getEmail());
@@ -293,6 +296,7 @@
            } else {
                //异常处理
                mailAccountIfc.updateMailStatus("异常", account.getAccountId());
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
@@ -407,6 +411,52 @@
    }
    /**
     * 检测绑定是否已经存在(是否已绑定)
     *
     * @param email
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/eEmailExists.do")
    public AllBackMsg eEmailExists(String email, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            if (StringUtils.isBlank(email)) {
                msg.setFail("邮箱不能为空");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //根据当前用户查询绑定的邮箱信息
            T482102Entity emailEntity = mailAccountIfc.getAccountInfo(userCode, email);//返回邮箱的账号信息
            Map<String, Object> map = new HashMap<>();
            if (emailEntity == null || StringUtils.isBlank(emailEntity.getEmail())) {
                map.put("code", 0);
                map.put("msg", "该邮箱未绑定");
            } else {
                map.put("code", -1);
                map.put("msg", "该邮箱已绑定");
            }
            msg.setSuccess("执行成功", map);
        } catch (MessagingException e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
    /**
     * 检测绑定是否有效
     *
     * @param email
@@ -440,7 +490,7 @@
            String error = emailDetection(emailEntity);
            Map<String, Object> map = new HashMap<>();
            if (StringUtils.isBlank(error)) {
//                map.put("code", "0");
                map.put("code", 0);
                map.put("email", emailEntity.getEmail());
                map.put("protocol", emailEntity.getReceiveProtocol());
                map.put("receiveHost", emailEntity.getReceiveHost());
@@ -451,12 +501,12 @@
                map.put("smtpPort", emailEntity.getSmtpPort());
                map.put("proxyFlag", emailEntity.isProxyFlag());
                map.put("biSyncFlag", emailEntity.isBiSyncFlag());
                map.put("mailStatus", emailEntity.getMailStatus());
                map.put("mailStatus", "正常");
                msg.setSuccess("执行成功", map);
            } else {
                //异常处理
                mailAccountIfc.updateMailStatus("异常", emailEntity.getAccountId());
//                map.put("code", "-1");
                map.put("code", -1);
                map.put("mailStatus", "异常");
                map.put("error", error);
                msg.setSuccess("执行成功", map);
@@ -481,17 +531,30 @@
    private String emailDetection(T482102Entity emailEntity) throws MessagingException {
        String errorMsg = "";
        try {
            String protocol = emailEntity.getReceiveProtocol().toLowerCase();//接收协议 imap pop3
            Properties props = new Properties();
            props.put("mail.store.protocol", protocol);//指定接收的邮件协议 IMAP over pop3
            if (protocol.contains("imap")) {
                props.put("mail.imaps.host", emailEntity.getReceiveHost());
                props.put("mail.imaps.port", emailEntity.getReceivePort());
            } else if (protocol.contains("pop3")) {
                props.put("mail.pop3.host", emailEntity.getReceiveHost());
                props.put("mail.pop3.port", emailEntity.getReceivePort());
            } else {
                props.put("mail.imaps.host", emailEntity.getReceiveHost());
                props.put("mail.imaps.port", emailEntity.getReceivePort());
            }
//            props.put("mail.imap.ssl.enable", emailEntity.isReceiveSSL()+""); // 使用 SSL 连接
//            props.put("mail.imap.starttls.enable", "true");//// IMAP 协议设置 STARTTLS
            props.put("mail.store.protocol", "imaps");//emailEntity.getReceiveProtocol()); // IMAP over SSL
            props.put("mail.imaps.host", emailEntity.getReceiveHost());
            props.put("mail.imaps.port", emailEntity.getReceivePort());
            props.put("mail.imaps.starttls.enable", "true");//// IMAP 协议设置 STARTTLS
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.ssl.enable", emailEntity.isSmtpSSL() + "");//启用SSL
            props.put("mail.smtp.host", emailEntity.getSmtpHost()); // 替换为你的SMTP服务器
            props.put("mail.smtp.port", emailEntity.getSmtpPort()); // 或者你使用的端口
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");//启用tls
            props.put("mail.smtp.ssl.enable", emailEntity.isSmtpSSL() + "");//启用SSL
//            props.put("mail.smtp.starttls.enable", "true");//启用tls
//            props.setProperty("mail.smtp.socketFactory.port", "587");//启用tls后port:587
            HashMap IAM = new HashMap();
            //带上IMAP ID信息,由key和value组成,例如name,version,vendor,support-email等。
@@ -509,7 +572,7 @@
            });
            //存储对象
            IMAPStore store = (IMAPStore) session.getStore(emailEntity.getReceiveProtocol());//imap协议或pop3协议类型(推荐你使用IMAP协议来存取服务器上的邮件。)
            IMAPStore store = (IMAPStore) session.getStore(protocol);//imap协议或pop3协议类型(推荐你使用IMAP协议来存取服务器上的邮件。)
            //连接
            store.connect(emailEntity.getReceiveHost(), emailEntity.getReceiveEmail(), emailEntity.getReceivePassword());
            store.id(IAM);//163邮箱需要,不然会报:A3 NO SELECT Unsafe Login. Please contact kefu@188.com for help
@@ -521,17 +584,10 @@
            folder.close(false);
            store.close();
//            Session mailSession = Session.getInstance(props, new Authenticator() {
//                @Override
//                protected PasswordAuthentication getPasswordAuthentication() {
//                    return new PasswordAuthentication(emailEntity.getEmail(), emailEntity.getPassword());
//                }
//            });
            Transport transport = session.getTransport();
            transport.connect();
            transport.close();
            mailAccountIfc.updateMailStatus("正常", emailEntity.getAccountId());
        } catch (MessagingException e) {
            errorMsg = e.getCause() != null ? e.getCause().getMessage() : e.getMessage();
        } catch (Exception e) {
@@ -540,6 +596,69 @@
        return errorMsg;
    }
    /**
     * 常规查询
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/getConvention.do")
    public AllBackMsg getConvention(HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //根据当前用户查询绑定的邮箱信息
        } catch (MessagingException e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
    /**
     * 常规更新
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/updateConvention.do")
    public AllBackMsg updateConvention(HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            //根据当前用户查询绑定的邮箱信息
        } catch (MessagingException e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
            SpObserver.setDBtoInstance();
        }
        return msg;
    }
    private T482102Entity getMailConfigProperties(T482102Entity t482102) {
        try {
            String email = t482102.getEmail();