java给钉钉邮箱发送邮件功能实现
作者:o0麦嘎
文章介绍了如何使用Java给钉钉邮箱发送邮件,包括开通POP和IMAP、引入pom、逻辑处理、直接添加前端传来的MultipartFile、添加多个附件以及给多个邮箱发送邮件等步骤,感兴趣的朋友一起看看吧
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);到此这篇关于java给钉钉邮箱发送邮件的文章就介绍到这了,更多相关java邮箱发送邮件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
