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

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

(0)

相关推荐

  • Spring Boot实现邮件服务(附:常见邮箱的配置)

    前言 发送邮件应该是网站的必备功能之一,什么注册验证,忘记密码或者是给用户发送营销信息.最早期的时候我们会使用JavaMail相关api来写发送邮件的相关代码,后来spring退出了JavaMailSender更加简化了邮件发送的过程,在之后springboot对此进行了封装就有了现在的spring-boot-starter-mail,本文将详细给大家介绍了关于Spring Boot邮件服务的相关内容,下面话不多说了,来一起看看详细的介绍吧 1. pom.xml文件中引入依赖 <dependen

  • springboot实现发送邮件(QQ邮箱为例)

    本文实例为大家分享了springboot实现发送邮件的具体代码,供大家参考,具体内容如下 1.引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> 2.找到qq邮箱,开启smtp服务,这里生成你的密码,复制第三步用 3.password里输

  • SpringBoot中快速实现邮箱发送代码解析

    前言 在许多企业级项目中,需要用到邮件发送的功能,如: 注册用户时需要邮箱发送验证 用户生日时发送邮件通知祝贺 发送邮件给用户等 创建工程导入依赖 Copy <!-- 邮箱发送依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency&g

  • SpringBoot发送邮箱验证码功能

    一.开启QQ邮箱服务 (1)登陆QQ找到设置,点击账户 (2)往下拉,开启POP3/SMTP服务和IMAP/SMTP服务 当开启IMAP/SMTP会有一串密文密码,保存起来后面要用到 二.spring boot配置邮箱服务   在spring boot的配置文件application.yml中添加以下配置 spring: mail: username: 1963342385@qq.com password: yqc...fchj host: smtp.qq.com password是在开启邮箱服

  • SpringBoot使用邮箱发送验证码实现注册功能

    本文为大家分享了SpringBoot使用邮箱发送验证码实现注册功能实例,供大家参考,具体内容如下 这里有两种方式: 使用Apache Common包中开源的email组件,通过实例化HtmlEmail()对象,可通过配置外置字典.或yml等配置文件实现灵活配置: 依赖: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId>

  • springboot实现邮箱验证码功能

    本文实例为大家分享了springboot实现邮箱验证码功能的具体代码,供大家参考,具体内容如下 我这边使用的QQ邮箱 1.首先创建maven项目,配置pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSch

  • SpringBoot 动态配置邮箱发件人过程解析

    前言 现在的消息模块少不了邮件发送.短信发送和手机推送的功能.邮件发送的功能历史最为悠久,也算的上烂大街的功能.一般在配置文件中设置好邮箱地址.账号.密码和发件服务器地址后便不会再去改动.可是有的客户却希望人为指定发件人信息.这个需求并不过分,需要解决两个大问题:如何在容器启动成功后重新修改发送邮件的Bean.如何在服务器重启后,发件人依然是更改后的配置信息.这里记录实现的步骤. 需求分析 一).在未配置邮箱账号时,系统拥有默认的邮箱发件人 二).重新设置邮箱发件人后,需立即生效 三).重启服务

  • 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 or

  • Spring Boot实现qq邮箱验证码注册和登录验证功能

    1.登录注册思路 这是一个使用spring boot做的一个qq邮箱注册和登录的项目. 没写前端页面,使用postman测试.有截图详细. 1.1.思路 注册:通过输入的邮箱发送验证码,检验前端传来的验证码是否和后台生成的一致,若一致,将数据写入数据库,完成注册: 登录:通过输入的邮箱查询密码,然后比较密码是否一致,一致就是登录成功. 1.2.整个项目结构图 2.准备 2.1.开启邮箱POP3/SMTP服务 登录qq邮箱后,点击左上方的设置,选择账户,如下图. 然后一直往下滑,看到如下图的POP

  • Spring boot 使用QQ邮箱进行一个验证登入功能

    目录 Spring boot 使用QQ邮箱进行一个验证登入 QQ邮箱开启权限 创建发送验证码的请求Controller 注册,登录验证 Spring boot 使用QQ邮箱进行一个验证登入 QQ邮箱开启权限 在QQ邮箱设置->账户里面,往下拉找到这个开启,手机号验证成功后会有一串英文字符串是待会儿要用到的密码. prom.xml 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <ar

  • spring boot配置MySQL数据库连接、Hikari连接池和Mybatis的简单配置方法

    此方法为极简配置,支持MySQL数据库多库连接.支持Hikari连接池.支持MyBatis(包括Dao类和xml文件位置的配置). 1.pom.xml中引入依赖: <!-- Begin of DB related --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId>

  • spring boot整合mybatis使用c3p0数据源连接mysql

    刚刚接触springboot,对很多东西都不熟悉,例如,它的注解方式,他的配置方式等:听说它很牛逼,所以就尝试着去学习.在基本熟悉springboot的第一个程序之后.想到当时spring整合mybatis时使用了数据源连接数据库,所以自己也想尝试使用c3p0连接数据库.所以就有了以下的内容: 首先第一步,创建maven项目导入包: pom.xml <parent> <groupId>org.springframework.boot</groupId> <arti

  • spring boot ${}占位符不起作用的解决方案

    spring boot ${}占位符不起作用 问题: 在 pom.xml 文件里定义好属性标签,然后在 properties或者xml 中使用${key}引用,打包之后就会自动替换掉.但是在使用 spring boot 后发现,@可以替换,但是${-}替换不了. 分析: spring boot设置了默认值. 解决: 在pom文件中自己定义delimiters springboot配置文件占位符 1.随机数 #idea配置文件默认是utf-8 person.name=r a n d o m . u

  • 基于Spring Boot的线程池监控问题及解决方案

    目录 前言 为什么需要对线程池进行监控 如何做线程池的监控 数据采集 数据存储以及大盘的展示 进一步扩展以及思考 如何合理配置线程池参数 如何动态调整线程池参数 如何给不同的服务之间做线程池的隔离 实现方案 前言 这篇是推动大家异步编程的思想的线程池的准备篇,要做好监控,让大家使用无后顾之忧,敬畏生产. 为什么需要对线程池进行监控 Java线程池作为最常使用到的并发工具,相信大家都不陌生,但是你真的确定使用对了吗?大名鼎鼎的阿里Java代码规范要求我们不使用 Executors来快速创建线程池,

  • Spring Boot 连接LDAP的方法

    本文是Spring Boot系列文集中关于LDAP连接相关操作的一文.仅仅涉及基本的使用ODM来快速实现LDAP增删改查操作.详细的关于Spring LDAP的其他操作,可以参考翻译的官方文档. 本文目的:使用Spring Boot构建项目,帮助读者快速配置并使用Spring LDAP操作LDAP.大致步骤如下: 1.创建Spring Boot项目(约1分钟) 2.添加pom.xml文件中Spring LDAP依赖(约1分钟) 3.配置Spring LDAP连接信息(约1分钟) 4.创建实体类作

  • spring boot整合mybatis利用Mysql实现主键UUID的方法

    前言 本文主要给大家介绍了关于spring boot整合mybatis利用Mysql实现主键UUID的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 实现 基础项目的pom.xml部分代码如下 <properties> <java.version>1.8</java.version> </properties> <!-- Inherit defaults from Spring Boot --> <parent&

随机推荐