11. azkaban将调度结果发送到邮箱
修改配置文件
[root@node1 conf]# pwd
/opt/app/azkaban-3.85.0/web-server/conf
[root@node1 conf]# ls
azkaban.properties azkaban-users.xml global.properties log4j.properties
[root@node1 conf]# vi azkaban.properties
{
mail.sender=1941323335@qq.com
mail.host=smtp.qq.com
mail.user=1941323335@qq.com
mail.password=QHFORNQJMAGBCISU
}
或者
{
mail.sender=jsq951357@163.com
mail.host=smtp.163.com
mail.user=jsq951357@163.com
mail.password=QHFORNQJMAGBCISU
}
java代码
- pom.xml文件
- java代码
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class Test {
public static String emailHost = "smtp.qq.com"; //发送邮件的主机
public static String transportType = "smtp"; //邮件发送的协议
public static String fromUser = "支鹏"; //发件人名称
public static String fromEmail = "xxxx@qq.com"; //发件人邮箱
public static String authCode = "xxxxx"; //发件人邮箱授权码
public static void main(String[] args) throws UnsupportedEncodingException, javax.mail.MessagingException {
//初始化默认参数
Properties props = new Properties();
props.setProperty("mail.transport.protocol", transportType);
props.setProperty("mail.host", emailHost);
props.setProperty("mail.user", fromUser);
props.setProperty("mail.from", fromEmail);
props.setProperty("mail.smtp.auth", "true");
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, authCode);
}
};
//获取Session对象
Session session = Session.getInstance(props, auth);
//开启后有调试信息
session.setDebug(true);
//通过MimeMessage来创建Message接口的子类
MimeMessage message = new MimeMessage(session);
//下面是对邮件的基本设置
//设置发件人:
//设置发件人第一种方式:直接显示:antladdie <antladdie@163.com>
//InternetAddress from = new InternetAddress(sender_username);
//设置发件人第二种方式:发件人信息拼接显示:蚂蚁小哥 <antladdie@163.com>
String formName = MimeUtility.encodeWord("支鹏") + " <" + fromEmail + ">";
InternetAddress from = new InternetAddress(formName);
message.setFrom(from);
//设置收件人:
InternetAddress to = new InternetAddress("1101810845@qq.com");
message.setRecipient(Message.RecipientType.TO, to);
//设置邮件主题
message.setSubject("测试");
//设置邮件内容,这里我使用html格式,其实也可以使用纯文本;纯文本"text/plain"
message.setContent("<h1>测试标题</h1><button>登录</button>", "text/html;charset=UTF-8");
//保存上面设置的邮件内容
message.saveChanges();
//获取Transport对象
Transport transport = session.getTransport();
//发送邮件
transport.send(message);// 发送
}
}
本文来自博客园,作者:jsqup,转载请注明原文链接:https://www.cnblogs.com/jsqup/p/16587876.html