《pro Spring》学习笔记之使用Sprin+James邮件服务器发送复杂类型邮件

所谓可替换的复杂类型邮件,就是不简单的html邮件和纯文本邮件而是在html中再加入内嵌图片

实现的大体思路和这篇文章http://blog.csdn.net/daryl715/archive/2007/09/17/1788481.aspx类似,不过我们要更换一下结果,一般的可替换邮件是
wrapper
       ----HTML
       ----TEXT

而加入内嵌图片则变为
wrapper
       ----wrapper
              ----HTML
              ----TEXT
       ----TEXT

 

<? xml version="1.0" encoding="UTF-8" ?>
< beans
    
xmlns ="http://www.springframework.org/schema/beans"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd" >
< bean  id ="messageSender"  class ="ch15.ComplesChangeMail.MessageSender" >
  
< property  name ="sender" >
    
< ref  bean ="sender" />
  
</ property >
</ bean >

< bean  id ="sender"  class ="org.springframework.mail.javamail.JavaMailSenderImpl" >
  
< property  name ="host" >
    
< value > king.cn </ value >
  
</ property >  
  
< property  name ="username" >
    
< value > gaoxiang </ value >
  
</ property >
  
< property  name ="password" >
    
< value > gaoxiang </ value >
  
</ property >
  
< property  name ="javaMailProperties" >
        
< props >
           
< prop  key ="mail.smtp.auth" > true </ prop >
           
< prop  key ="mail.smtp.timeout" > 10000 </ prop >
        
</ props >
  
</ property >
</ bean >

< bean  id ="messagePreparator"  class ="ch15.ComplesChangeMail.MessagePreparator" >
  
< property  name ="to" >
    
< value > [email protected] </ value >
  
</ property >
  
< property  name ="from" >
    
< value > [email protected] </ value >
  
</ property >
  
< property  name ="subject" >
    
< value > html/text mime mail </ value >
  
</ property >
  
< property  name ="htmlContent" >
   
< value > <![CDATA[ <html><head><body>this is a pic mail<img src="cid:abc"></body></head></html> ]]> </ value >
  
</ property >
  
< property  name ="textContent" >
    
< value > this is a text mail </ value >
  
</ property >
  
< property  name ="dataHandler" >
    
< ref  bean ="dataHandler" />
  
</ property >
</ bean >
<!--  注意这里使用javax.activation包的FileDataSource  -->
< bean  id ="dataHandler"  class ="javax.activation.DataHandler" >
   
< constructor-arg >
     
< bean  class ="javax.activation.FileDataSource" >
      
< constructor-arg >
        
< bean  class ="java.io.File" >
          
< constructor-arg >
            
< value > ./src/ch15/ComplesChangeMail/mail.jpg </ value >
          
</ constructor-arg >
        
</ bean >
      
</ constructor-arg >
    
</ bean >
   
</ constructor-arg >
</ bean >
</ beans >

 

 

package  ch15.ComplesChangeMail;


import  javax.activation.DataHandler;
import  javax.mail.BodyPart;
import  javax.mail.Message;
import  javax.mail.internet.InternetAddress;
import  javax.mail.internet.MimeBodyPart;
import  javax.mail.internet.MimeMessage;
import  javax.mail.internet.MimeMultipart;

import  org.springframework.mail.javamail.MimeMessagePreparator;


public   class  MessagePreparator  implements  MimeMessagePreparator  {
    
private String to;
    
private String from;
    
private String htmlContent;
    
private String textContent;
    
private String subject;
    
private DataHandler dataHandler;
    
public DataHandler getDataHandler() {
        
return dataHandler;
    }

    
public void setDataHandler(DataHandler dataHandler) {
        
this.dataHandler = dataHandler;
    }

    
public String getHtmlContent() {
        
return htmlContent;
    }

    
public void setHtmlContent(String htmlContent) {
        
this.htmlContent = htmlContent;
    }

    
public String getTextContent() {
        
return textContent;
    }

    
public void setTextContent(String textContent) {
        
this.textContent = textContent;
    }

    
public String getSubject() {
        
return subject;
    }

    
public void setSubject(String subject) {
        
this.subject = subject;
    }

    
public String getTo() {
        
return to;
    }

    
public void setTo(String to) {
        
this.to = to;
    }

    
public String getFrom() {
        
return from;
    }

    
public void setFrom(String from) {
        
this.from = from;
    }


    
public void prepare(MimeMessage msg) throws Exception {
        
//设置邮件信息
        msg.addFrom(InternetAddress.parse(from));
        msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        msg.setSubject(subject);
        
        
//创建multipart/alternative的包装器
        MimeMultipart mimeMultipart=new MimeMultipart();
        msg.setContent(mimeMultipart);
        
        
//创建text的邮件体
        BodyPart plainText=new MimeBodyPart();
        plainText.setText(textContent);
        mimeMultipart.addBodyPart(plainText);
        
        
//创建第二个multipart/alternative的包装器,封装html和内嵌图片
        BodyPart relate=new MimeBodyPart();
        MimeMultipart mr
=new MimeMultipart();
        relate.setContent(mr);
        mimeMultipart.addBodyPart(relate);
        
        
//创建html内容和内嵌图片资源
        BodyPart html=new MimeBodyPart();
        html.setContent(htmlContent,
"text/html");
        mr.addBodyPart(html);
        
        BodyPart img
=new MimeBodyPart();
        img.setHeader(
"Content-ID""abc");
        img.setDisposition(
"inline");
        img.setDataHandler(dataHandler);
        mr.addBodyPart(img);
        
        
    }

     
}

 

 

package  ch15.ComplesChangeMail;

import  org.springframework.mail.javamail.JavaMailSenderImpl;

public   class  MessageSender  {
       
        
protected JavaMailSenderImpl sender;
        
public void sendMessage(MessagePreparator msgP){
            sender.send(msgP);
        }

        
public JavaMailSenderImpl getSender() {
            
return sender;
        }

        
public void setSender(JavaMailSenderImpl sender) {
            
this.sender = sender;
        }

       
}

 

测试代码:

 

别忘了再classpath下加入一个名为mail.jpg图片

package  ch15.ComplesChangeMail;

import  java.io.File;

import  org.springframework.context.ApplicationContext;
import  org.springframework.context.support.ClassPathXmlApplicationContext;

public   class  ComplesChangeTest  {


    
public static void main(String[] args) throws Exception {
        ApplicationContext context 
= new ClassPathXmlApplicationContext(
                
"ch15/ComplesChangeMail/applicationContext.xml");
        MessagePreparator messagePreparator 
= (MessagePreparator) context.getBean("messagePreparator");
        MessageSender messageSender
=(MessageSender)context.getBean("messageSender");
        
for (int i = 0; i < 10; i++{
            messageSender.sendMessage(messagePreparator);
            System.out.println(
"成功");
            Thread.sleep(
2000); //发送后延时2秒钟
        }

    }

}

 

别忘了再classpath下加入一个名为mail.jpg

你可能感兴趣的:(《pro Spring》学习笔记之使用Sprin+James邮件服务器发送复杂类型邮件)