SpringBoot 整合163邮箱 阿里云25端口问题

SpringBoot 整合163邮箱阿里云25端口问题

使用 163 邮箱 SMTP服务器 非SSL协议 25端口,项目本地测试时没有问题, 但是发布到阿里云服务器就报错
经检查发现阿里云出于安全考虑封禁了25端口, 解决方案更换为SSL协议 使用465端口
异常信息:

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163.com, 25; timeout -1;
  nested exception is:
        java.net.ConnectException: 连接超时 (Connection timed out). Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163.com, 25; timeout -1;
  nested exception is:
        java.net.ConnectException: 连接超时 (Connection timed out)
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
        at cn.decentchina.kentucky.merchant.utils.JavaMailMessageUtils.sendMall(JavaMailMessageUtils.java:81)
        at cn.decentchina.kentucky.merchant.utils.JavaMailMessageUtils.sendFileMail(JavaMailMessageUtils.java:46)
        at cn.decentchina.kentucky.merchant.code.exchange.service.impl.CodeCardBatchServiceImpl.getSimpleMessage(CodeCardBatchServiceImpl.java:593)
        at cn.decentchina.kentucky.merchant.code.exchange.service.impl.CodeCardBatchServiceImpl.cardListSend(CodeCardBatchServiceImpl.java:379)
        at cn.decentchina.kentucky.merchant.code.exchange.service.impl.CodeCardBatchServiceImpl$$FastClassBySpringCGLIB$$56a01fec.invoke()
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
        at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:685)
        at cn.decentchina.kentucky.merchant.code.exchange.service.impl.CodeCardBatchServiceImpl$$EnhancerBySpringCGLIB$$1b17249b.cardListSend()
        at cn.decentchina.kentucky.merchant.code.exchange.service.impl.CodeCardBatchServiceImpl$$FastClassBySpringCGLIB$$56a01fec.invoke()
        at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
        at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
        at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

pom.xml

      
        
            org.springframework.boot
            spring-boot-starter-mail
        

application.yml

spring:      
  mail:
    host: smtp.163.com
    password: 授权码(163邮箱非登录密码)
    username:用户名
    default-encoding: UTF-8
    protocol: smtp
    port: 465
    properties:
      mail:
        smtp:
          auth: true
          socketFactory:
            class: javax.net.ssl.SSLSocketFactory
            port: 465
          ssl:
            enable: true
          starttls:
            enable: true
            required: true

java

@Component
public class JavaMailMessageUtils {
    /**
     * 邮件主题最大长度
     */
    private static final Integer MAX_LENGTH = 270;
    @Resource
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String userName;

    /**
     * 发送邮件
     *
     * @param to      接收方
     * @param subject 主题
     * @param text    信息
     * @param file    文件
     */
    public void sendFileMail(String to, String subject, String text, File file) throws MessagingException {
        // 如果主题长度超过最大值,显示...
        if (StringUtils.isNotBlank(subject) && subject.length() > MAX_LENGTH) {
            subject = subject.substring(0, MAX_LENGTH - 3) + "...";
        }
        sendMall(new String[]{to}, subject, text, file);
    }

    /**
     * 发送邮件
     *
     * @param to      接收方
     * @param subject 主题
     * @param text    信息
     * @param file    文件
     */
    public void sendFileMail(String[] to, String subject, String text, File file) throws MessagingException {
        // 如果主题长度超过最大值,显示...
        if (StringUtils.isNotBlank(subject) && subject.length() > MAX_LENGTH) {
            subject = subject.substring(0, MAX_LENGTH - 3) + "...";
        }
        sendMall(to, subject, text, file);
    }

    private void sendMall(String[] toArr, String subject, String text, File file) throws MessagingException {
        System.setProperty("mail.mime.splitlongparameters", "false");
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        if (ArrayUtils.getLength(toArr) == 1) {
            mimeMessageHelper.setTo(toArr[0]);
        } else {
            mimeMessageHelper.setTo(toArr);
        }
        mimeMessageHelper.setFrom(userName);
        mimeMessage.setSubject(subject);
        mimeMessageHelper.setText(text, true);
        if (Objects.nonNull(file)) {
            FileSystemResource attachFile = new FileSystemResource(file);
            mimeMessageHelper.addAttachment(file.getName(), attachFile);
        }
        javaMailSender.send(mimeMessage);
    }
}

你可能感兴趣的:(问题整理,smtp,javamail,java)