JavaWeb中JavaMail創(chuàng)建郵件和發(fā)送郵件
一、RFC882文檔簡單說明
RFC882文檔規(guī)定了如何編寫一封簡單的郵件(純文本郵件),一封簡單的郵件包含郵件頭和郵件體兩個部分,郵件頭和郵件體之間使用空行分隔。
郵件頭包含的內(nèi)容有:
from字段 --用于指明發(fā)件人
to字段 --用于指明收件人
subject字段 --用于說明郵件主題
cc字段 -- 抄送,將郵件發(fā)送給收件人的同時抄送給另一個收件人,收件人可以看到郵件抄送給了誰
bcc字段 -- 密送,將郵件發(fā)送給收件人的同時將郵件秘密發(fā)送給另一個收件人,收件人無法看到郵件密送給了誰郵件體指的就是郵件的具體內(nèi)容。
二、MIME協(xié)議簡單介紹
在我們的實際開發(fā)當(dāng)中,一封郵件既可能包含圖片,又可能包含有附件,在這樣的情況下,RFC882文檔規(guī)定的郵件格式就無法滿足要求了。
MIME協(xié)議是對RFC822文檔的升級和補充,它描述了如何生產(chǎn)一封復(fù)雜的郵件。通常我們把MIME協(xié)議描述的郵件稱之為MIME郵件。MIME協(xié)議描述的數(shù)據(jù)稱之為MIME消息。
對于一封復(fù)雜郵件,如果包含了多個不同的數(shù)據(jù),MIME協(xié)議規(guī)定了要使用分隔線對多段數(shù)據(jù)進(jìn)行分隔,并使用Content-Type頭字段對數(shù)據(jù)的類型、以及多個數(shù)據(jù)之間的關(guān)系進(jìn)行描述。
三、使用JavaMail創(chuàng)建郵件和發(fā)送郵件
JavaMail創(chuàng)建的郵件是基于MIME協(xié)議的。因此可以使用JavaMail創(chuàng)建出包含圖片,包含附件的復(fù)雜郵件。
3.1、JavaMail API的簡單介紹



3.2、創(chuàng)建郵件發(fā)送測試項目

3.3、發(fā)送一封只包含文本的簡單郵件
package me.gacl.main;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @ClassName: Sendmail
* @Description: 發(fā)送Email
* @author: 孤傲蒼狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail發(fā)送郵件的5個步驟
//1、創(chuàng)建session
Session session = Session.getInstance(prop);
//開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài)
session.setDebug(true);
//2、通過session得到transport對象
Transport ts = session.getTransport();
//3、使用郵箱的用戶名和密碼連上郵件服務(wù)器,發(fā)送郵件時,發(fā)件人需要提交郵箱的用戶名和密碼給smtp服務(wù)器,用戶名和密碼都通過驗證之后才能夠正常發(fā)送郵件給收件人。
ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
//4、創(chuàng)建郵件
Message message = createSimpleMail(session);
//5、發(fā)送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 創(chuàng)建一封只包含文本的郵件
* @Anthor:孤傲蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//創(chuàng)建郵件對象
MimeMessage message = new MimeMessage(session);
//指明郵件的發(fā)件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//指明郵件的收件人,現(xiàn)在發(fā)件人和收件人是一樣的,那就是自己給自己發(fā)
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
//郵件的標(biāo)題
message.setSubject("只包含文本的簡單郵件");
//郵件的文本內(nèi)容
message.setContent("你好?。?, "text/html;charset=UTF-8");
//返回創(chuàng)建好的郵件對象
return message;
}
}
3.4、發(fā)送包含內(nèi)嵌圖片的郵件
package me.gacl.main;
import java.io.FileOutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* @ClassName: Sendmail
* @Description: 發(fā)送Email
* @author: 孤傲蒼狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail發(fā)送郵件的5個步驟
//1、創(chuàng)建session
Session session = Session.getInstance(prop);
//開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài)
session.setDebug(true);
//2、通過session得到transport對象
Transport ts = session.getTransport();
//3、連上郵件服務(wù)器,需要發(fā)件人提供郵箱的用戶名和密碼進(jìn)行驗證
ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
//4、創(chuàng)建郵件
Message message = createImageMail(session);
//5、發(fā)送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createImageMail
* @Description: 生成一封郵件正文帶圖片的郵件
* @Anthor:孤傲蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createImageMail(Session session) throws Exception {
//創(chuàng)建郵件
MimeMessage message = new MimeMessage(session);
// 設(shè)置郵件的基本信息
//發(fā)件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//郵件標(biāo)題
message.setSubject("帶圖片的郵件");
// 準(zhǔn)備郵件數(shù)據(jù)
// 準(zhǔn)備郵件正文數(shù)據(jù)
MimeBodyPart text = new MimeBodyPart();
text.setContent("這是一封郵件正文帶圖片<img src='cid:xxx.jpg'>的郵件", "text/html;charset=UTF-8");
// 準(zhǔn)備圖片數(shù)據(jù)
MimeBodyPart image = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\1.jpg"));
image.setDataHandler(dh);
image.setContentID("xxx.jpg");
// 描述數(shù)據(jù)關(guān)系
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(text);
mm.addBodyPart(image);
mm.setSubType("related");
message.setContent(mm);
message.saveChanges();
//將創(chuàng)建好的郵件寫入到E盤以文件的形式進(jìn)行保存
message.writeTo(new FileOutputStream("E:\\ImageMail.eml"));
//返回創(chuàng)建好的郵件
return message;
}
}
3.5、發(fā)送包含附件的郵件
package me.gacl.main;
import java.io.FileOutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
* @ClassName: Sendmail
* @Description: 發(fā)送Email
* @author: 孤傲蒼狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail發(fā)送郵件的5個步驟
//1、創(chuàng)建session
Session session = Session.getInstance(prop);
//開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài)
session.setDebug(true);
//2、通過session得到transport對象
Transport ts = session.getTransport();
//3、連上郵件服務(wù)器
ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
//4、創(chuàng)建郵件
Message message = createAttachMail(session);
//5、發(fā)送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createAttachMail
* @Description: 創(chuàng)建一封帶附件的郵件
* @Anthor:孤傲蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createAttachMail(Session session) throws Exception{
MimeMessage message = new MimeMessage(session);
//設(shè)置郵件的基本信息
//發(fā)件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//收件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
//郵件標(biāo)題
message.setSubject("JavaMail郵件發(fā)送測試");
//創(chuàng)建郵件正文,為了避免郵件正文中文亂碼問題,需要使用charset=UTF-8指明字符編碼
MimeBodyPart text = new MimeBodyPart();
text.setContent("使用JavaMail創(chuàng)建的帶附件的郵件", "text/html;charset=UTF-8");
//創(chuàng)建郵件附件
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName()); //
//創(chuàng)建容器描述數(shù)據(jù)關(guān)系
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(attach);
mp.setSubType("mixed");
message.setContent(mp);
message.saveChanges();
//將創(chuàng)建的Email寫入到E盤存儲
message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
//返回生成的郵件
return message;
}
}
3.6、發(fā)送包含內(nèi)嵌圖片和附件的復(fù)雜郵件
package me.gacl.main;
import java.io.FileOutputStream;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* @ClassName: Sendmail
* @Description: 發(fā)送Email
* @author: 孤傲蒼狼
* @date: 2015-1-12 下午9:42:56
*
*/
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail發(fā)送郵件的5個步驟
//1、創(chuàng)建session
Session session = Session.getInstance(prop);
//開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運行狀態(tài)
session.setDebug(true);
//2、通過session得到transport對象
Transport ts = session.getTransport();
//3、連上郵件服務(wù)器
ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
//4、創(chuàng)建郵件
Message message = createMixedMail(session);
//5、發(fā)送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createMixedMail
* @Description: 生成一封帶附件和帶圖片的郵件
* @Anthor:孤傲蒼狼
*
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createMixedMail(Session session) throws Exception {
//創(chuàng)建郵件
MimeMessage message = new MimeMessage(session);
//設(shè)置郵件的基本信息
message.setFrom(new InternetAddress("gacl@sohu.com"));
message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));
message.setSubject("帶附件和帶圖片的的郵件");
//正文
MimeBodyPart text = new MimeBodyPart();
text.setContent("xxx這是女的xxxx<br/><img src='cid:aaa.jpg'>","text/html;charset=UTF-8");
//圖片
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));
image.setContentID("aaa.jpg");
//附件1
MimeBodyPart attach = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip"));
attach.setDataHandler(dh);
attach.setFileName(dh.getName());
//附件2
MimeBodyPart attach2 = new MimeBodyPart();
DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip"));
attach2.setDataHandler(dh2);
attach2.setFileName(MimeUtility.encodeText(dh2.getName()));
//描述關(guān)系:正文和圖片
MimeMultipart mp1 = new MimeMultipart();
mp1.addBodyPart(text);
mp1.addBodyPart(image);
mp1.setSubType("related");
//描述關(guān)系:正文和附件
MimeMultipart mp2 = new MimeMultipart();
mp2.addBodyPart(attach);
mp2.addBodyPart(attach2);
//代表正文的bodypart
MimeBodyPart content = new MimeBodyPart();
content.setContent(mp1);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
message.setContent(mp2);
message.saveChanges();
message.writeTo(new FileOutputStream("E:\\MixedMail.eml"));
//返回創(chuàng)建好的的郵件
return message;
}
}
以上就是使用JavaMail的API創(chuàng)建郵件和發(fā)送郵件的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- Java基于JavaMail實現(xiàn)向QQ郵箱發(fā)送郵件
- JavaWeb中使用JavaMail實現(xiàn)發(fā)送郵件功能實例詳解
- 基于SSM框架+Javamail發(fā)送郵件的代碼實例
- Spring框架JavaMailSender發(fā)送郵件工具類詳解
- Spring Boot中利用JavaMailSender發(fā)送郵件的方法示例(附源碼)
- SpringBoot整合JavaMail通過阿里云企業(yè)郵箱發(fā)送郵件的實現(xiàn)
- 使用JavaMail發(fā)送郵件保證成功的方法
- SpringBoot JavaMailSender發(fā)送郵件功能
- 基于java使用JavaMail發(fā)送郵件
- 基于Javamail實現(xiàn)發(fā)送郵件(QQ/網(wǎng)易郵件服務(wù)器)
相關(guān)文章
@FeignClient的使用和Spring?Boot的版本適配方式
這篇文章主要介紹了@FeignClient的使用和Spring?Boot的版本適配方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
深入理解JVM之Class類文件結(jié)構(gòu)詳解
這篇文章主要介紹了深入理解JVM之Class類文件結(jié)構(gòu),結(jié)合實例形式詳細(xì)分析了Class類文件結(jié)構(gòu)相關(guān)概念、原理、結(jié)構(gòu)、常用方法與屬性,需要的朋友可以參考下2019-09-09
Springcloud中的Nacos?Config服務(wù)配置流程分析
這篇文章主要介紹了Springcloud中的Nacos?Config服務(wù)配置,本文以用戶微服務(wù)為例,進(jìn)行統(tǒng)一的配置,結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
使用ServletInputStream()輸入流讀取圖片方式
這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
詳解SpringMVC @RequestBody接收J(rèn)son對象字符串
這篇文章主要介紹了詳解SpringMVC @RequestBody接收J(rèn)son對象字符串,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
解決@Autowired注入空指針問題(利用Bean的生命周期)
這篇文章主要介紹了解決@Autowired注入空指針問題(利用Bean的生命周期),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

