当前位置: 首页 > ops >正文

【JAVA】使用javax.mail发送qq邮件

导入包

下载链接https://repo1.maven.org/maven2/com/sun/mail/javax.mail/

代码

package sample;import javax.imageio.ImageIO;
import javax.mail.*;
import javax.mail.internet.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;public class ScreenshotMailer {private static final String SMTP_HOST = "smtp.qq.com";private static final String SMTP_PORT = "587";private static final String FROM_EMAIL = "234@qq.com";  // 需替换为发件邮箱private static final String PASSWORD = "444ffffff";     // 需替换为邮箱授权码private static final String TO_EMAIL = "234@qq.com";public static void main(String[] args) {Timer timer = new Timer();timer.scheduleAtFixedRate(new TimerTask() {@Overridepublic void run() {try {// 1. 截屏Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());BufferedImage screenCapture = new Robot().createScreenCapture(screenRect);// 2. 保存临时文件File tempFile = File.createTempFile("screenshot_", ".png");ImageIO.write(screenCapture, "png", tempFile);// 3. 发送邮件sendEmailWithAttachment(tempFile);// 4. 删除临时文件tempFile.delete();} catch (Exception e) {e.printStackTrace();}}}, 0, 60 * 1000); // 立即开始,每分钟执行}private static void sendEmailWithAttachment(File attachment) throws Exception {Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.starttls.enable", "true");props.put("mail.smtp.host", SMTP_HOST);props.put("mail.smtp.port", SMTP_PORT);Session session = Session.getInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(FROM_EMAIL, PASSWORD);}});Message message = new MimeMessage(session);message.setFrom(new InternetAddress(FROM_EMAIL));message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(TO_EMAIL));message.setSubject("定时截图 - " + new Date());MimeBodyPart textPart = new MimeBodyPart();textPart.setText("系统自动发送的定时截图");MimeBodyPart attachmentPart = new MimeBodyPart();attachmentPart.attachFile(attachment);Multipart multipart = new MimeMultipart();multipart.addBodyPart(textPart);multipart.addBodyPart(attachmentPart);message.setContent(multipart);Transport.send(message);}
}
http://www.xdnf.cn/news/7371.html

相关文章:

  • Notepad++ 学习(三)使用python插件编写脚本:实现跳转指定标签页(自主研发)
  • [Linux排查] “Permission Denied“?一文解决Linux文件/目录权限不足问题
  • 头歌之软件工程-用例设计
  • 《中国智算中心市场发展研究报告》:算力政策与AI技术双重驱动下的供需两旺
  • 2025年即时通讯APP如何抵御DDoS攻击?全链路防御方案与实战解析
  • 10、底层注解-@Conditional条件装配
  • 绿幕抠图与虚拟背景:直播美颜SDK开发前沿技术探索
  • Keil 调试模式下拷贝Flash,RAM中的数据
  • 双周报Vol.72:字段级文档注释支持、视图类型现为值类型,减少内存分配
  • 实战手册--如何针对衡石分析平台的数据集成详解
  • 配电房值守难题终结者:EdgeView智能监控的7×24小时守护
  • ICU库交叉编译
  • 软件设计师关系代数和元组演算(关联、笛卡尔积、除、映射、分段等问题)考点分析——求三连
  • MCP与通讯模式:理论篇
  • 云服务器开发软件操作步骤
  • 微机系统第二章-题目整理
  • 数据治理进阶:精读45页深入浅出数据治理培训教材【附全文阅读】
  • Nginx基础知识
  • 学习BI---QuickBI介绍
  • 【find / -name “xxx“】为什么有时候搜索范围扩大之后,就无法找到
  • 预训练、指令微调与RLHF如何塑造LLM
  • 【AI论文】端到端视觉标记器调优
  • Typora编写Markdown文档(支持Mermaid)
  • 客户体验数据使用的三种视角——旅程视角
  • python-判断闰年( 极其简单的python)
  • ubuntu sh安装包的安装方式
  • 关于摄像头模块的红外截止滤光片
  • Pichome 任意文件读取漏洞复现(CVE-2025-1743)
  • 技术解码 | 玩转视频播放,自适应码流技术
  • Google设置app-ads.txt