posts - 495,  comments - 11,  trackbacks - 0

1.准备工作

除了必要spring的支持外,还需要引入两个jar包,分别是activation.jar和mail.jar

2.代码

SimpleHtmlMailSender.java

/*
* @(#)SimpleHtmlMailSender.java 2009-7-28
*
* Copyright (c) 2009 by jadmin. All Rights Reserved.
*/

package com.jsoft.s2sh.util.mail;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
* 用于发送简单的HTML文本邮件
*
* @author <a href="mailto:jadmin ON 126.com">jadmin</a>
* @version $Id: SimpleHtmlMailSender.java 2009-7-28 上午01:15:35$
* @see <a href="http://hi.baidu.com/jadmin">myblog</a>
*/
public class SimpleHtmlMailSender {

protected JavaMailSender sender;

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

public void sendMessage(String message,String to, String from, String subject, String encoding) throws MessagingException {
   MimeMessage msg = sender.createMimeMessage();
   MimeMessageHelper helper = new MimeMessageHelper(msg, true, encoding);
   helper.setTo(to);
   helper.setFrom(from);
   helper.setSubject(subject);
   helper.setText(message, true);

   sender.send(msg);
}

public static void main(String[] args) throws MessagingException {
   ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-mail.xml");
   String to = "etxp on   qq.com";
   String from = "etxp on 163.com";
   String subject = "感谢您对本站的关注,请激活您的帐号";
  
   String message = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>哈哈!"
    + "</a></h1></body></html>";

   SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx.getBean("mailSender");
   sender.sendMessage(message, to, from, subject, "GB2312");

}

}

3.配置

applicationContext.xml(将此文件之余classpath下)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
   <property name="host">
    <value>smtp.163.com</value>
   </property>
   <property name="username">
    <value>etxp</value>
   </property>
   <property name="password">
    <value>**********</value>
   </property>
   <property name="javaMailProperties">
    <props>
     <prop key="mail.smtp.auth">true</prop>
    </props>
   </property>
</bean>

<bean id="mailSender" class="com.jsoft.s2sh.util.mail.SimpleHtmlMailSender">
   <property name="sender">
    <ref bean="javaMailSender" />
   </property>
</bean>

</beans>

4.运行

posted on 2009-07-28 04:29 jadmin 阅读(86) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: