首页 > 其他分享 >11. azkaban将调度结果发送到邮箱

11. azkaban将调度结果发送到邮箱

时间:2022-08-15 18:59:33浏览次数:48  
标签:11 发送到 InternetAddress azkaban 发件人 new mail com public

修改配置文件

[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
{
[email protected]
mail.host=smtp.qq.com
[email protected]
mail.password=QHFORNQJMAGBCISU
}
或者
{
[email protected]
mail.host=smtp.163.com
[email protected]
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 = "[email protected]";  //发件人邮箱
    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 <[email protected]>
        //InternetAddress from = new InternetAddress(sender_username);
        //设置发件人第二种方式:发件人信息拼接显示:蚂蚁小哥 <[email protected]>
        String formName = MimeUtility.encodeWord("支鹏") + " <" + fromEmail + ">";
        InternetAddress from = new InternetAddress(formName);
        message.setFrom(from);

        //设置收件人:
        InternetAddress to = new InternetAddress("[email protected]");
        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);// 发送
    }
}

标签:11,发送到,InternetAddress,azkaban,发件人,new,mail,com,public
From: https://www.cnblogs.com/jsqup/p/16587876.html

相关文章