《pro Spring》学习笔记之使用Spring+James邮件服务器发送带有附件的HTML邮件

 

首先,请参考这一片文章,配置好James邮件服务器

 http://blog.csdn.net/daryl715/archive/2007/09/14/1784869.aspx

发送带有附件的html邮件,和发送内嵌图片的邮件类型,唯独不同的是需要使用MimiMessageHelper的addAttachment方法

抽象类:

 

package  ch15.SimpleFileMail;

import  org.springframework.mail.javamail.JavaMailSenderImpl;

public   abstract   class  AbstractMessageSender  {
        
protected String to;
        
protected String from;
        
protected String subject;
        
protected String text;
        
protected JavaMailSenderImpl sender;
        
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 String getSubject() {
            
return subject;
        }

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

        
public String getText() {
            
return text;
        }

        
public void setText(String text) {
            
this.text = text;
        }

        
public JavaMailSenderImpl getSender() {
            
return sender;
        }

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

       
}

实现类:

 

package  ch15.SimpleFileMail;

import  javax.mail.MessagingException;
import  javax.mail.internet.MimeMessage;

import  org.springframework.core.io.FileSystemResource;
import  org.springframework.mail.javamail.MimeMessageHelper;

public   class  SimpleFileMessageSender  extends  AbstractMessageSender  {
    
private MimeMessageHelper helper;
    
private MimeMessage msg;
    
private FileSystemResource img;
    
    
public FileSystemResource getImg() {
        
return img;
    }


    
public void setImg(FileSystemResource img) {
        
this.img = img;
    }


    
public void sendMessage(){
        msg
=sender.createMimeMessage();
        
        
try {
            helper
=new MimeMessageHelper(msg,true); //使用内嵌资源时,需要额外配置一个true参数
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(text,
true);//true表示text/html格式邮件
            helper.addAttachment("mail.jpg", img);  //加入一个名为abc的内嵌资源
            System.out.println(img);
        }
catch (Exception e) {
            System.out.println(e.getMessage());
        }

        
        sender.send(msg);
    }

        
}

 

配置文件:

 

<? 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 ="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 ="messageSender"  class ="ch15.SimpleFileMail.SimpleFileMessageSender" >
  
< property  name ="sender" >
    
< ref  bean ="sender" />
  
</ property >
  
< property  name ="to" >
    
< value > [email protected] </ value >
  
</ property >
  
< property  name ="from" >
    
< value > [email protected] </ value >
  
</ property >
  
< property  name ="subject" >
    
< value > html mime mail </ value >
  
</ property >
  
<!--  此处用cid:abc  表示实用的一个内嵌资源,名称为abc  -->
  
< property  name ="text" >
    
< value > <![CDATA[ <html><head><body>this is a file mail</body></head></html> ]]> </ value >
  
</ property >
  
< property  name ="img" >
    
< bean  class ="org.springframework.core.io.FileSystemResource" >
      
< constructor-arg >
        
< bean  class ="java.io.File" >
          
< constructor-arg >
            
< value > ./src/ch15/SimpleFileMail/mail.jpg </ value >
          
</ constructor-arg >
        
</ bean >
      
</ constructor-arg >
    
</ bean >
  
</ property >
</ bean >


</ beans >

 

测试代码:

 

package  ch15.SimpleFileMail;

import  java.io.File;

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

public   class  SimpleFileTest  {


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

    }

}

你可能感兴趣的:(《pro Spring》学习笔记之使用Spring+James邮件服务器发送带有附件的HTML邮件)