编程:java 发送email程序:通用版本:借鉴“蚂蚁小哥 <antladdie@163.com>”的文章

 

  1 package com.alibaba.otter.canal.adapter.launcher.loader;
  2 
  3 import java.text.SimpleDateFormat;
  4 import java.util.*;
  5 import javax.mail.*;
  6 import javax.mail.internet.*;
  7 
  8 public class JavaxJavaMailClient {
  9 
 10     private static SimpleDateFormat isodate = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS");
 11 
 12     public static Session session = null;
 13     public static long last_mail_timestamp = 0;
 14     public String toEmail = "a@b.c"; // 收件人邮箱
 15     public String[] toEmailCC = new String[] {};
 16     public String subject = "Canal 问题报警 测试"; // 主题信息
 17 
 18     public void sendMail(String content) {
 19 
 20         if ((System.currentTimeMillis() - last_mail_timestamp) < 300 * 1000)
 21             return;
 22 
 23         System.out.println("######################################################");
 24         System.out.println("will send alert mail ...");
 25         System.out.println("######################################################");
 26 
 27         if (content == null)
 28             content = "[空内容]";
 29 
 30         try {
 31             if (session == null) {
 32                 // 初始化默认参数
 33                 Properties props = new Properties();
 34                 props.setProperty("mail.transport.protocol", "smtp"); // 邮件发送的协议
 35                 props.setProperty("mail.host", "smtp.exmail.qq.com");// 发送邮件的服务器
 36                 props.setProperty("a@b.c");// 发件人名称
 37                 props.setProperty("a@b.c");// 发件人名称
 38                 props.setProperty("mail.smtp.auth", "true");
 39                 props.setProperty("mail.smtp.ssl.enable", "true");
 40                 props.setProperty("mail.smtp.starttls.enable", "true");
 41                 props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
 42                 props.setProperty("mail.smtp.socketFactory.fallback", "false");
 43                 props.setProperty("mail.smtp.socketFactory.port", "465");
 44 
 45                 try {
 46                     // 获取Session对象
 47                     session = Session.getInstance(props, null);
 48                     // 开启后有调试信息
 49                     // session.setDebug(true);
 50                 } catch (Exception e) {
 51                     e.printStackTrace();
 52                 }
 53             }
 54 
 55             // 通过MimeMessage来创建Message接口的子类
 56             MimeMessage message = new MimeMessage(session);
 57             // 设置发件人第一种方式:直接显示:antladdie <antladdie@163.com>
 58             // InternetAddress from = new InternetAddress(sender_username);
 59             // 设置发件人第二种方式:发件人信息拼接显示:蚂蚁小哥 <antladdie@163.com>
 60             // String formName = MimeUtility.encodeWord("蚂蚁小哥") + " <" + fromEmail + ">";
 61             // InternetAddress from = new InternetAddress(formName);
 62 
 63             message.setFrom(new InternetAddress("a@b.c"));
 64 
 65             // 设置收件人:
 66             InternetAddress to = new InternetAddress(toEmail);
 67             message.setRecipient(Message.RecipientType.TO, to);
 68 
 69             // 设置抄送人(两个)可有可无:
 70             if (toEmailCC.length > 0) {
 71                 ArrayList<InternetAddress> addresses = new ArrayList<InternetAddress>();
 72                 for (String cc : toEmailCC) {
 73                     addresses.add(new InternetAddress(cc));
 74                 }
 75                 InternetAddress[] addressesArr = (InternetAddress[]) addresses.toArray();
 76                 message.setRecipients(Message.RecipientType.CC, addressesArr);
 77             }
 78 
 79             // 设置密送人 可有可无密送人:
 80             // InternetAddress toBCC = new InternetAddress(toEmail);
 81             // message.setRecipient(Message.RecipientType.BCC, toBCC);
 82 
 83             // 设置邮件主题
 84             message.setSubject(subject + "@" + isodate.format(new Date()));
 85 
 86             // 设置邮件内容,这里我使用html格式,其实也可以使用纯文本;纯文本"text/plain"
 87             message.setContent("<h1>你好:<h1> <h3> canal 有问题,错误信息为:<pre>" + content + "</pre></h3>", "text/html;charset=UTF-8");
 88 
 89             // 保存上面设置的邮件内容
 90             message.saveChanges();
 91 
 92             // 获取Transport对象
 93             Transport transport = session.getTransport();
 94             // smtp验证,就是你用来发邮件的邮箱用户名密码(若在之前的properties中指定默认值,这里可以不用再次设置)
 95             // transport.connect(null, null, authCode);
 96             transport.connect("a@b.c", "password");
 97             // 发送邮件
 98             transport.sendMessage(message, message.getAllRecipients()); // 发送
 99 
100             last_mail_timestamp = System.currentTimeMillis();
101 
102         } catch (Exception e) {
103             e.printStackTrace();
104             session = null;
105         }
106     }
107 
108 //    public static void main(String[] args) throws Exception {
109 //
110 //        System.out.println("program begin ...");
111 //        new JavaxJavaMailClient().sendMail("just test");
112 //        System.out.println("program exit.");
113 //
114 //    }
115 
116 }

 

posted on 2024-05-08 21:50  jinzhenshui  阅读(8)  评论(0编辑  收藏  举报