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

 更新时间:2020年09月21日 11:11:46   作者:手撕高达的村长  
这篇文章主要介绍了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

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

相关文章

  • springboot调用webservice-soap接口的实现

    springboot调用webservice-soap接口的实现

    接口协议目前广泛使用的有http协议和RPC协议和webservice,本文主要介绍了springboot调用webservice-soap接口的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • Spring JDBCTemplate原理及使用实例

    Spring JDBCTemplate原理及使用实例

    这篇文章主要介绍了Spring JDBCTemplate原理及使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • java创建jar包并被项目引用步骤详解

    java创建jar包并被项目引用步骤详解

    这篇文章主要介绍了java创建jar包并被项目引用步骤详解,jar包实现了特定功能的,java字节码文件的压缩包,更多相关内容需要的朋友可以参考一下
    2022-07-07
  • Spring AOP的几种实现方式总结

    Spring AOP的几种实现方式总结

    本篇文章主要介绍了Spring AOP的几种实现方式总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • java实战之桌球小游戏

    java实战之桌球小游戏

    这篇文章主要为大家详细介绍了java实战之桌球小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Java实现读取文件夹下(包括子目录)所有文件的文件名

    Java实现读取文件夹下(包括子目录)所有文件的文件名

    这篇文章主要介绍了Java实现读取文件夹下(包括子目录)所有文件的文件名,本文把代码组织成了一个模块,可以很方便的使用,需要的朋友可以参考下
    2015-06-06
  • Java中DecimalFormat用法及符号含义

    Java中DecimalFormat用法及符号含义

    DecimalFormat是NumberFormat的一个具体子类,用于格式化十进制数字。这篇文章介绍了DecimalFormat的用法及符号含义,需要的朋友可以收藏下,方便下次浏览观看
    2021-12-12
  • 详解Spring Boot中如何自定义SpringMVC配置

    详解Spring Boot中如何自定义SpringMVC配置

    这篇文章主要给大家介绍了关于Spring Boot中如何自定义SpringMVC配置的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • 教你java面试时如何聊单例模式

    教你java面试时如何聊单例模式

    这篇文章主要给大家介绍了关于Java单例模式推荐的几种模式,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2021-06-06
  • maven项目在实践中的构建管理之路的方法

    maven项目在实践中的构建管理之路的方法

    这篇文章主要介绍了maven项目在实践中的构建管理之路的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05

最新评论