使用Spring提供的MailSender异步发送文本邮件

在工程中经常有发送邮件的任务,如果使用JavaMail来发送邮件,用到的代码较多,过程和细节也相对复杂,而使用Spring的MailSender能相对简单方便些,这样使程序员能更快捷的完成邮件发送任务。下面请看示例代码:


注意在执行代码前,请确认已经将activation.jar,commons-logging-1.0.4.jar,mail.jar和spring.jar载入工程。
package com.heyang.tool.mailsender;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

/** 
 * 抽象父类,定义了发送邮件的函数,取得getMailSender函数留待子类实现 
 * @author: 何杨([email protected]) 
 * @date: 2008-8-6-下午03:11:38 
 */ 
public abstract class BaseMailSender{ 
    protected String userName;
    protected String password;
    protected String stmpServer;
    protected String senderEmailAddr;
    
    /** 
     * 取得邮件发送者,留待子类实现 
     * @return 
     */ 
    protected abstract MailSender getMailSender(); 
     
    /** 
     * 发送邮件 
     * @param from :寄信人邮件地址 
     * @param to :收信人邮件地址 
     * @param subject :邮件标题 
     * @param concept :邮件内容 
     */ 
    public final void sendMail(String to,String subject,String concept){ 
        SimpleMailMessage msg=new SimpleMailMessage(); 
         
        msg.setFrom(senderEmailAddr); 
        msg.setTo(to); 
        msg.setSubject(subject); 
        msg.setText(concept); 
         
        MailSender sender=getMailSender(); 
        sender.send(msg); 
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setSenderEmailAddr(String senderEmailAddr) {
        this.senderEmailAddr = senderEmailAddr;
    }

    public void setStmpServer(String stmpServer) {
        this.stmpServer = stmpServer;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    } 
} 


二.实现类MailSenderImpl代码。
package com.heyang.tool.mailsender;

import java.util.Properties;

import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

/** 
 * 邮件发送者实现类 
 * @author: 何杨([email protected]) 
 * @date: 2008-8-6-下午03:14:49 
 */ 
public class MailSenderImpl extends BaseMailSender{         
    /** 
     * 取得邮件发送者 
     * @return 
     */ 
    protected MailSender getMailSender(){ 
        JavaMailSenderImpl sender=new JavaMailSenderImpl(); 
         
        // 邮件发送者邮箱SMTP服务器 
        sender.setHost(stmpServer); 
         
        // 邮件发送者邮箱用户 
        sender.setUsername(userName); 
         
        // 邮件发送者邮箱密码 
        sender.setPassword(password); 
         
        // 客户端认证设置 
        Properties props = new Properties(); 
         
        // 同时通过验证 
        props.put("mail.smtp.auth", "true"); 
         
        // 存储发送邮件服务器的信息 
        props.put("mail.smtp.host", stmpServer); 
         
        // 服务器端认证设置 
        sender.setJavaMailProperties(props); 
         
        return sender; 
    } 
} 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="mailSender" class="com.heyang.tool.mailsender.MailSenderImpl" singleton="false">
        <property name="userName" value="XXXXXXXXXX"/>
        <property name="password" value="XXXXXXXX"/>
        <property name="stmpServer" value="smtp.163.com"/>
        <property name="senderEmailAddr" value="[email protected]"/>
    </bean>
</beans> 

四.使用示例
package com.heyang.util;

import java.util.List;

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

import com.heyang.tool.mailsender.BaseMailSender;
import com.heyang.tool.mailsender.MailSenderImpl;

/** 
 * 异步邮件发送实用类(使用Spring的邮件发送形式) 
 * @author: 何杨([email protected]) 
 * @date: 2008-8-6-下午02:43:22 
 */ 
public final class MailUtil { 
    /** 
     * 发送邮件 
     * @param emailAddr:收信人邮件地址 
     * @param mailTitle:邮件标题 
     * @param mailConcept:邮件内容 
     */ 
    public static void sendMail(final String emailAddr, final String mailTitle, 
            final String mailConcept) { 
        Runnable thread=new Runnable(){
            public void run(){
                ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");        
                BaseMailSender sender=(MailSenderImpl)context.getBean("mailSender"); 
                sender.sendMail(emailAddr, mailTitle, mailConcept); 
            }
        };
        
        new Thread(thread).start();
    } 
    
    /**
     * 向系统维护者发送邮件
     * @param mailTitle
     * @param mailConcept
     */
    public static void sendMailToSupporters(final String mailTitle, 
            final String mailConcept){
        Runnable thread=new Runnable(){
            public void run(){
                ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");        
                SupportTeam supportTeam=(SupportTeam)context.getBean("supportTeam"); 
                BaseMailSender sender=(MailSenderImpl)context.getBean("mailSender"); 
                
                List<String> mails=supportTeam.getSupporterEmails();
                
                for(String mailAddr:mails){
                    sender.sendMail(mailAddr, mailTitle, mailConcept); 
                }
            }
        };
        
        new Thread(thread).start();
    }

    /** 
     * 测试邮件发送 
     * @param args 
     */ 
    public static void main(String[] args){ 
        MailUtil.sendMailToSupporters("title_spring", "concept_spring"); 
    } 
}

你可能感兴趣的:(mailsender,sping发送邮件)