下面的案例继续延续Spark监控中的邮件监控,在监控中检测到数据异常,需要发送邮件告警
发送邮件工具类
public class MsgUtils { public static void send(String recivers, String title, String content) throws Exception {
Properties properties = new Properties(); properties.setProperty("mail.host","smtp.qq.com"); properties.setProperty("mail.transport.protocol","smtp"); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.smtp.ssl.enable","false");
MailSSLSocketFactory factory = new MailSSLSocketFactory(); factory.setTrustAllHosts(true); properties.put("mail.smtp.ssl.socketFactory", factory);
Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String username = "发送者qq邮箱"; String password = "发送者qq授权码"; return new PasswordAuthentication(username, password); } }; Session session = Session.getInstance(properties, authenticator);
MimeMessage message = new MimeMessage(session); InternetAddress from = new InternetAddress("发送者qq邮箱"); message.setFrom(from); InternetAddress[] tos = InternetAddress.parse(recivers); message.setRecipients(Message.RecipientType.TO, tos); message.setSubject(title); message.setContent(content, "text/html;charset=UTF-8");
Transport.send(message); }
public static void main(String[] args) throws Exception{ send("接收邮箱", "测试", "测试内容"); } }
|
Spark监控中调用
MsgUtils.send("接收者邮箱,接收者邮箱", "ERROR:数据异常", s"jobID: $jobId 数据异常,请马上检查: ${listener.toString}s")
|