springboot 邮箱发送学习心得

2019-02-19  本文已影响0人  轰轰_9ccd

163邮箱容易报错系统认证不过去,所以我听网上的劝解用qq邮箱

首先在POM里载入依赖 

    <groupId>org.springframework.boot

    <artifactId>spring-boot-starter-mail

</dependency>


然后再application.properties里添加

JavaMailSender 邮件发送的配置

spring.mail.host=smtp.qq.com

spring.mail.username=2368271114@qq.com

spring.mail.password=qtozwzjkclmvdjda//此处是pop3/SMTP的授权码

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=false

spring.mail.properties.mail.smtp.starttls.required=false

mail.smtp.port=465


在控制层头部加入

@Autowired

private JavaMailSendermailSender; //自动注入的Bean

@Value("${spring.mail.username}")

private StringSender; //读取配置文件中的参数

然后下面是具体发送简单邮箱的代码

SimpleMailMessage message =new SimpleMailMessage();

try {

message.setFrom(Sender);

    //发送的内容

    message.setText("您的六位数验证码为"+verifyCode);

    message.setSubject("主题:07下载验证码");

    message.setTo(content); //给谁发送邮件

    mailSender.send(message);

    logger.info("简单邮件已经发送。");

    return Result.build(200,"发送成功,请查看您的邮箱并填写验证码");

}catch (Exception e) {

logger.error("发送简单邮件时发生异常!", e);

    return Result.build(400,"发送成功,请查看您的邮箱并填写验证码");

}

上一篇 下一篇

猜你喜欢

热点阅读