怎樣使用Spring發郵件?

關於Spring的郵件發送的功能演示:
(我把程序中一些敏感的地方替換成了其餘的文字,直接調試確定不行,只是給你們一個參考)
  須要用到的jar包是
  spring-context.jar
  mail.jar
  spring-core.jar
  activation.jar
  commons-logging.jar
  這些是程序必需要用到的包。
 
1:簡單郵件
  簡單郵件中是能夠實現郵件羣發的功能的,區別是收件人能夠是一個數 組。
   
package com; 
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
         /**
            * 本類測試簡單郵件
            * @author sunny
            *
            */

public class SingleMailSend {
  public static void main(String args[]){
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
     //設定mail server
    senderImpl.setHost( "192.168.1.1");
    senderImpl.setUsername( "username");
    senderImpl.setPassword( "password");
    
    
     //創建郵件消息
    SimpleMailMessage mailMessage = new SimpleMailMessage();
     //設置收件人,寄件人
                 //String[] array = new String[]    {"[email]sun111@163.com[/email]","[email]sun222@sohu.com[/email]"};    
     //mailMessage.setTo(array);
    mailMessage.setTo( "[email]sun111@163.com[/email]");
    mailMessage.setFrom( "[email]webadmin@163.com[/email]");
    mailMessage.setSubject( "測試郵件羣發!");
    mailMessage.setText( "測試個人簡單郵件發送機制!!");
    
     //發送郵件
    senderImpl.send(mailMessage);
    
    System.out.println( "郵件發送成功.....");
     }
        }

2:HTML郵件的發送
  
package com;
         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( "192.168.1.1");
    senderImpl.setUsername( "username");
    senderImpl.setPassword( "password");
    
                 //創建郵件消息,發送簡單郵件和html郵件的區別
    MimeMessage mailMessage = senderImpl.createMimeMessage();
    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
                
     //設置收件人,寄件人
    messageHelper.setTo( "[email]sun111@163.com[/email]");
    messageHelper.setFrom( "[email]webadmin@163.com[/email]");
    messageHelper.setSubject( "測試HTML郵件!");
     //true 表示啓動HTML格式的郵件
    messageHelper.setText( "<html><head></head><body><h1>hello!!zhangjian</h1></body></html>", true);
    
     //發送郵件
    senderImpl.send(mailMessage);
    
    System.out.println( "郵件發送成功.....");
}
        }
3:本類測試郵件中嵌套圖片
 
package com;
         import java.io.File;
         import javax.mail.internet.MimeMessage;
         import org.springframework.core.io.FileSystemResource;
         import org.springframework.mail.javamail.JavaMailSenderImpl;
         import org.springframework.mail.javamail.MimeMessageHelper;
         /**
         * 本類測試郵件中嵌套圖片
         * @author sunny    
         *
         */

         public class AttachedImageMail {
public static void main(String[] args) throws Exception{
    JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    
     //設定mail server
    senderImpl.setHost( "192.168.1.2");
    senderImpl.setUsername( "username");
    senderImpl.setPassword( "pssword");
     //創建郵件消息,發送簡單郵件和html郵件的區別
    MimeMessage mailMessage = senderImpl.createMimeMessage();
     //注意這裏的boolean,等於真的時候才能嵌套圖片,在構建MimeMessageHelper時候,所給定的值是true表示啓用,        
multipart模式
    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
    
     //設置收件人,寄件人
    messageHelper.setTo( "[email]sun111@163.com[/email]");
    messageHelper.setFrom( "[email]webadmin@163.com[/email]");
    messageHelper.setSubject( "測試郵件中嵌套圖片!!");
     //true 表示啓動HTML格式的郵件
    messageHelper.setText( "<html><head></head><body><h1>hello!!zhangjian</h1>" +
         "<img src=\"cid:aaa\"/></body></html>", true);
            
    FileSystemResource img = new FileSystemResource( new File( "c:/aaa.jpg"));
    
    messageHelper.addInline( "aaa",img);
    
     //發送郵件
    senderImpl.send(mailMessage);
    
    System.out.println( "郵件發送成功.....");
}
        }
   
4:  測試郵件中包含附件
   
package com;
    
         import java.io.File;
         import javax.mail.internet.MimeMessage;
         import org.springframework.core.io.FileSystemResource;
         import org.springframework.mail.javamail.JavaMailSenderImpl;
         import org.springframework.mail .javamail.MimeMessageHelper;
         public class AttachedFileMail {
/**
    * 本類測試的是關於郵件中帶有附件的例子
    * @param args
    */

public static void main(String[] args) throws Exception{
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
    
     //設定mail server
    senderImpl.setHost( "192.168.1.2");
    senderImpl.setUsername( "username");
    senderImpl.setPassword( "password");
     //創建郵件消息,發送簡單郵件和html郵件的區別
    MimeMessage mailMessage = senderImpl.createMimeMessage();
     //注意這裏的boolean,等於真的時候才能嵌套圖片,在構建MimeMessageHelper時候,所給定的值是true表示啓用,        
multipart模式
    MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true);
    
     //設置收件人,寄件人
    messageHelper.setTo( "[email]sun111@163.com[/email]");    
    messageHelper.setFrom( "[email]webadmin@163.com[/email]");
    messageHelper.setSubject( "測試郵件中上傳附件!!");
     //true 表示啓動HTML格式的郵件
    messageHelper.setText("<html><head></head><body><h1>你好:附件中有關於CSS的學習資料!!        
</h1></body></html>", true);
            
    FileSystemResource file = new FileSystemResource( new File( "d:/CSS110.rar"));
     //這裏的方法調用和插入圖片是不一樣的。
    messageHelper.addAttachment( "CSS110.rar",file);
    
     //發送郵件
    senderImpl.send(mailMessage);
    
    System.out.println( "郵件發送成功....."); }         }
相關文章
相關標籤/搜索