一. 准備工作
1. 傳輸協議
SMTP協議-->發送郵件:
我們通常把處理用戶smtp請求(郵件發送請求)的服務器稱之為SMTP服務器(郵件發送服務器)
POP3協議-->接收郵件:
我們通常把處理用戶pop3請求(郵件接收請求)的服務器稱之為POP3服務器(郵件接收服務器)
2. 郵件收發原理
- 閃電俠網易雲郵箱通過smtp協議連接到Smtp服務器,然后發送一封郵件給網易的郵件服務器
- 網易分析發現需要去QQ的郵件服務器,通過smtp協議將郵件轉投給QQ的Smtp服務器
- QQ將接收到的郵件存儲在 962113063@qq.com這個郵件賬號的空間中
- 閃電俠qq郵箱通過Pop3協議連接到Pop3服務器收取郵件
- 從 962113063@qq.com這個郵件賬號的空間中取出郵件
- Pop3服務器將取出來的郵件送到閃電俠qq郵箱中
3. QQ郵箱中獲取對應的權限
QQ郵箱需要安全驗證,我們需要獲取他對應的權限;
QQ郵箱-->郵箱設置-->賬戶
4. 導入jar包
mail.jar
activation.ja
二. Java發送純文本郵件
編寫測試代碼
public class SendMain {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties prop = new Properties();
//設置QQ郵件服務器
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.txt、創建定義整個應用程序所需的環境信息的Session對象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
//發件人郵件用戶名、授權碼
return new PasswordAuthentication("962113063@qq.com",
"授權碼");
}
});
//開啟Session的debug模式,這樣就可以查看到程序發送Email的運行狀態
session.setDebug(true);
//2、通過session得到transport對象
Transport ts = session.getTransport();
//3、使用郵箱的用戶名和授權碼連上郵件服務器
ts.connect("smtp.qq.com", "962113063@qq.com", "授權碼");
//4,創建郵件
//4-1.txt,創建郵件對象
MimeMessage message = new MimeMessage(session);
//4-2,指明郵件的發件人
message.setFrom(new InternetAddress("962113063@qq.com"));
//4-3,指明郵件的收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("962113063@qq.com"));
//4-4,郵件標題
message.setSubject("Hello");
//4-5,郵件文本內容
message.setContent("我是鋼鐵俠!", "text/html;charset=UTF-8");
//4-6,發送郵件
ts.sendMessage(message, message.getAllRecipients());
//5,關閉連接
ts.close();
}
}
三. Java發送創建包含內嵌圖片的郵件
1. 導入圖片
2. 編寫測試代碼
public class SendMainpicture1 {
public static void main(String[] args)throws GeneralSecurityException, MessagingException{
Properties prop = new Properties();
//設置QQ郵件服務器
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.txt、創建定義整個應用程序所需的環境信息的Session對象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
//發件人郵件用戶名、授權碼
return new PasswordAuthentication("962113063@qq.com",
"授權碼");
}
});
//開啟Session的debug模式,這樣就可以查看到程序發送Email的運行狀態
session.setDebug(true);
//2、通過session得到transport對象
Transport ts = session.getTransport();
//3、使用郵箱的用戶名和授權碼連上郵件服務器
ts.connect("smtp.qq.com", "962113063@qq.com", "授權碼");
//4,創建郵件
//4-1.txt,創建郵件對象
MimeMessage message = new MimeMessage(session);
//4-2,指明郵件的發件人
message.setFrom(new InternetAddress("962113063@qq.com"));
//4-3,指明郵件的收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("962113063@qq.com"));
//4-4,郵件標題
message.setSubject("Hello,鋼鐵俠");
//5.准備圖片
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src/resources/9.jpg"));
image.setDataHandler(dh);
image.setContentID("9.jpg");
//6.准備正文數據
MimeBodyPart text = new MimeBodyPart();
text.setContent("這是一封郵件正文帶圖片<img src='cid:9.jpg'>的郵件", "text/html;charset=UTF-8");
//7.描述數據關系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
//設置到消息中,保存修改
message.setContent(mm);
message.saveChanges();
//發送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
}
四. Java發送帶圖片和附件的復雜郵件
1. 導入附件和圖片
2. 編寫測試代碼
public class SendMainpicture2 {
public static void main(String[] args) throws GeneralSecurityException, MessagingException {
Properties prop = new Properties();
//設置QQ郵件服務器
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.txt、創建定義整個應用程序所需的環境信息的Session對象
Session session = Session.getDefaultInstance(prop, new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
//發件人郵件用戶名、授權碼
return new PasswordAuthentication("962113063@qq.com",
"授權碼");
}
});
//可以通過session開啟Dubug模式,查看所有的過程
session.setDebug(true);
//2.獲取連接對象,通過session對象獲得Transport,需要捕獲或者拋出異常;
Transport tp = session.getTransport();
//3.連接服務器,需要拋出異常;
tp.connect("smtp.qq.com","962113063@qq.com","授權碼");
//4.連接上之后我們需要發送郵件;
MimeMessage mimeMessage = imageMail(session);
//5.發送郵件
tp.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
//6.關閉連接
tp.close();
}
public static MimeMessage imageMail(Session session) throws MessagingException {
//消息的固定信息
MimeMessage mimeMessage = new MimeMessage(session);
//郵件發送人
mimeMessage.setFrom(new InternetAddress("962113063@qq.com"));
//郵件接收人,可以同時發送給很多人
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("624794851@qq.com"));
mimeMessage.setSubject("我也不知道是個什么東西就發給你了"); //郵件主題
//圖片
MimeBodyPart body1 = new MimeBodyPart();
body1.setDataHandler(new DataHandler(new FileDataSource("src/resources/9.jpg")));
body1.setContentID("9.jpg"); //圖片設置ID
//文本
MimeBodyPart body2 = new MimeBodyPart();
body2.setContent("請注意,我不是廣告<img src='cid:9.jpg'>","text/html;charset=utf-8");
//附件
MimeBodyPart body3 = new MimeBodyPart();
body3.setDataHandler(new DataHandler(new FileDataSource("src/resources/log4j.properties")));
body3.setFileName("log4j.properties"); //附件設置名字
MimeBodyPart body4 = new MimeBodyPart();
body4.setDataHandler(new DataHandler(new FileDataSource("src/resources/1.txt")));
body4.setFileName("1.txt"); //附件設置名字
//拼裝郵件正文內容
MimeMultipart multipart1 = new MimeMultipart();
multipart1.addBodyPart(body1);
multipart1.addBodyPart(body2);
multipart1.setSubType("related"); //1.txt.文本和圖片內嵌成功!
//new MimeBodyPart().setContent(multipart1); //將拼裝好的正文內容設置為主體
MimeBodyPart contentText = new MimeBodyPart();
contentText.setContent(multipart1);
//拼接附件
MimeMultipart allFile =new MimeMultipart();
allFile.addBodyPart(body3); //附件
allFile.addBodyPart(body4); //附件
allFile.addBodyPart(contentText);//正文
allFile.setSubType("mixed"); //正文和附件都存在郵件中,所有類型設置為mixed;
//放到Message消息中
mimeMessage.setContent(allFile);
mimeMessage.saveChanges();//保存修改
return mimeMessage;
}
}