程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 使用Spring提供的MailSender異步發送文本郵件

使用Spring提供的MailSender異步發送文本郵件

編輯:關於JAVA

在工程中經常有發送郵件的任務,如果使用JavaMail來發送郵件,用到的代碼較多,過程和細節也相對復雜,而使用Spring的MailSender能相對簡單方便些,這樣使程序員能更快捷的完成郵件發送任務。下面請看示例代碼:

注意在執行代碼前,請確認已經將activation.jar,commons-logging-1.0.4.jar,mail.jar和spring.jar載入工程。

1.抽象郵件發送類BaseMailSender代碼;

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;
   }
}

三.bean.xml

<?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");
   }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved