目錄
- 電子郵件
- 傳輸協議
- 郵件接收協議:POP3協議
- 郵件發送協議:SMTP協議
- 概述
- 文本郵件
- 創建Java工程,匯入jar
- 開啟POP3/SMTP服務?
- 復雜郵件
- 注冊發送郵件
- 工具類:多執行緒實作用戶體驗
- 發送郵件
- springboot實作發送郵件
- pom.xml添加依賴
- 組態檔
- 發送郵件
電子郵件

傳輸協議
郵件接收協議:POP3協議
郵件發送協議:SMTP協議

概述



文本郵件
創建Java工程,匯入jar

開啟POP3/SMTP服務?

package com.qing;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.GeneralSecurityException;
import java.util.Properties;
/**
* 簡單郵件:沒有附件和圖片,純文本郵件
*/
public class MailDemo01 {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
// 要發送郵件,需要獲得協議和支持,即開啟POP3/SMTP服務
// QQ郵箱開啟POP3/SMTP服務,授權碼:授權碼
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com"); // 設定郵件服務器
prop.setProperty("mail.transport.protocol","smtp"); // 設定郵件發送協議
prop.setProperty("mail.smtp.auth","true"); // 需要驗證用戶名密碼
// 關于QQ郵箱,還需要設定SSL加密,需要加上以下代碼,其他郵箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true");
prop.put("mail.smtp.ssl.socketFactory",sf);
// 使用JavaMail發送郵件的5個步驟
// 1.創建定義整個應用程式所需的環境資訊的Session物件
// QQ郵箱才需要,其他郵箱不需要
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 發件人郵件用戶名、授權碼
return new PasswordAuthentication("[email protected]", "授權碼");
}
});
// FGRXSBNPAPTGYDVT
// 開啟session的debug模式,就可以查看程式發送email的運行狀態
session.setDebug(true);
// 2.通過Session物件得到transport物件
Transport ts = session.getTransport();
// 3.使用郵箱的用戶名和授權碼臉上郵件服務器
ts.connect("smtp.qq.com","[email protected]","授權碼");
// 4.創建郵件
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));// 發件人
// TO表示主要接收人,CC表示抄送人,BCC表示秘密抄送人
msg.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]")); // 收件人
msg.setSubject("我是主題"); // 郵件主題
msg.setContent("我是內容","text/html;charset=UTF-8"); // 郵件內容
// 5.發送郵件
ts.sendMessage(msg,msg.getAllRecipients());
// 6.關閉連接
ts.close();
}
}
復雜郵件


package com.qing;
import com.sun.mail.util.MailSSLSocketFactory;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.security.GeneralSecurityException;
import java.util.Properties;
/**
* 復雜郵件:帶附件和圖片的郵件
*/
public class MailDemo02 {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
// 要發送郵件,需要獲得協議和支持,即開啟POP3/SMTP服務
// QQ郵箱開啟POP3/SMTP服務,授權碼:授權碼
Properties prop = new Properties();
prop.setProperty("mail.host","smtp.qq.com"); // 設定郵件服務器
prop.setProperty("mail.transport.protocol","smtp"); // 設定郵件發送協議
prop.setProperty("mail.smtp.auth","true"); // 需要驗證用戶名密碼
// 關于QQ郵箱,還需要設定SSL加密,需要加上以下代碼,其他郵箱不需要
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
prop.put("mail.smtp.ssl.enable","true");
prop.put("mail.smtp.ssl.socketFactory",sf);
// 使用JavaMail發送郵件的5個步驟
// 1.創建定義整個應用程式所需的環境資訊的Session物件
// QQ郵箱才需要,其他郵箱不需要
Session session = Session.getDefaultInstance(prop, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 發件人郵件用戶名、授權碼
return new PasswordAuthentication("[email protected]", "授權碼");
}
});
// FGRXSBNPAPTGYDVT
// 開啟session的debug模式,就可以查看程式發送email的運行狀態
session.setDebug(true);
// 2.通過Session物件得到transport物件
Transport ts = session.getTransport();
// 3.使用郵箱的用戶名和授權碼臉上郵件服務器
ts.connect("smtp.qq.com","[email protected]","授權碼");
// 4.創建郵件
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("[email protected]"));// 發件人
// TO表示主要接收人,CC表示抄送人,BCC表示秘密抄送人
msg.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]")); // 收件人
msg.setSubject("我是復雜郵件"); // 郵件主題
// 準備圖片資料
MimeBodyPart image = new MimeBodyPart();
// 圖片需要經過資料處理 DataHandler:資料處理
DataHandler dh = new DataHandler(new FileDataSource("src/1.jpg"));
image.setDataHandler(dh); // 放入處理的圖片資料
image.setContentID("1.jpg"); // 給圖片設定ID,后面正文資料中可以使用cid使用
// 準備正文資料
MimeBodyPart text = new MimeBodyPart();
// 使用cid使用前面的圖片資料
text.setContent("這是帶圖片<img src='https://img.uj5u.com/2021/06/14/244151140611021.jpg'>的郵件","text/html;charset=UTF-8");
// 描述資料關系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
// 設定到訊息中,保存修改
msg.setContent(mm);
msg.saveChanges();
// 5.發送郵件
ts.sendMessage(msg,msg.getAllRecipients());
// 6.關閉連接
ts.close();
}
}


注冊發送郵件

工具類:多執行緒實作用戶體驗



發送郵件

springboot實作發送郵件
pom.xml添加依賴

組態檔

發送郵件


轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/287062.html
標籤:其他
