java給釘釘郵箱發(fā)送郵件功能實(shí)現(xiàn)
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"; // 釘釘發(fā)送者郵箱
String password = "xxxxxx"; // 發(fā)送者郵箱賬號(hào)
String toEmail = "xxxxx@dingtalk.com"; // 釘釘接收者郵箱
String subject = "郵件標(biāo)題"
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");
// 啟用調(diào)試
//props.setProperty("mail.debug", "true");
props.setProperty("mail.smtp.socketFactory.port", port);
props.setProperty("mail.smtp.auth", "true");
// 建立郵件會(huì)話
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
// 建立郵件對(duì)象
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轉(zhuǎn)為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);
// 設(shè)置郵件整體內(nèi)容
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); // 設(shè)置郵件整體內(nèi)容 message.setContent(multipart); Transport.send(message); .....
5.添加多個(gè)附件
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);
//添加附件的內(nèi)容
attachmentBodyPart.setDataHandler(new DataHandler(source));
//添加附件的標(biāo)題
attachmentBodyPart.setFileName(file.getName());
multipart.addBodyPart(filePart);
}
// 設(shè)置郵件整體內(nèi)容
message.setContent(multipart);
Transport.send(message);6.給多個(gè)郵箱發(fā)郵件
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);到此這篇關(guān)于java給釘釘郵箱發(fā)送郵件的文章就介紹到這了,更多相關(guān)java郵箱發(fā)送郵件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
intellij idea旗艦版解決學(xué)生無法注冊(cè)問題詳解
這篇文章主要介紹了intellij idea旗艦版解決學(xué)生無法注冊(cè)問題詳解,文中通過圖文示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Mybatis返回類型為Map時(shí)遇到的類型轉(zhuǎn)化的異常問題
這篇文章主要介紹了Mybatis返回類型為Map時(shí)遇到的類型轉(zhuǎn)化的異常問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Java簡單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過程解析
這篇文章主要介紹了Java簡單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Springboot如何使用Map將錯(cuò)誤提示輸出到頁面
這篇文章主要介紹了Springboot如何使用Map將錯(cuò)誤提示輸出到頁面,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
Java8時(shí)間api之LocalDate/LocalDateTime的用法詳解
在項(xiàng)目中,時(shí)間的使用必不可少,而java8之前的時(shí)間api?Date和Calander等在使用上存在著很多問題,于是,jdk1.8引進(jìn)了新的時(shí)間api-LocalDateTime,本文就來講講它的具體使用吧2023-05-05
java多態(tài)性中的Overload和Override區(qū)別詳解
這篇文章主要介紹了java多態(tài)性中的Overload和Override區(qū)別詳解,重寫(Overriding)是父類與子類之間多態(tài)性的一種表現(xiàn),而重載(Overloading)是一個(gè)類中多態(tài)性的一種表現(xiàn),需要的朋友可以參考下2023-07-07

