Spring boot发送邮件过程及问题整理
最开始参考的是网上的普遍教程,用的qq邮箱,但是qq邮箱及其不稳定。
有时候发送成功,有时候不能发送成功。
而且今天在运行的时候,突然崩了,我去qq邮箱查看了一下,显示不能找到qq邮箱服务器ip。
果断放弃了qq邮箱,转用了163邮箱,非常nice!!
贴一个今天的报错信息
Error creating bean with name 'org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration':
Bean instantiation via constructor failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [org.springframework.boot.autoconfigure.mail.MailSenderValidatorAutoConfiguration]: Constructor threw exception; nested exception is java.lang.IllegalStateException: Mail server is not available
使用springboot发送邮件
更改邮箱配置
首先更改邮箱配置
开启下面箭头所指地方,它会让你发送一个短信,然后给你一串字符,保存一下这串字符,它是你springboot连接邮箱的登录密码。
Spingboot pom.xml文件添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Springboot .properties文件配置
#邮箱配置
###
#smtp服务主机 qq邮箱则为smtp.qq.com
spring.mail.host=smtp.163.com
#端口
spring.mail.port=465
#服务协议
spring.mail.protocol=smtp
#编码集
spring.mail.default-encoding=UTF-8
#发送邮件的账户
spring.mail.username=x1692760724@163.com
#授权码
spring.mail.password=TXVMCBZOMAYCXADM
spring.mail.test-connection=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.debug=true
Controller层
package com.example.homework.controller;
import com.example.homework.entity.*;
import com.example.homework.service.AdminService;
import com.example.homework.service.StudentService;
import com.example.homework.service.TeacherService;
import com.example.homework.util.VerCodeGenerateUtil;
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.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/common")
public class CommonAjaxController {
@Value("${spring.mail.username}")
private String from;
@Resource
private JavaMailSender mailSender;
@RequestMapping(value = "/sendEmail",method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> commonEmail(String id, String email,Integer role, Model model) {
String userName="小明";
String userEmail="123@qq.com";
// 创建邮件消息
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(userEmail);
message.setSubject("验证码");
String verCode = VerCodeGenerateUtil.generateVerCode();
message.setText("尊敬的"+userName+",您好:\n"
+ "\n本次请求的邮件验证码为:" + verCode + ",本验证码 5 分钟内效,请及时输入。(请勿泄露此验证码)\n"
+ "\n如非本人操作,请忽略该邮件。\n(这是一封通过自动发送的邮件,请不要直接回复)");
mailSender.send(message);
Map<String,Object> mp=new HashMap<>();
mp.put("success",verCode);
mp.put("message","验证码已发送至邮箱,请注意查收!");
return mp;
}
}
生成验证码util代码
package com.example.homework.util;
import java.security.SecureRandom;
import java.util.Random;
// 随机生成6位验证码
public class VerCodeGenerateUtil {
// 验证码包含的字段,可自己设置
private static final String SYMBOLS = "0123456789ABCDEFGHIGKLMNOPQRSTUVWXYZ";
private static final Random RANDOM = new SecureRandom();
// 生成 6 位数的随机数字
public static String generateVerCode() {
// 如果是六位,就生成大小为 6 的数组
char[] numbers = new char[6];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
}
return new String(numbers);
}
}