首先开启发送接收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-8port: 465protocol: smtpproperties:mail:debug:falsesmtp: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("\n\n------------------------\n\n");}return sb.toString();}@GetMapping("/show")public List<AlarmMessage> show(){return lastList;}
}
查看邮箱,就收到了