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

java给钉钉邮箱发送邮件

1.开通POP和IMAP

在这里插入图片描述

2.引入pom

<dependency>
	<groupId>javax.mail</groupId>
	<artifactId>mail</artifactId>
	<version>1.4.7</version>
</dependency>

3.逻辑

String host = "smtp.qiye.aliyun.com";
String port = "465";
String username = "xxxxxx@dingtalk.com"; // 钉钉发送者邮箱
String password = "xxxxxx"; // 发送者邮箱账号
String toEmail = "xxxxx@dingtalk.com"; // 钉钉接收者邮箱
String subject = "邮件标题"

try {
	Properties props = new Properties();
	props.setProperty("mail.smtp.host", host);
	props.setProperty("mail.smtp.port", port);
	props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
 	props.setProperty("mail.smtp.socketFactory.fallback", "false");
	// 启用调试
	//props.setProperty("mail.debug", "true");
	props.setProperty("mail.smtp.socketFactory.port", port);
	props.setProperty("mail.smtp.auth", "true");
	// 建立邮件会话
	Session session = Session.getDefaultInstance(props, new Authenticator() {
		@Override
		protected PasswordAuthentication getPasswordAuthentication() {
			return new PasswordAuthentication(username, password);
		}
	});
	// 建立邮件对象
	MimeMessage message = new MimeMessage(session);
	message.setFrom(new InternetAddress(username));
	message.setRecipients(Message.RecipientType.TO, toEmail);
	message.setSubject(subject);
	MimeMultipart multipart = new MimeMultipart();
	BodyPart contentPart = new MimeBodyPart();
	// 邮件正文
	contentPart.setContent(content, "text/html;charset=utf-8");
	 multipart.addBodyPart(contentPart);
	// 附件
	MimeBodyPart attachmentBodyPart = new MimeBodyPart();
	// 读取本地文件,如果是前端传过来的MultipartFile文件,需要将MultipartFile转为file,再通过下面的方式:
	// File file = MultipartFileToFile(multipartFile);
	// DataSource source = new FileDataSource(file);
	// attachmentBodyPart.setDataHandler(new DataHandler(source));
	DataHandler dataHandler = new DataHandler(new FileDataSource("E:\\soft\\test.doc");
	attachmentBodyPart.setDataHandler(dataHandler);
	attachmentBodyPart.setFileName("test.doc");
	multipart.addBodyPart(attachmentBodyPart);
	// 设置邮件整体内容
	message.setContent(multipart);
	Transport.send(message);
} catch (Exception e) {
	e.printStackTrace();
}

4.直接添加前端传过来的MultipartFile

.....
MimeMultipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
// 邮件正文
contentPart.setContent(content, "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
// 附件--这里改下
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
File file = MultipartFileToFile(multipartFile);
DataSource source = new FileDataSource(file);
attachmentBodyPart.setDataHandler(new DataHandler(source));
attachmentBodyPart.setFileName(file.getName());
multipart.addBodyPart(attachmentBodyPart);
// 设置邮件整体内容
message.setContent(multipart);
Transport.send(message);
.....

5.添加多个附件

MimeMultipart multipart = new MimeMultipart();
BodyPart contentPart = new MimeBodyPart();
// 邮件正文
contentPart.setContent(content, "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
// 附件--这里改下
for (MultipartFile multipartFile : files){
	MimeBodyPart attachmentBodyPart = new MimeBodyPart();
	File file = MultipartFileToFile(multipartFile);
	DataSource source = new FileDataSource(file);
	//添加附件的内容
	attachmentBodyPart.setDataHandler(new DataHandler(source));
	//添加附件的标题
	attachmentBodyPart.setFileName(file.getName());
	multipart.addBodyPart(filePart);
}
// 设置邮件整体内容
message.setContent(multipart);
Transport.send(message);

6.给多个邮箱发邮件

String tos = "a1@dingtalk.com,a2@dingtalk.com,a3@dingtalk.com";
String[] toList = to.split(",");
Address[] addresses = new Address[toList.length];
for (int i = 0; i < toList.length; i++) {
	addresses[i] = new InternetAddress(toList[i]);
}
message.setRecipients(Message.RecipientType.TO, addresses);

相关文章:

  • stm32仿真 74hc238流水灯 数码管动态数字显示
  • 快速入门——前端数据模拟MockJS
  • java后端开发day19--学生管理系统升级
  • TypeError: the JSON object must be str, bytes or bytearray, not dict
  • LLM全栈框架完整分类清单(预训练+微调+工具链)
  • VMware中的linux常用指令
  • STM32 缺一不可的最基础的初始化部分
  • CSS—引入方式、选择器、复合选择器、文字控制属性、CSS特性
  • smolagents学习笔记系列(六)Secure code execution
  • Redis 面试题
  • RT-Thread+STM32L475VET6——TF 卡文件系统
  • 创建型模式 - 原型模式 (Prototype Pattern)
  • 【Leetcode】两数之和
  • 【Blender】三、材质篇--01,Blender材质基础 原理化BSDF
  • Go红队开发—基础语法入门
  • 如何在 Ubuntu 上安装和使用 Podman ?
  • 【STL】4.<list>
  • kotlin 知识点 七 泛型的高级特性
  • Java 入门第一课 InteliJ IDEA 的快捷操作
  • 阿里云可观测全面拥抱 OpenTelemetry 社区
  • 浙江一家长称小学老师打孩子还威胁要从3楼扔下,当地警方已立案
  • 国防部:赖清德歪曲二战历史,背叛民族令人不齿
  • 占地57亩的“潮汕豪宅”面临强制拆除:曾被实施没收,8年间举行5次听证会
  • 中国科学院院士、我国航天液体火箭技术专家朱森元逝世
  • MSCI中国指数5月调整:新增5只A股、1只港股
  • 日本前卫艺术先驱群展上海:当具体派相遇古树古宅