Spring Boot Mail QQ企业邮箱无法连接解决方案

这里记录一下QQ企业邮箱发邮件问题,因为之前遇到过一种情况是本地测试没问题,结果线上出现问题

Couldn't connect to host, port: smtp.qq.com, 25; timeout -1

要使用企业邮箱生成的授权密码.

这里只要是因为QQ邮箱默认端口是465,需要修改为SSL配置

java代码

package com.chenpeng.cpeducloud.service.impl;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;
 
import com.chenpeng.cpeducloud.base.WebConstants;
import com.chenpeng.cpeducloud.service.MailService;
import com.chenpeng.cpeducloud.util.Constants;
import com.chenpeng.cpeducloud.util.DateUtils;
import com.chenpeng.cpeducloud.util.StringUtils;
 
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 
/**auth : szy
 *time : 2019-05-16
 **/
@Service
@Slf4j
public class MailServiceImpl implements MailService {
 
  @Autowired
  private JavaMailSender mailSender;
 
  @Value("${mail.formSender}")
  private String sender;// 发送者
 
  @Value("${mail.formMobile}")
  private String formMobile;// 联系电话
   
  /**
   * 发送简单邮件(收件人,主题,内容)
   */
  @Override
  public void sendSimpleMail(String to, String subject, String content) {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(sender);
    message.setTo(to);
    message.setSubject(subject);
    message.setText(content);
    try {
      mailSender.send(message);
      log.info("简单邮件发送成功!");
    } catch (Exception e) {
      log.info("发送简单邮件时发生异常!"+e);
    }
  }
 
 
  /**
   * 发送Html邮件(收件人,主题,内容)
   */
  @Override
  public void sendHtmlMail(String to, String subject, String content) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = null;  //true表示需要创建一个multipart message
      try {
        helper = new MimeMessageHelper(message, true);
        message.setFrom(sender);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);
        log.info("html邮件发送成功");
      } catch (javax.mail.MessagingException e) {
        e.printStackTrace();
      }
 
    } catch (MessagingException e) {
      log.info("发送html邮件时发生异常!"+e);
    }
  }
 
  /**
   * 发送带附件的邮件
   * @param to
   * @param subject
   * @param content
   * @param filePath
   */
  @Override
  public void sendAttachmentsMail(String to, String subject, String content, String filePath){
    MimeMessage message = mailSender.createMimeMessage();
 
    try {
      MimeMessageHelper helper = null;
      try {
        helper = new MimeMessageHelper(message, true);
        message.setFrom(sender);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
 
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);
        //helper.addAttachment("test"+fileName, file);
 
        mailSender.send(message);
        log.info("带附件的邮件已经发送。");
      } catch (javax.mail.MessagingException e) {
        e.printStackTrace();
      }
 
    } catch (MessagingException e) {
      log.info("发送带附件的邮件时发生异常!"+e);
    }
  }
 
 
  /**
   * 发送Html邮件(收件人,主题,内容),
   * 带多附件
   */
  @Override
  public void sendHtmlMailAndAttachments(String[] to,String[] cc, String subject, String content, List<String> files) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
      MimeMessageHelper helper = null;  //true表示需要创建一个multipart message
      try {
        helper = new MimeMessageHelper(message, true);
        message.setFrom(sender);
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        helper.setText(content, true);
 
        for (String filePath : files){
          FileSystemResource file = new FileSystemResource(new File(filePath));
          String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
          helper.addAttachment(fileName, file);
        }
        mailSender.send(message);
        log.info("html邮件发送成功");
      } catch (javax.mail.MessagingException e) {
        e.printStackTrace();
      }
 
    } catch (MessagingException e) {
      log.info("发送html邮件时发生异常!"+e);
    }
  }
   
}

邮箱配置

#邮箱配置
mail:
 host: smtp.exmail.qq.com
 username: 11111@qq.com
 password: 密钥不是密码
 default-encoding: utf-8
 port: 465
 properties:
  mail:
   smtp:
    auth: true
    ssl:
     enable: true
     socketFactory:
      class: com.sun.mail.util.MailSSLSocketFactory
      fallback: false
    starttls:
        enable: true
        required: true

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。

相关推荐:

Spring常见的十大错误,78%的老程序员都踩过这些坑!

首先我们来看一下,Spring常见错误有那些 太过关注底 内部结构“泄露” 缺乏关注点分离 缺乏异常处理或处理不当 多线程处理不当 不使用基于注解的验证 (依旧)使用基于xml的配置 忽略profile 无法接受依赖项注入 缺乏测试,或测试不当 接下来就一一介绍这些常见的错误1.错误一:太过关注底层...

好程序员Java学习路线分享Spring创建Bean的3种方式

好程序员Java学习路线分享Spring创建Bean的3种方式,本文讲解了在Spring应用中创建Bean的多种方式,包括自动创建,以及手动创建注入方式,实际开发中可以根据业务场景选择合适的方案。方式1:使用SpringXML方式配置,该方式用于在纯Spring应用中,适用于简单的小应用,当应用变得...

追踪JVM中的本地内存

1.概述 有没有想过为什么Java应用程序通过众所周知的-Xms和-Xmx调优标志消耗的内存比指定数量多得多?出于各种原因和可能的优化,JVM可以分配额外的本机内存。这些额外的分配最终会使消耗的内存超出-Xmx限制。 在本教程中,我们将列举JVM中的一些常见内存分配源,以及它们的大小调整标志,然后学...

在用Spring boot搭建项目的时候出现 “无法访问此网站“ 是什么情况

这篇文章主要讲解了“在用Springboot搭建项目的时候出现“无法访问此网站“是什么情况”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“在用Springboot搭建项目的时候出现“无法访问此网站“是什么情况”吧!在用Springboot搭建项目的时候...

SpringBoot启动机制(starter机制)核心原理详解

作者:MyBug 一、前言 使用过springboot的同学应该已经知道,springboot通过默认配置了很多框架的使用方式帮我们大大简化了项目初始搭建以及开发过程。本文的目的就是一步步分析springboot的启动过程,这次主要是分析springboot特性自动装配。那么首先带领大家回顾一下以往...

一道78%的Java程序员搞不清的Spring bean面试题

熟悉Spring开发的朋友都知道Spring提供了5种scope分别是singleton、prototype、request、session、globalsession。如下图是官方文档上的截图,感兴趣的朋友可以进去看看这五种分别有什么不同。今天要介绍的是这五种中的前两种,也是Spring最初提供的...

SpringBoot2.0 基础案例(03):配置系统全局异常映射处理

一、异常分类 这里的异常分类从系统处理异常的角度看,主要分类两类:业务异常和系统异常。 1、业务异常 业务异常主要是一些可预见性异常,处理业务异常,用来提示用户的操作,提高系统的可操作性。常见的业务异常提示:1)请输入xxx2)xxx不能为空3)xxx重复,请更换 2、系统异常 系统异常主要是一些不...