SpringCloud配置EMail服务器启动启动不了

技术:

SpringCloud Nacos

结论:

  1. 如果使用@Autowired的时候如果是红的并且你是使用了Nacos是正常的,因为刚开始是找不到Nacos的配置的,如果是本地(即application.yml)配置的话那么也不用担心接着往后看
    @Autowired
    JavaMailSenderImpl javaMailSender;
  1. 注意application.yml或者Nacos上面的配置的问题得配置的正确才可以

完整案例:

Maven


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

application.yml

spring:
  servlet:
    multipart:
      enabled: true #是否启用http上传处理
      max-request-size: 100MB #最大请求文件的大小
      max-file-size: 20MB #设置单个文件最大长度
  #      file-size-threshold: 20MB #当文件达到多少时进行磁盘写入
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/network_disk?useUnicode=true&useSSL=false&characterEncoding=utf8&allowPublicKeyRetrieval=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        group: DEFAULT_GROUP
        namespace: 8c953a50-00c5-4988-8c71-d04f76c13509
   #邮箱基本配置
  mail:
    #配置smtp服务主机地址
    # qq邮箱为smtp.qq.com          端口号465或587
    # sina    smtp.sina.cn
    # aliyun  smtp.aliyun.com
    # 163     smtp.163.com       端口号465或994
    nickname: 囧站
    host: smtp.qq.com
    #发送者邮箱
    username: [email protected]
    #配置密码,注意不是真正的密码,而是刚刚申请到的授权码
    password: fjdsfjdosfjodsfids
    #端口号465或587
    port: 587
    #默认的邮件编码为UTF-8
    default-encoding: UTF-8
    #其他参数
    properties:
      mail:
        #配置SSL 加密工厂
        smtp:
          ssl:
            #本地测试,先放开ssl
            enable: false
            required: false
            #开启debug模式,这样邮件发送过程的日志会在控制台打印出来,方便排查错误
        debug: true
  main:
    lazy-initialization: true

java


@Service
@Log
public class SendEmailServiceImp implements SendEmailService{

    @Autowired
    JavaMailSenderImpl javaMailSender;

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

    @Value("${spring.mail.nickname}")
    private String myName;

    @Override
    public boolean sendVerificationCode(EmailBody emailBody) {
        try {
            //true 代表支持复杂的类型
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(),true);
            //邮件发信人
            mimeMessageHelper.setFrom(myName+"<"+sendEmailAccount+">");
            //邮件收信人  1或多个
            mimeMessageHelper.setTo(emailBody.getEmail());
            //邮件主题
            mimeMessageHelper.setSubject("囧站快递点验证码:");
            //邮件内容
            String text =
                    "\n" +
                            "\n" +
                            "\n" +
                            "    \n" +
                            "    \n" +
                            "    \n" +
                            "    获取验证码:\n" +
                            "\n" +
                            "" +
                            " 

账号为:"+ emailBody.getEmail() + "

"
+ "

本次验证码为: "+ emailBody.getVerification()+ "

"
+ "\n" + ""; mimeMessageHelper.setText(text,true); //邮件发送时间 mimeMessageHelper.setSentDate(new Date()); //发送邮件 javaMailSender.send(mimeMessageHelper.getMimeMessage()); log.info("发送邮件成功:"+sendEmailAccount+"->"+ emailBody.getEmail()); } catch (MessagingException e) { e.printStackTrace(); log.info("发送邮件失败:"+e.getMessage()); return false; } return true; } }

你可能感兴趣的:(spring,cloud,spring)