Java邮件通知
Java邮件通知
添加依赖包:
<!--mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
配置文件信息:
spring.mail.host=smtp.qq.com spring.mail.port= 465 spring.mail.username=xxxxxxxx@qq.com spring.mail.password=xxxxxxxxxx spring.mail.default-encoding= UTF-8 spring.mail.properties.mail.smtp.socketFactory.class= javax.net.ssl.SSLSocketFactory spring.mail.properties.mail.debug= true
java代码:
@GetMapping("/email")
@ApiOperation(value = "email验证码通知")
public void email(){
//生成6位随机数字
final String SYMBOLS = "0123456789";
final Random RANDOM = new SecureRandom();
char[] nonceChars = new char[6];
for (int index = 0; index < nonceChars.length; ++index) {
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
}
String vercode = new String(nonceChars);
try {
//发送验证码
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("验证码");//设置邮件标题
message.setText("尊敬的用户,您好:\n"
+ "\n本次请求的邮件验证码为:" + vercode + ",请及时输入。(请勿泄露此验证码)\n"
+ "\n如非本人操作,请忽略该邮件。\n(这是一封自动发送的邮件,请不要直接回复)"); //设置邮件正文
message.setTo("2724967259@qq.com");//设置收件人,测试
message.setFrom("2724967259@qq.com");//设置发件人,测试
mailSender.send(message);//发送邮件
} catch (MailSendException e) {
handleExeption(e);
}
}
备注:
将邮件通知写成方法加到格式化异常信息中,实现邮件通知;

浙公网安备 33010602011771号