天天看點

spring各種郵件發送

<a href="http://www.blogjava.net/tangzurui/archive/2008/12/08/244953.html" target="_blank">參考位址一</a>

<a href="http://blog.csdn.net/is_zhoufeng/article/details/12004073" target="_blank">參考位址二</a>

<a href="http://blog.csdn.net/geloin/article/details/7547216" target="_blank">參考位址三</a>

<a href="http://blog.csdn.net/w605283073/article/details/44089327" target="_blank">參考位址四</a>

Spring郵件抽象層的主要包為org.springframework.mail。它包括了發送電子郵件的主要接口MailSender,和值對象SimpleMailMessage,它封裝了簡單郵件的屬性如from, to,cc,subject,text。 包裡還包含一棵以MailException為根的checked Exception繼承樹,它們提供了對底層郵件系統異常的進階别抽象。 要獲得關于郵件異常層次的更豐富的資訊,請參考Javadocs。

為了使用JavaMail中的一些特色, 比如MIME類型的信件, Spring提供了MailSender的一個子接口, 即org.springframework.mail.javamail.JavaMailSender。Spring還提供了一個回調接口org.springframework.mail.javamail.MimeMessagePreparator, 用于準備JavaMail的MIME信件。

1.發送簡單的文本郵件

package  net.xftzr.mail;

import  java.util.Properties;

import  org.springframework.mail.SimpleMailMessage; 

import  org.springframework.mail.javamail.JavaMailSenderImpl; 

/** 

 * 本類測試簡單郵件 

 * 直接用郵件發送

 *  @author  Administrator

 *

  */ 

public   class  SingleMailSend { 

   public   static   void  main(String args[]){ 

    JavaMailSenderImpl senderImpl  =   new  JavaMailSenderImpl(); 

   // 設定mail server  

    senderImpl.setHost( " smtp.163.com " );

     // 建立郵件消息  

    SimpleMailMessage mailMessage  =   new  SimpleMailMessage(); 

     // 設定收件人,寄件人 用數組發送多個郵件

     // String[] array = new String[]    {"[email protected]","[email protected]"};    

     // mailMessage.setTo(array);  

    mailMessage.setTo( " [email protected] " ); 

    mailMessage.setFrom( " [email protected] " ); 

    mailMessage.setSubject( " 測試簡單文本郵件發送! " ); 

    mailMessage.setText( " 測試我的簡單郵件發送機制!! " ); 

    senderImpl.setUsername( " userName " ) ;  //  根據自己的情況,設定username 

    senderImpl.setPassword( " password " ) ;  //  根據自己的情況, 設定password 

 Properties prop  =   new  Properties() ;

 prop.put( " mail.smtp.auth " ,  " true " ) ;  //  将這個參數設為true,讓伺服器進行認證,認證使用者名和密碼是否正确 

 prop.put( " mail.smtp.timeout " ,  " 25000 " ) ; 

 senderImpl.setJavaMailProperties(prop);  

     // 發送郵件  

    senderImpl.send(mailMessage); 

    System.out.println( " 郵件發送成功

spring各種郵件發送

.. " ); 

     } 

  } 

2.發送簡單的html郵件 

org.springframework.mail.javamail.MimeMessageHelper是處理JavaMail郵件常用的順手元件之一。它可以讓你擺脫繁複的javax.mail.internetAPI類

package net.xftzr.mail;

import java.util.Properties;

import javax.mail.internet.MimeMessage; 

import org.springframework.mail.javamail.JavaMailSenderImpl; 

import org.springframework.mail.javamail.MimeMessageHelper; 

 * 本類測試html郵件 

 * @author sunny 

 * 

 */ 

public class HTMLMailDemo { 

* @param args 

*/ 

public static void main(String[] args) throws Exception{ 

    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); 

    //設定mail server 

    senderImpl.setHost("smtp.163.com"); 

    //建立郵件消息,發送簡單郵件和html郵件的差別 

    MimeMessage mailMessage = senderImpl.createMimeMessage(); 

    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage); 

    //設定收件人,寄件人 

    messageHelper.setTo("[email protected]"); 

    messageHelper.setFrom("[email protected]"); 

    messageHelper.setSubject("測試HTML郵件!"); 

    //true 表示啟動HTML格式的郵件 

    messageHelper.setText("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;hello!!spring html Mail&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;",true); 

    senderImpl.setUsername("username") ; // 根據自己的情況,設定username

    senderImpl.setPassword("password") ; // 根據自己的情況, 設定password

    Properties prop = new Properties() ;

    prop.put("mail.smtp.auth", "true") ; // 将這個參數設為true,讓伺服器進行認證,認證使用者名和密碼是否正确

    prop.put("mail.smtp.timeout", "25000") ; 

    senderImpl.setJavaMailProperties(prop); 

    //發送郵件 

    System.out.println("郵件發送成功

spring各種郵件發送

.."); 

3.發送嵌套圖檔的郵件

Email允許添加附件,也允許在multipart信件中内嵌資源。内嵌資源可能是你在信件中希望使用的圖像,或者樣式表,但是又不想把它們作為附件。

import java.io.File; 

import org.springframework.core.io.FileSystemResource; 

 * 本類測試郵件中嵌套圖檔 

 * @author sunny    

public class AttachedImageMail { 

    //注意這裡的boolean,等于真的時候才能嵌套圖檔,在建構MimeMessageHelper時候,所給定的值是true表示啟用,         

    //multipart模式 

    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true); 

    messageHelper.setTo("[email protected]"); 

    messageHelper.setSubject("測試郵件中嵌套圖檔!!"); 

    messageHelper.setText("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;hello!!spring image html mail&lt;/h1&gt;" + 

    "&lt;img src=\"cid:aaa\"/&gt;&lt;/body&gt;&lt;/html&gt;",true); 

    FileSystemResource img = new FileSystemResource(new File("g:/123.jpg")); 

    messageHelper.addInline("aaa",img); 

spring各種郵件發送

4.發送包含附件的郵件

spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送

import org.springframework.mail .javamail.MimeMessageHelper; 

spring各種郵件發送

public class AttachedFileMail { 

spring各種郵件發送
spring各種郵件發送

* 本類測試的是關于郵件中帶有附件的例子 

spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送

    //multipart模式 為true時發送附件 可以設定html格式

spring各種郵件發送

    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8"); 

spring各種郵件發送
spring各種郵件發送
spring各種郵件發送

    messageHelper.setTo("[email protected]");    

spring各種郵件發送
spring各種郵件發送

    messageHelper.setSubject("測試郵件中上傳附件!!"); 

spring各種郵件發送
spring各種郵件發送

    messageHelper.setText("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;你好:附件中有學習資料!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;",true); 

spring各種郵件發送
spring各種郵件發送

    FileSystemResource file = new FileSystemResource(new File("g:/test.rar")); 

spring各種郵件發送

    //這裡的方法調用和插入圖檔是不同的。 

spring各種郵件發送

    messageHelper.addAttachment("test.rar",file); 

spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送
spring各種郵件發送

}