首先开启发送接收qq邮箱的权限
开启之后,会让你发送信息,按着一系列操作,获得password
(授权码(例如,qq开启SMTP授权码,qq授权码16位))
<!-- mail邮箱-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring:
mail:
host: smtp.qq.com #qq邮箱的不用改
username: 2659934495@qq.com # 发送者的邮箱
password: woemqxxxxxxx #授权码,不是密码
to: 2659934495@qq.com #对方的邮箱
title: 告警邮箱
default-encoding: utf-8
port: 465
protocol: smtp
properties:
mail:
debug:
false
smtp:
socketFactory:
class: javax.net.ssl.SSLSocketFactory
@Getter
@EqualsAndHashCode
@RequiredArgsConstructor
@NoArgsConstructor(force = true)
public class AlarmMessage {
private final String scope;
private final int scopeId;
private final String name;
private final String id0;
private final String id1;
}
@RestController
@RequiredArgsConstructor
public class WebHooks {
private final JavaMailSender sender;
@Value("${spring.mail.username}")
private String from;
@Value("${spring.mail.to}")
private String to;
@Value("${spring.mail.title}")
private String title;
private List<AlarmMessage> lastList=new ArrayList<>();
//邮箱功能
private void sendMail(){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(title);
String content = getContent(lastList);
message.setText(content);
sender.send(message);
System.out.println("发送完成,请查收");
}
@PostMapping("/webhook")
public void webhook(@RequestBody List<AlarmMessage> alarmMessageList){
lastList = alarmMessageList;
//发送邮箱告警
System.out.println("开始发送");
sendMail();
}
private String getContent(List<AlarmMessage> lastList){
StringBuilder sb = new StringBuilder();
for (AlarmMessage alarmMessage:lastList){
sb.append("scope:").append(alarmMessage.getScope())
.append("nscopeId").append(alarmMessage.getScopeId())
.append("nname").append(alarmMessage.getName())
.append("nscopeId").append(alarmMessage.getScopeId())
.append("nid0").append(alarmMessage.getId0())
.append("nid1").append(alarmMessage.getId1())
.append("nn------------------------nn");
}
return sb.toString();
}
@GetMapping("/show")
public List<AlarmMessage> show(){
return lastList;
}
}
原文地址:https://blog.csdn.net/weixin_47617631/article/details/134679309
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_27076.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。