编程: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 }
分类:
编程
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)