我们处理异常通常会写入日志,但我们无法及时知道。如果能够将异常信息发送到邮箱,我们可以在第一时间发现这个异常。除此以外,还可以用来给用户发验证码以及各种离线消息等等。
说明:本Demo是用Springboot + Spring自带的JavaMailSender + QQ邮箱 来发邮件的
JavaMailSender
发纯文本的消息,
还可以发送HTML格式的内容,
而且还可以携带附件,
还支持群发。
一、pom.xml中引入依赖
复制收展XML<!-- mail发邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
application.properties
QQ邮箱使用的协议有:电子邮件读取协议(POP3和IMAP)、简单邮件传输协议(SMTP)
SMTP授权码 QQ邮箱开启SMTP方法如何授权
复制收展Java#使用smtp.qq.com的邮件服务器
spring.mail.host=smtp.qq.com
#用户名
spring.mail.username=17***80@qq.com
#密码,SMTP授权码
spring.mail.password=ra*******jfg
- 1
- 2
- 3
- 4
- 5
- 6
Controller
复制收展Javapackage 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 "邮件群发送失败";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
测试
http://localhost:8080/sendmail/simple
http://localhost:8080/sendmail/enclosure
我的CSDN https://blog.csdn.net/csdnluolei/article/details/86701847