1. 程式人生 > >使用Java傳送各種格式的郵件

使用Java傳送各種格式的郵件

轉自:https://blog.csdn.net/u011031689/article/details/51326595


          測試可用:

         有些重複程式碼沒有給註釋。類的方法作用自行檢視API瞭解,最後附上原始碼。


        首先使用JavaMail的jar,官網可下載。

       公共靜態的常量:

       

 
  1. public final static String MAIL = "@sina.com"; // 郵箱格式

  2. public final static String SEND_HOST = "smtp.sina.com"; // 郵箱傳送伺服器

  3. public final static String ACCEPT_HOST = "pop.sina.com"; // 郵箱伺服器

  4. public final static String SEND_USER = "xxxx"; // 使用者名稱

  5. public final static String SEND_PWD = "xxxxx"; // 密碼

  6. public final static String FROM_MAIL = SEND_USER + "@sina.com";// 傳送方郵箱地址

  7. public final static String TO_MAIL = "[email protected]"; // 接收方郵箱地址

  8. public final static String CC_MAIL = SEND_USER + MAIL; // 抄送方郵箱地址

  9. public final static String BCC_MAIl = SEND_USER + MAIL; // 密送方郵箱地址

  10.  
  11. public final static String ENCODE = "UTF-8";

  12. public static Date date = new Date();

    

        使用java自帶的介面和類傳送文字格式郵件

   

 
  1. Properties prop = new Properties();

  2. prop.setProperty("mail.host", SEND_HOST);

  3. prop.setProperty("mail.transport.protocol", "smtp");

  4. prop.setProperty("mail.smtps.ssl.enable", "true");

  5. // prop.setProperty("mail.smtp.port", "25");

  6. prop.setProperty("mail.smtp.auth", "true");

  7.  
  8. Session session = Session.getInstance(prop); //建立應用會話

  9.  
  10. Message message = new MimeMessage(session); //訊息摘要,是郵件的主體

  11. message.setSubject("測試"); //設定主題

  12. message.setText("你好!"); //郵件內容

  13. message.setSentDate(new Date()); //傳送日期

  14. message.setFrom(new InternetAddress(FROM_MAIL)); //傳送方

  15. message.addRecipient(Message.RecipientType.TO, new InternetAddress(TO_MAIL)); //接受方

  16. message.saveChanges(); //儲存郵件主體物件內容

  17.  
  18. Transport transport = session.getTransport(); //傳輸物件

  19. transport.connect(SEND_HOST, FROM_MAIL, SEND_PWD); //連線伺服器中的郵箱

  20. transport.sendMessage(message, message.getAllRecipients()); //傳送

  21. transport.close(); //關閉傳輸

  22. System.out.println("Successfully send mail to all user");

 

感覺自帶的介面方法比較麻煩,使用spring封裝的javamail,記的匯入spring相關包。

 

使用spring傳送文字格式的郵件,程式碼如下:

 

 
  1. public static void sendTxtMail() throws MailException {

  2. JavaMailSenderImpl send = new JavaMailSenderImpl();

  3. Properties prop = new Properties();

  4. prop.setProperty("mail.transport.protocol", "smtp");

  5. prop.setProperty("mail.host", SEND_HOST);

  6.  
  7. prop.setProperty("mail.smtps.ssl.enable", "true");

  8. prop.setProperty("mail.smtp.auth", "true");

  9.  
  10.  
  11. send.setUsername(SEND_USER);

  12. send.setPassword(SEND_PWD);

  13. send.setJavaMailProperties(prop);

  14.  
  15. SimpleMailMessage msg = new SimpleMailMessage();

  16. msg.setFrom(FROM_MAIL);

  17. msg.setTo(TO_MAIL);

  18. msg.setReplyTo(FROM_MAIL);

  19. msg.setCc(CC_MAIL);

  20. msg.setBcc(BCC_MAIl);

  21. msg.setSentDate(date);

  22. msg.setSubject("傳送的文字格式郵件");

  23. msg.setText("文字格式 測試成功!");

  24. send.send(msg);

  25. System.out.println("Successfully send mail to the user");


使用spring的封裝方法傳送Html格式郵件 ,程式碼如下:

 

 
  1. // 傳送Html格式郵件

  2. public static void sendHtmlMail() throws Exception {

  3. JavaMailSenderImpl send = new JavaMailSenderImpl();

  4. Properties prop = new Properties();

  5. prop.setProperty("mail.transport.protocol", "smtp");

  6. prop.setProperty("mail.host", SEND_HOST);

  7. prop.setProperty("mail.smtps.ssl.enable", "true");

  8. prop.setProperty("mail.smtp.auth", "true");

  9.  
  10. send.setUsername(SEND_USER);

  11. send.setPassword(SEND_PWD);

  12. send.setJavaMailProperties(prop);

  13.  
  14. MimeMessage msg = send.createMimeMessage();

  15.  
  16. MimeMessageHelper helper = new MimeMessageHelper(msg, ENCODE);

  17. helper.setFrom(FROM_MAIL);

  18. helper.setTo(TO_MAIL);

  19. helper.setReplyTo(FROM_MAIL);

  20. helper.setCc(CC_MAIL);

  21. helper.setBcc(BCC_MAIl);

  22. helper.setSentDate(date);

  23. helper.setSubject("傳送的HTML格式郵件");

  24. String html = "<font size='5' color='red'>HTML格式測試成功!</font>";

  25. helper.setText(html, true); // 郵件內容,引數true表示是html程式碼

  26. send.send(msg);

  27. System.out.println("Successfully send mail to the user");

 

使用spring的封裝方法傳送帶內嵌內容的Html格式郵件 ,程式碼如下:

 

 

 
  1. // 傳送帶內嵌檔案的HTML格式郵件

  2. public static void sendInlineMail() throws Exception {

  3. // spring提供的郵件實現類

  4. JavaMailSenderImpl send = new JavaMailSenderImpl();

  5. Properties prop = new Properties();

  6. prop.setProperty("mail.transport.protocol", "smtp"); // 設定郵件傳送協議

  7. prop.setProperty("mail.host", SEND_HOST); // 郵件伺服器地址

  8. prop.setProperty("mail.smtps.ssl.enable", "true"); // 郵件ssl驗證

  9. prop.setProperty("mail.smtp.auth", "true"); // 郵件服務身份驗證

  10.  
  11. send.setUsername(SEND_USER); // 設定使用者名稱

  12. send.setPassword(SEND_PWD); // 設定密碼

  13. send.setJavaMailProperties(prop);

  14.  
  15. MimeMessage msg = send.createMimeMessage();

  16. // 指定HTML編碼,引數true表示為multipart

  17. MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);

  18. helper.setFrom(FROM_MAIL); // 傳送者郵箱

  19. helper.setTo(TO_MAIL); // 接收者郵箱

  20. helper.setReplyTo(FROM_MAIL); // 回覆郵箱

  21. helper.setCc(CC_MAIL); // 抄送郵箱

  22. helper.setBcc(BCC_MAIl); // 密送郵箱

  23. helper.setSentDate(date); // 傳送日期

  24. helper.setSubject("傳送的帶有內部檔案的HTML格式郵件");

  25. String html = "<font size='5' color='red'>HTML格式測試成功!</font>"

  26. + "<img src ='cid:demoimg'>"; // cid是一個固定字首,demoimg是一個資源名稱

  27. helper.setText(html, true); // 郵件內容,引數true表示是html程式碼

  28. ClassPathResource resource = new ClassPathResource("col.jpg"); // 載入專案路徑下資源

  29. helper.addInline("demoimg", resource); // 將資源繫結到demoimg上

  30. send.send(msg); // 傳送郵件

  31. System.out.println("Successfully send mail to the user");

  32. }


使用spring的封裝方法傳送帶附件的郵件 ,程式碼如下:

 

 

 
  1. // 傳送帶附件的郵件

  2. public static void sendAttachmentMail() throws Exception {

  3. // spring提供的郵件實現類

  4. JavaMailSenderImpl send = new JavaMailSenderImpl();

  5. Properties prop = new Properties();

  6. prop.setProperty("mail.transport.protocol", "smtp"); // 設定郵件傳送協議

  7. prop.setProperty("mail.host", SEND_HOST); // 郵件伺服器地址

  8. prop.setProperty("mail.smtps.ssl.enable", "true"); // 郵件ssl驗證

  9. prop.setProperty("mail.smtp.auth", "true"); // 郵件服務身份驗證

  10.  
  11. send.setUsername(SEND_USER); // 設定使用者名稱

  12. send.setPassword(SEND_PWD); // 設定密碼

  13. send.setJavaMailProperties(prop);

  14.  
  15. MimeMessage msg = send.createMimeMessage();

  16. // 指定HTML編碼,引數true表示為multipart

  17. MimeMessageHelper helper = new MimeMessageHelper(msg, true, ENCODE);

  18. helper.setFrom(FROM_MAIL); // 傳送者郵箱

  19. helper.setTo(TO_MAIL); // 接收者郵箱

  20. helper.setReplyTo(FROM_MAIL); // 回覆郵箱

  21. helper.setCc(CC_MAIL); // 抄送郵箱

  22. helper.setBcc(BCC_MAIl); // 密送郵箱

  23. helper.setSentDate(date); // 傳送日期

  24. helper.setSubject("傳送帶有附件的郵件");

  25. String html = "<font size='5' color='red'>附件測試成功!</font>";

  26. helper.setText(html, true); // 郵件內容,引數true表示是html程式碼

  27. String demo = "demo.docx";

  28. ClassPathResource resource = new ClassPathResource(demo); // 載入專案路徑下資源

  29. helper.addAttachment(MimeUtility.encodeWord(demo), resource); // 如果檔案是中文名,需要轉碼。

  30. send.send(msg); // 傳送郵件

  31. System.out.println("Successfully send mail to the user");

  32. }

 

  

   在測試之前記得將郵箱smtp、pop設定上,否者會報驗證錯誤或連線服務錯誤 即50X系列錯誤。