我们处理异常通常会写入日志,但我们无法及时知道。如果能够将异常信息发送到邮箱,我们可以在第一时间发现这个异常。除此以外,还可以用来给用户发验证码以及各种离线消息等等。
说明:本Demo是用Springboot + Spring自带的JavaMailSender + QQ邮箱 来发邮件的
JavaMailSender
发纯文本的消息,
还可以发送HTML格式的内容,
而且还可以携带附件,
还支持群发。
一、pom.xml中引入依赖
<!-- mail发邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.properties
SMTP授权码 QQ邮箱开启SMTP方法如何授权
#使用smtp.qq.com的邮件服务器
spring.mail.host=smtp.qq.com
#用户名
spring.mail.username=17***80@qq.com
#密码,SMTP授权码
spring.mail.password=ra*******jfg
Controller
package com.example.springBootdemo.controller;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 发送邮件Demo
* 1、单发、发送普通文本
* 2、单发、发送html格式的内容,并携带附件
* 3、群发给多个人
* @author luolei
* @date 2019年1月30日
*/
@RestController
@RequestMapping("/sendmail")
public class SendMailController{
@Autowired
private JavaMailSender javaMailSender;
/**
* 发送普通文本
* @return
* String
*/
@RequestMapping("/simple")
public String sendMail() {
//创建一个SimpleMailMessage对象
SimpleMailMessage message = new SimpleMailMessage();
//发件人
message.setFrom("17***80@qq.com");
//收件人
message.setTo("17***80@qq.com");
message.setSubject("报警消息");
try{
int a = 1/0;
}catch(Exception e){
//错误详细信息
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
pw.flush();
sw.flush();
message.setText(sw.toString());
javaMailSender.send(message);
return "邮件发送成功";
}
return "邮件发送失败";
}
/**
* 发送html格式的内容,并携带附件
* @return
* @throws MessagingException
* String
*/
@RequestMapping("/enclosure")
public String sendMail2() throws MessagingException {
//创建一个SimpleMailMessage对象
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//需要创建一个MimeMessageHelper对象,相关参数和简单邮件类似
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
//发件人
helper.setFrom("17***80@qq.com");
//收件人
helper.setTo("17***80@qq.com");
helper.setSubject("报警消息");
//将邮件内容设置为html格式
try{
int a = 1/0;
}catch(Exception e){
//错误详细信息
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
pw.flush();
sw.flush();
helper.setText("<html><body><p style='color:red;'>"+ sw.toString() +"</p></body></html>", true);
//定义文件,这是classpach路径下的也就是java.main.resources下的文件load4.gif
ClassPathResource file = new ClassPathResource("static/img/loading/load4.gif");
//添加附件文件, 设置文件名为error.gif
helper.addAttachment("error.gif", file);
javaMailSender.send(mimeMessage);
return "邮件发送成功";
}
return "邮件发送失败";
}
/**
* 群发给多个人
* @return
* @throws MessagingException
* String
*/
@RequestMapping("/group")
public String sendMail3() throws MessagingException {
//用户组
String users[] = {"17***80@qq.com","10***27@qq.com"};
//创建一个SimpleMailMessage对象
SimpleMailMessage message = new SimpleMailMessage();
//发件人
message.setFrom("17***80@qq.com");
//收件人
message.setTo(users); // 群发
try{
int a = 1/0;
}catch(Exception e){
//错误详细信息
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
pw.flush();
sw.flush();
message.setText(sw.toString());
javaMailSender.send(message);
return "邮件群发送成功";
}
return "邮件群发送失败";
}
}
测试
http://localhost:8080/sendmail/simple
http://localhost:8080/sendmail/enclosure
http://localhost:8080/sendmail/group