一、RFC882文檔簡單說明
RFC882文檔規定了如何編寫一封簡單的郵件(純文本郵件),一封簡單的郵件包含郵件頭和郵件體兩個部分,郵件頭和郵件體之間使用空行分隔。
郵件頭包含的內容有:
from字段 --用于指明發件人
to字段 --用于指明收件人
subject字段 --用于說明郵件主題
cc字段 -- 抄送,將郵件發送給收件人的同時抄送給另一個收件人,收件人可以看到郵件抄送給了誰
bcc字段 -- 密送,將郵件發送給收件人的同時將郵件秘密發送給另一個收件人,收件人無法看到郵件密送給了誰
郵件體指的就是郵件的具體內容。
二、MIME協議簡單介紹
在我們的實際開發當中,一封郵件既可能包含圖片,又可能包含有附件,在這樣的情況下,RFC882文檔規定的郵件格式就無法滿足要求了。
MIME協議是對RFC822文檔的升級和補充,它描述了如何生產一封復雜的郵件。通常我們把MIME協議描述的郵件稱之為MIME郵件。MIME協議描述的數據稱之為MIME消息。
對于一封復雜郵件,如果包含了多個不同的數據,MIME協議規定了要使用分隔線對多段數據進行分隔,并使用Content-Type頭字段對數據的類型、以及多個數據之間的關系進行描述。
三、使用JavaMail創建郵件和發送郵件
JavaMail創建的郵件是基于MIME協議的。因此可以使用JavaMail創建出包含圖片,包含附件的復雜郵件。
3.1、JavaMail API的簡單介紹
3.2、創建郵件發送測試項目
3.3、發送一封只包含文本的簡單郵件
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: 發送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發送郵件的5個步驟 //1、創建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發送Email的運行狀態 session.setDebug(true); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、使用郵箱的用戶名和密碼連上郵件服務器,發送郵件時,發件人需要提交郵箱的用戶名和密碼給smtp服務器,用戶名和密碼都通過驗證之后才能夠正常發送郵件給收件人。 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼"); //4、創建郵件 Message message = createSimpleMail(session); //5、發送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createSimpleMail * @Description: 創建一封只包含文本的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createSimpleMail(Session session) throws Exception { //創建郵件對象 MimeMessage message = new MimeMessage(session); //指明郵件的發件人 message.setFrom(new InternetAddress("gacl@sohu.com")); //指明郵件的收件人,現在發件人和收件人是一樣的,那就是自己給自己發 message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com")); //郵件的標題 message.setSubject("只包含文本的簡單郵件"); //郵件的文本內容 message.setContent("你好啊!", "text/html;charset=UTF-8"); //返回創建好的郵件對象 return message; }}
3.4、發送包含內嵌圖片的郵件
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: 發送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發送郵件的5個步驟 //1、創建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發送Email的運行狀態 session.setDebug(true); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、連上郵件服務器,需要發件人提供郵箱的用戶名和密碼進行驗證 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼"); //4、創建郵件 Message message = createImageMail(session); //5、發送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createImageMail * @Description: 生成一封郵件正文帶圖片的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createImageMail(Session session) throws Exception { //創建郵件 MimeMessage message = new MimeMessage(session); // 設置郵件的基本信息 //發件人 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("這是一封郵件正文帶圖片<img src='cid:xxx.jpg'>的郵件", "text/html;charset=UTF-8"); // 準備圖片數據 MimeBodyPart image = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("src//1.jpg")); image.setDataHandler(dh); image.setContentID("xxx.jpg"); // 描述數據關系 MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text); mm.addBodyPart(image); mm.setSubType("related"); message.setContent(mm); message.saveChanges(); //將創建好的郵件寫入到E盤以文件的形式進行保存 message.writeTo(new FileOutputStream("E://ImageMail.eml")); //返回創建好的郵件 return message; }}
3.5、發送包含附件的郵件
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: 發送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發送郵件的5個步驟 //1、創建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發送Email的運行狀態 session.setDebug(true); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、連上郵件服務器 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼"); //4、創建郵件 Message message = createAttachMail(session); //5、發送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createAttachMail * @Description: 創建一封帶附件的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createAttachMail(Session session) throws Exception{ MimeMessage message = new MimeMessage(session); //設置郵件的基本信息 //發件人 message.setFrom(new InternetAddress("gacl@sohu.com")); //收件人 message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn")); //郵件標題 message.setSubject("JavaMail郵件發送測試"); //創建郵件正文,為了避免郵件正文中文亂碼問題,需要使用charset=UTF-8指明字符編碼 MimeBodyPart text = new MimeBodyPart(); text.setContent("使用JavaMail創建的帶附件的郵件", "text/html;charset=UTF-8"); //創建郵件附件 MimeBodyPart attach = new MimeBodyPart(); DataHandler dh = new DataHandler(new FileDataSource("src//2.jpg")); attach.setDataHandler(dh); attach.setFileName(dh.getName()); // //創建容器描述數據關系 MimeMultipart mp = new MimeMultipart(); mp.addBodyPart(text); mp.addBodyPart(attach); mp.setSubType("mixed"); message.setContent(mp); message.saveChanges(); //將創建的Email寫入到E盤存儲 message.writeTo(new FileOutputStream("E://attachMail.eml")); //返回生成的郵件 return message; }}
3.6、發送包含內嵌圖片和附件的復雜郵件
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: 發送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發送郵件的5個步驟 //1、創建session Session session = Session.getInstance(prop); //開啟Session的debug模式,這樣就可以查看到程序發送Email的運行狀態 session.setDebug(true); //2、通過session得到transport對象 Transport ts = session.getTransport(); //3、連上郵件服務器 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼"); //4、創建郵件 Message message = createMixedMail(session); //5、發送郵件 ts.sendMessage(message, message.getAllRecipients()); ts.close(); } /** * @Method: createMixedMail * @Description: 生成一封帶附件和帶圖片的郵件 * @Anthor:孤傲蒼狼 * * @param session * @return * @throws Exception */ public static MimeMessage createMixedMail(Session session) throws Exception { //創建郵件 MimeMessage message = new MimeMessage(session); //設置郵件的基本信息 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())); //描述關系:正文和圖片 MimeMultipart mp1 = new MimeMultipart(); mp1.addBodyPart(text); mp1.addBodyPart(image); mp1.setSubType("related"); //描述關系:正文和附件 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")); //返回創建好的的郵件 return message; }}
以上就是使用JavaMail的API創建郵件和發送郵件的全部內容,希望對大家的學習有所幫助。
新聞熱點
疑難解答