使用JavaMail建立郵件和發送郵件

使用JavaMail建立郵件和發送郵件

1、RFC882文檔簡單說明

  RFC882文檔規定了如何編寫一封簡單的郵件(純文本郵件),一封簡單的郵件包含郵件頭和郵件體兩個部分,郵件頭和郵件體之間使用空行分隔。html

  郵件頭包含的內容有:java

  1. from字段   --用於指明發件人
  2. to字段       --用於指明收件人
  3. subject字段  --用於說明郵件主題
  4. cc字段      -- 抄送,將郵件發送給收件人的同時抄送給另外一個收件人,收件人能夠看到郵件抄送給了誰
  5. bcc字段    -- 密送,將郵件發送給收件人的同時將郵件祕密發送給另外一個收件人,收件人沒法看到郵件密送給了誰

  郵件體指的就是郵件的具體內容。服務器

2、MIME協議簡單介紹

  在咱們的實際開發當中,一封郵件既可能包含圖片,又可能包含有附件,在這樣的狀況下,RFC882文檔規定的郵件格式就沒法知足要求了。session

  MIME協議是對RFC822文檔的升級和補充,它描述瞭如何生產一封複雜的郵件。一般咱們把MIME協議描述的郵件稱之爲MIME郵件MIME協議描述的數據稱之爲MIME消息。
  對於一封複雜郵件,若是包含了多個不一樣的數據,MIME協議規定了要使用分隔線對多段數據進行分隔,並使用Content-Type頭字段對數據的類型、以及多個數據之間的關係進行描述。
post

3、使用JavaMail建立郵件和發送郵件

  JavaMail建立的郵件是基於MIME協議的。所以可使用JavaMail建立出包含圖片,包含附件的複雜郵件。測試

3.一、JavaMail API的簡單介紹

  

  

  

3.二、建立郵件發送測試項目

  

3.三、發送一封只包含文本的簡單郵件

複製代碼
 1 package me.gacl.main;  2  3 import java.util.Properties;  4 import javax.mail.Message;  5 import javax.mail.Session;  6 import javax.mail.Transport;  7 import javax.mail.internet.InternetAddress;  8 import javax.mail.internet.MimeMessage;  9 10 /** 11 * @ClassName: Sendmail 12 * @Description: 發送Email 13 * @author: 孤傲蒼狼 14 * @date: 2015-1-12 下午9:42:56 15 * 16 */ 17 public class Sendmail { 18 19 /** 20  * @param args 21  * @throws Exception 22 */ 23 public static void main(String[] args) throws Exception { 24 25 Properties prop = new Properties(); 26 prop.setProperty("mail.host", "smtp.sohu.com"); 27 prop.setProperty("mail.transport.protocol", "smtp"); 28 prop.setProperty("mail.smtp.auth", "true"); 29 //使用JavaMail發送郵件的5個步驟 30 //一、建立session 31 Session session = Session.getInstance(prop); 32 //開啓Session的debug模式,這樣就能夠查看到程序發送Email的運行狀態 33 session.setDebug(true); 34 //二、經過session獲得transport對象 35 Transport ts = session.getTransport(); 36 //三、使用郵箱的用戶名和密碼連上郵件服務器,發送郵件時,發件人須要提交郵箱的用戶名和密碼給smtp服務器,用戶名和密碼都經過驗證以後纔可以正常發送郵件給收件人。 37 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼"); 38 //四、建立郵件 39 Message message = createSimpleMail(session); 40 //五、發送郵件 41  ts.sendMessage(message, message.getAllRecipients()); 42  ts.close(); 43  } 44 45 /** 46  * @Method: createSimpleMail 47  * @Description: 建立一封只包含文本的郵件 48  * @Anthor:孤傲蒼狼 49  * 50  * @param session 51  * @return 52  * @throws Exception 53 */ 54 public static MimeMessage createSimpleMail(Session session) 55 throws Exception { 56 //建立郵件對象 57 MimeMessage message = new MimeMessage(session); 58 //指明郵件的發件人 59 message.setFrom(new InternetAddress("gacl@sohu.com")); 60 //指明郵件的收件人,如今發件人和收件人是同樣的,那就是本身給本身發 61 message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com")); 62 //郵件的標題 63 message.setSubject("只包含文本的簡單郵件"); 64 //郵件的文本內容 65 message.setContent("你好啊!", "text/html;charset=UTF-8"); 66 //返回建立好的郵件對象 67 return message; 68  } 69 }
複製代碼

3.四、發送包含內嵌圖片的郵件

複製代碼
 1 package me.gacl.main;  2  3 import java.io.FileOutputStream;  4 import java.util.Properties;  5  6 import javax.activation.DataHandler;  7 import javax.activation.FileDataSource;  8 import javax.mail.Message;  9 import javax.mail.Session; 10 import javax.mail.Transport; 11 import javax.mail.internet.InternetAddress; 12 import javax.mail.internet.MimeBodyPart; 13 import javax.mail.internet.MimeMessage; 14 import javax.mail.internet.MimeMultipart; 15 16 /** 17 * @ClassName: Sendmail 18 * @Description: 發送Email 19 * @author: 孤傲蒼狼 20 * @date: 2015-1-12 下午9:42:56 21 * 22 */ 23 public class Sendmail { 24 25 /** 26  * @param args 27  * @throws Exception 28 */ 29 public static void main(String[] args) throws Exception { 30 31 Properties prop = new Properties(); 32 prop.setProperty("mail.host", "smtp.sohu.com"); 33 prop.setProperty("mail.transport.protocol", "smtp"); 34 prop.setProperty("mail.smtp.auth", "true"); 35 //使用JavaMail發送郵件的5個步驟 36 //一、建立session 37 Session session = Session.getInstance(prop); 38 //開啓Session的debug模式,這樣就能夠查看到程序發送Email的運行狀態 39 session.setDebug(true); 40 //二、經過session獲得transport對象 41 Transport ts = session.getTransport(); 42 //三、連上郵件服務器,須要發件人提供郵箱的用戶名和密碼進行驗證 43 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼"); 44 //四、建立郵件 45 Message message = createImageMail(session); 46 //五、發送郵件 47  ts.sendMessage(message, message.getAllRecipients()); 48  ts.close(); 49  } 50 51 /** 52  * @Method: createImageMail 53  * @Description: 生成一封郵件正文帶圖片的郵件 54  * @Anthor:孤傲蒼狼 55  * 56  * @param session 57  * @return 58  * @throws Exception 59 */ 60 public static MimeMessage createImageMail(Session session) throws Exception { 61 //建立郵件 62 MimeMessage message = new MimeMessage(session); 63 // 設置郵件的基本信息 64 //發件人 65 message.setFrom(new InternetAddress("gacl@sohu.com")); 66 //收件人 67 message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn")); 68 //郵件標題 69 message.setSubject("帶圖片的郵件"); 70 71 // 準備郵件數據 72 // 準備郵件正文數據 73 MimeBodyPart text = new MimeBodyPart(); 74 text.setContent("這是一封郵件正文帶圖片<img src='cid:xxx.jpg'>的郵件", "text/html;charset=UTF-8"); 75 // 準備圖片數據 76 MimeBodyPart image = new MimeBodyPart(); 77 DataHandler dh = new DataHandler(new FileDataSource("src\\1.jpg")); 78 image.setDataHandler(dh); 79 image.setContentID("xxx.jpg"); 80 // 描述數據關係 81 MimeMultipart mm = new MimeMultipart(); 82 mm.addBodyPart(text); 83 mm.addBodyPart(image); 84 mm.setSubType("related"); 85 86 message.setContent(mm); 87 message.saveChanges(); 88 //將建立好的郵件寫入到E盤以文件的形式進行保存 89 message.writeTo(new FileOutputStream("E:\\ImageMail.eml")); 90 //返回建立好的郵件 91 return message; 92 } 93 }
複製代碼

3.五、發送包含附件的郵件

複製代碼
 1 package me.gacl.main;  2  3 import java.io.FileOutputStream;  4 import java.util.Properties;  5  6 import javax.activation.DataHandler;  7 import javax.activation.FileDataSource;  8 import javax.mail.Message;  9 import javax.mail.Session; 10 import javax.mail.Transport; 11 import javax.mail.internet.InternetAddress; 12 import javax.mail.internet.MimeBodyPart; 13 import javax.mail.internet.MimeMessage; 14 import javax.mail.internet.MimeMultipart; 15 16 /** 17 * @ClassName: Sendmail 18 * @Description: 發送Email 19 * @author: 孤傲蒼狼 20 * @date: 2015-1-12 下午9:42:56 21 * 22 */ 23 public class Sendmail { 24 25 /** 26  * @param args 27  * @throws Exception 28 */ 29 public static void main(String[] args) throws Exception { 30 31 Properties prop = new Properties(); 32 prop.setProperty("mail.host", "smtp.sohu.com"); 33 prop.setProperty("mail.transport.protocol", "smtp"); 34 prop.setProperty("mail.smtp.auth", "true"); 35 //使用JavaMail發送郵件的5個步驟 36 //一、建立session 37 Session session = Session.getInstance(prop); 38 //開啓Session的debug模式,這樣就能夠查看到程序發送Email的運行狀態 39 session.setDebug(true); 40 //二、經過session獲得transport對象 41 Transport ts = session.getTransport(); 42 //三、連上郵件服務器 43 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼"); 44 //四、建立郵件 45 Message message = createAttachMail(session); 46 //五、發送郵件 47  ts.sendMessage(message, message.getAllRecipients()); 48  ts.close(); 49  } 50 51 /** 52  * @Method: createAttachMail 53  * @Description: 建立一封帶附件的郵件 54  * @Anthor:孤傲蒼狼 55  * 56  * @param session 57  * @return 58  * @throws Exception 59 */ 60 public static MimeMessage createAttachMail(Session session) throws Exception{ 61 MimeMessage message = new MimeMessage(session); 62 63 //設置郵件的基本信息 64 //發件人 65 message.setFrom(new InternetAddress("gacl@sohu.com")); 66 //收件人 67 message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn")); 68 //郵件標題 69 message.setSubject("JavaMail郵件發送測試"); 70 71 //建立郵件正文,爲了不郵件正文中文亂碼問題,須要使用charset=UTF-8指明字符編碼 72 MimeBodyPart text = new MimeBodyPart(); 73 text.setContent("使用JavaMail建立的帶附件的郵件", "text/html;charset=UTF-8"); 74 75 //建立郵件附件 76 MimeBodyPart attach = new MimeBodyPart(); 77 DataHandler dh = new DataHandler(new FileDataSource("src\\2.jpg")); 78 attach.setDataHandler(dh); 79 attach.setFileName(dh.getName()); // 80 81 //建立容器描述數據關係 82 MimeMultipart mp = new MimeMultipart(); 83 mp.addBodyPart(text); 84 mp.addBodyPart(attach); 85 mp.setSubType("mixed"); 86 87 message.setContent(mp); 88 message.saveChanges(); 89 //將建立的Email寫入到E盤存儲 90 message.writeTo(new FileOutputStream("E:\\attachMail.eml")); 91 //返回生成的郵件 92 return message; 93 } 94 }
複製代碼

3.六、發送包含內嵌圖片和附件的複雜郵件

複製代碼
  1 package me.gacl.main;  2  3 import java.io.FileOutputStream;  4 import java.util.Properties;  5 import javax.activation.DataHandler;  6 import javax.activation.FileDataSource;  7 import javax.mail.Message;  8 import javax.mail.Session;  9 import javax.mail.Transport;  10 import javax.mail.internet.InternetAddress;  11 import javax.mail.internet.MimeBodyPart;  12 import javax.mail.internet.MimeMessage;  13 import javax.mail.internet.MimeMultipart;  14 import javax.mail.internet.MimeUtility;  15  16 /**  17 * @ClassName: Sendmail  18 * @Description: 發送Email  19 * @author: 孤傲蒼狼  20 * @date: 2015-1-12 下午9:42:56  21 *  22 */  23 public class Sendmail {  24  25 /**  26  * @param args  27  * @throws Exception  28 */  29 public static void main(String[] args) throws Exception {  30  31 Properties prop = new Properties();  32 prop.setProperty("mail.host", "smtp.sohu.com");  33 prop.setProperty("mail.transport.protocol", "smtp");  34 prop.setProperty("mail.smtp.auth", "true");  35 //使用JavaMail發送郵件的5個步驟  36 //一、建立session  37 Session session = Session.getInstance(prop);  38 //開啓Session的debug模式,這樣就能夠查看到程序發送Email的運行狀態  39 session.setDebug(true);  40 //二、經過session獲得transport對象  41 Transport ts = session.getTransport();  42 //三、連上郵件服務器  43 ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");  44 //四、建立郵件  45 Message message = createMixedMail(session);  46 //五、發送郵件  47  ts.sendMessage(message, message.getAllRecipients());  48  ts.close();  49  }  50  51 /**  52  * @Method: createMixedMail  53  * @Description: 生成一封帶附件和帶圖片的郵件  54  * @Anthor:孤傲蒼狼  55  *  56  * @param session  57  * @return  58  * @throws Exception  59 */  60 public static MimeMessage createMixedMail(Session session) throws Exception {  61 //建立郵件  62 MimeMessage message = new MimeMessage(session);  63  64 //設置郵件的基本信息  65 message.setFrom(new InternetAddress("gacl@sohu.com"));  66 message.setRecipient(Message.RecipientType.TO, new InternetAddress("xdp_gacl@sina.cn"));  67 message.setSubject("帶附件和帶圖片的的郵件");  68  69 //正文  70 MimeBodyPart text = new MimeBodyPart();  71 text.setContent("xxx這是女的xxxx<br/><img src='cid:aaa.jpg'>","text/html;charset=UTF-8");  72  73 //圖片  74 MimeBodyPart image = new MimeBodyPart();  75 image.setDataHandler(new DataHandler(new FileDataSource("src\\3.jpg")));  76 image.setContentID("aaa.jpg");  77  78 //附件1 79 MimeBodyPart attach = new MimeBodyPart(); 80 DataHandler dh = new DataHandler(new FileDataSource("src\\4.zip")); 81 attach.setDataHandler(dh); 82 attach.setFileName(dh.getName()); 83 84 //附件2 85 MimeBodyPart attach2 = new MimeBodyPart(); 86 DataHandler dh2 = new DataHandler(new FileDataSource("src\\波子.zip")); 87 attach2.setDataHandler(dh2); 88 attach2.setFileName(MimeUtility.encodeText(dh2.getName())); 89 90 //描述關係:正文和圖片 91 MimeMultipart mp1 = new MimeMultipart(); 92 mp1.addBodyPart(text); 93 mp1.addBodyPart(image); 94 mp1.setSubType("related"); 95 96 //描述關係:正文和附件 97 MimeMultipart mp2 = new MimeMultipart(); 98 mp2.addBodyPart(attach); 99 mp2.addBodyPart(attach2); 100 101 //表明正文的bodypart 102 MimeBodyPart content = new MimeBodyPart(); 103 content.setContent(mp1); 104 mp2.addBodyPart(content); 105 mp2.setSubType("mixed"); 106 107 message.setContent(mp2); 108 message.saveChanges(); 109 110 message.writeTo(new FileOutputStream("E:\\MixedMail.eml")); 111 //返回建立好的的郵件 112 return message; 113 } 114 }
複製代碼
相關文章
相關標籤/搜索