<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 78,  comments - 48,  trackbacks - 0
    1.下載spring及velocity類庫,

    email配置文件:mail.properties:
    mail.default.from=jfishsz@163.com
    mail.host=smtp.163.com
    mail.username=xxxxxx
    mail.password=xxxxxx
    mail.smtp.auth=true
    mail.smtp.timeout=25000

    spring配置文件:applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
      <!-- For mail settings and future properties files -->
      <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:mail.properties</value>
            </list>
        </property>
      </bean>
      <bean id="mailSender"
        class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host">
            <value>${mail.host}</value>
        </property>
        <property name="username">
            <value>${mail.username}</value>
        </property>
        <property name="password">
            <value>${mail.password}</value>
        </property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
                <prop key="mail.smtp.timeout">
                  ${mail.smtp.timeout}
                </prop>
            </props>
        </property>
      </bean>
      <bean id="mailMessage"
        class="org.springframework.mail.SimpleMailMessage"
        singleton="false">
        <property name="from">
            <value>${mail.default.from}</value>
        </property>
       
      </bean>
      <bean id="sendMail" class="net.pms.email.SendMail">
        <property name="mailSender" ref="mailSender" />
        <property name="message" ref="mailMessage" />
        <property name="velocityEngine" ref="velocityEngine" />
      </bean>
      <!-- Configure Velocity for sending e-mail -->
      <bean id="velocityEngine"
        class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="velocityProperties">
            <props>
                <prop key="resource.loader">class</prop>
                <prop key="class.resource.loader.class">
                  org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
                </prop>
                <prop key="velocimacro.library"></prop>
            </props>
        </property>
      </bean>
    </beans>
    velocity模板文件:accountCreated.vm:
    ${message}

    Username: ${username}
    Password: ${Password}

    Login at: ${applicationURL}

    2.實現類
    SendMail.java
    package net.pms.email;

    import java.util.Map;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.velocity.app.VelocityEngine;
    import org.apache.velocity.exception.VelocityException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.ui.velocity.VelocityEngineUtils;

    public class SendMail {
      protected static final Log log = LogFactory.getLog(SendMail.class);

      private MailSender mailSender;

      private SimpleMailMessage message;

      private VelocityEngine velocityEngine;

      public void setVelocityEngine(VelocityEngine velocityEngine) {
        this.velocityEngine = velocityEngine;
      }

      public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
      }

      public void setMessage(SimpleMailMessage message) {
        this.message = message;
      }

      public void sendEmail(Map model) {
        message.setTo("jfishsz@163.com");
        message.setSubject("subject");
        String result = null;
        try {
            result = VelocityEngineUtils.mergeTemplateIntoString(
                  velocityEngine, "accountCreated.vm", model);
        } catch (VelocityException e) {
            e.printStackTrace();
        }
        message.setText(result);
        mailSender.send(message);
      }
    }
    測試類:SendMailTest.java
    package net.pms.email;

    import java.util.HashMap;
    import java.util.Map;

    import junit.framework.TestCase;

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

    public class SendMailTest extends TestCase {
      String[] paths = { "config/applicationContext*.xml" };

      ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);

      SendMail s = (SendMail) ctx.getBean("sendMail");

      protected void setUp() throws Exception {
        super.setUp();
      }

      protected void tearDown() throws Exception {
        super.tearDown();
      }

      /*
      * Test method for 'net.pms.email.SendMail.sendEmail(Map)'
      */
      public void testSendEmail() {
        Map model = new HashMap();
        model.put("message", "msg");
        model.put("username", "jack");
        model.put("Password", "666666");
        model.put("applicationURL", "www.163.com");
        s.sendEmail(model);
      }

    }
    posted on 2006-03-13 10:35 黑咖啡 閱讀(1749) 評論(1)  編輯  收藏 所屬分類: Velocity

    FeedBack:
    # re: spring+velocity自動發送郵件
    2009-11-06 10:58 | geek87
    相當好,謝謝。。。  回復  更多評論
      

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(2)

    隨筆分類(67)

    文章分類(43)

    Good Article

    Good Blogs

    Open Source

    最新隨筆

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 性色av无码免费一区二区三区| 6080午夜一级毛片免费看| 性做久久久久免费看| 亚洲精品一二三区| 91嫩草国产在线观看免费| 亚洲一区二区三区免费观看| 一色屋成人免费精品网站| 亚洲kkk4444在线观看| 拨牐拨牐x8免费| 爱情岛论坛免费视频| 亚洲Av无码乱码在线播放| www永久免费视频| 亚洲av无码国产精品夜色午夜 | 久久不见久久见中文字幕免费 | 亚洲中文字幕无码av永久| 好吊妞在线新免费视频| 福利片免费一区二区三区| 亚洲中文字幕无码专区 | 日本最新免费网站| 亚洲avav天堂av在线网爱情| 午夜a级成人免费毛片| 老司机午夜免费视频| 亚洲国产精彩中文乱码AV| 久久国产精品成人片免费| 国产午夜亚洲精品| 亚洲精品综合久久| 91香焦国产线观看看免费| 亚洲精品无码日韩国产不卡av| 亚洲第一黄片大全| 香港a毛片免费观看| 亚洲欧美日韩中文无线码 | 免费成人在线视频观看| 亚洲乱码在线播放| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲成人福利在线| 四虎影视永久免费观看网址| 最近免费mv在线观看动漫| 亚洲人成www在线播放| 亚洲视频在线精品| 思思re热免费精品视频66| 一个人免费观看www视频|