<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.實現(xiàn)類
    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 黑咖啡 閱讀(1748) 評論(1)  編輯  收藏 所屬分類: Velocity

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

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     

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

    留言簿(2)

    隨筆分類(67)

    文章分類(43)

    Good Article

    Good Blogs

    Open Source

    最新隨筆

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 日韩高清在线免费观看| 午夜视频在线在免费| 亚洲A∨无码无在线观看| 国产一级一毛免费黄片| 最新国产AV无码专区亚洲 | 在线观看人成视频免费| 亚洲日韩久久综合中文字幕| 妞干网在线免费视频| 蜜桃传媒一区二区亚洲AV| 日韩高清在线高清免费| 青青草97国产精品免费观看| 亚洲乱亚洲乱少妇无码| 18禁在线无遮挡免费观看网站| 亚洲人成网站影音先锋播放| h视频在线观看免费完整版| 四虎亚洲精品高清在线观看| 国产精品黄页在线播放免费| 一级黄色毛片免费看| 亚洲春色在线视频| 99久久99这里只有免费费精品| 一本天堂ⅴ无码亚洲道久久| 国产18禁黄网站免费观看| 9久热精品免费观看视频| 亚洲AV天天做在线观看| 成熟女人牲交片免费观看视频| 久久久久久亚洲av无码蜜芽| 亚洲成a人片在线播放| 99在线免费观看视频| 亚洲综合精品成人| 久久亚洲2019中文字幕| 1000部禁片黄的免费看 | 日韩在线观看免费| 亚洲不卡av不卡一区二区| 亚洲高清中文字幕免费| 黄页网址在线免费观看 | 亚洲熟妇无码一区二区三区| 亚洲日韩国产成网在线观看| 最近高清中文字幕免费| 全部一级一级毛片免费看| 亚洲免费视频网站| 全部免费毛片在线|