springcloud 整合email的坑(Unrecognized SSL message, plaintext connection?)


springcloud整合email真的是搞得我脑壳痛,因为我需要注册的时候通过rabbitmq给用户发一封邮件,因为这个报错的原因导致我mq一直监听失败然后就开始了循环发消息,这就导致邮箱一直在不停的发。

话不多说直接给解决方案。

需要的依赖


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client



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

email服务邮箱的yml

其中 protocol: smtp的配置查了好多博客,一些说使用465需要smtps,经过我的测试好像都可以没什么区别

spring:
  #  邮箱配置
  mail:
    # 配置 SMTP 服务器地址
    host: smtp.qq.com
    # 发送者邮箱
    username: xxxxxxx@qq.com
    # 配置密码,注意这不是真正的密码,而是刚刚申请到的16位授权码
    password: nxarhendrqmlbddd
    # 端口号465或587
    port: 465
    # 默认的邮件编码为UTF-8
    default-encoding: UTF-8
    #这里完全可以直接用smtp
    protocol: smtp
    test-connection: false
    properties:
      mail:
        debug: true
        smtp:
          ssl:
            enable: true
          auth: true
          socketFactory:
#这个可要可不要
#            class: javax.net.ssl.SSLSocketFactory
            port: 465
          starttls:
            enable: true
            required: true

 邮件发送

import com.cn.me.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Dshzs月
 * @version 1.0.0
 * @ClassName SendEmailController.java
 * @Description TODO
 * @createTime 2022年03月24日 10:36:00
 */
@RestController
public class SendEmailController {
    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String name;

    // 上传文件存储目录
    @RequestMapping("/email")
    public void email(@RequestParam("email") String email,@RequestParam("title") String title,@RequestParam("content") String content) {
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject(title);
        // 设置邮件发送者,这个跟application.yml中设置的要一致
        message.setFrom(name);
        // 设置邮件接收者,可以有多个接收者,中间用逗号隔开,以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        message.setTo(email);
        // 设置邮件抄送人,可以有多个抄送人
        //message.setCc("12****32*qq.com");
        // 设置隐秘抄送人,可以有多个
        //message.setBcc("7******9@qq.com");
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText(content);
        // 发送邮件
        javaMailSender.send(message);
    }
}

 应为我是通过feign调用那么也贴一些feign

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Dshzs月
 * @version 1.0.0
 * @ClassName SendEmailFeignClient.java
 * @Description TODO
 * @createTime 2022年03月25日 14:26:00
 */
@FeignClient(value = "email-server")
public interface SendEmailFeignClient {
    @RequestMapping(value = "email", consumes = "application/json")
    void email(@RequestParam("visitoremail") String email, @RequestParam("title") String title, @RequestParam("content") String content);

}

消费端调用

import com.cn.me.feign.SendEmailFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Dshzs月
 * @version 1.0.0
 * @ClassName Tool.java
 * @Description TODO
 * @createTime 2022年03月25日 14:25:00
 */
@Component
public class Tool {
    @Autowired
    private SendEmailFeignClient sendEmailFeignClient;

    /**
     * 发送邮件的方法
     *
     * @param email
     */
    public void sendEmails(String username, String email) {
        //定义标题
        String title = "欢迎加入我们!";
        //定义内容
        String content = "您好,您在婚纱殿的账户激活成功,您的登录用户名是: " +username+
                ",谢谢您的青睐 0^0!";
        //通过sendEmailController调用email方法
        sendEmailFeignClient.email(email, title, content);
    }

mq就不贴了。

总结:导致(Unrecognized SSL message, plaintext connection?)的元凶就是ssl需要开启,协议http和https是不同的,然后端口不能用587

相关