xinyb
2024-09-04 4284431e6755e854a7de7afb2ef09e461d3e72eb
src/com/yc/crm/mail/action/MailAccount.java
@@ -12,10 +12,14 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.mail.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
 * @BelongsProject: eCoWorksV3
@@ -62,7 +66,13 @@
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            mailAccountIfc.addEmailAccount(account);//添加新的邮箱配置
            msg.setOk("已保存");
            Map<String, Object> map = new HashMap<>();
            map.put("companyId", companyId);
            map.put("companyName", companyName);
            map.put("userCode", userCode);
            map.put("userName", userName);
            map.put("email", account.getEmail());
            msg.setSuccess("已保存", map);
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
@@ -83,13 +93,14 @@
    public AllBackMsg updateAccount(@RequestBody T482102Entity account, HttpServletRequest request, HttpServletResponse response) {
        AllBackMsg msg = new AllBackMsg();
        try {
            if(account.getAccountId()==0){
            if (account.getAccountId() == 0) {
                msg.setFail("获取不到邮箱配置ID,请检查");
                return msg;
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if(StringUtils.isBlank(userCode)){
            String userName = (String) session.getAttribute(SessionKey.USER_NAME);
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
@@ -97,7 +108,11 @@
            DataSourceEntity dataSource = MultiDataSource.getDataSourceMap(request);//获取数据源
            SpObserver.setDBtoInstance("_" + dataSource.getDbId());
            mailAccountIfc.updateEmailAccount(account);
            msg.setOk("已更新");
            Map<String, Object> map = new HashMap<>();
            map.put("userCode", userCode);
            map.put("userName", userName);
            map.put("email", account.getEmail());
            msg.setSuccess("已更新", map);
        } catch (Exception e) {
            msg.setFail(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
        } finally {
@@ -124,7 +139,7 @@
            }
            HttpSession session = request.getSession();
            String userCode = (String) session.getAttribute(SessionKey.USERCODE);
            if(StringUtils.isBlank(userCode)){
            if (StringUtils.isBlank(userCode)) {
                msg.setFail("登录用户已过期,请检查");
                return msg;
            }
@@ -161,7 +176,7 @@
            List<T482102Entity> t482102Entities = mailAccountIfc.getAccount(userCode, mail);
            if (t482102Entities.size() > 0) {
                msg.setSuccess("执行成功", t482102Entities.get(0));
            }else{
            } else {
                msg.setFail("未找到配置信息,0条数据");
            }
        } catch (Exception e) {
@@ -171,4 +186,72 @@
        }
        return msg;
    }
    /**
     * 检测绑定是否有效
     *
     * @param email
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/isEmailValid.do")
    public AllBackMsg isEmailValid(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);//返回邮箱的账号信息
            if (emailEntity == null || StringUtils.isBlank(emailEntity.getEmail())) {
                msg.setFail("找不到邮箱:" + email + "配置信息请完善。");
                return msg;
            }
            Properties props = new Properties();
            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");
            if (emailEntity.getSmtpHost().contains("163")) {//163邮箱设置
                props.put("mail.smtp.ssl.enable", "true");
            }
            Session mailSession = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailEntity.getEmail(), emailEntity.getPassword());
                }
            });
//            Store store = mailSession.getTransport("imaps"); // 使用IMAP协议
//            store.connect();
//            Session mailSession = Session.getInstance(props, null);
//            mailSession.setDebug(false);
//
            Transport transport = mailSession.getTransport();
            transport.connect();
            transport.close();
//            transport.connect(emailEntity.getSmtpHost(),emailEntity.getEmail(), emailEntity.getPassword());
//            transport.sendMessage(new MimeMessage(mailSession), new Address[]{new InternetAddress(emailEntity.getEmail())});
//            transport.close();
            msg.setOk("有效邮箱");
        } 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;
    }
}