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

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

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

    隨筆-153  評論-235  文章-19  trackbacks-0
        由于項目里要發(fā)郵件,用了spring 和 velocity 模板。寫下來以后可以看,好記性不如博客。
    版本說明下:
    spring 2.0.6
    velocity 1.5
    javamail 用 spring/lib下的

    1.封裝郵件信息類:
    package com.chenlb.mail;

    import java.util.Map;

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

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.velocity.app.VelocityEngine;
    import org.springframework.mail.MailException;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessageHelper;
    import org.springframework.ui.velocity.VelocityEngineUtils;

    /**
     * 郵件發(fā)送器
     * @作者 chenlb
     * @創(chuàng)建時間 2007-7-28 下午03:35:31 
     
    */
    public class VelocityTemplateMailMessage {

        
    protected final Log logger = LogFactory.getLog(getClass());
        
        
    private JavaMailSender javaMailSender;
        
    private VelocityEngine velocityEngine;
        
    private String from;
        
    private String title;
        
    private String encoding;
        
    private String templateLocation;
        
    private String[] toEmails;
        
    private Map model;


        
    public boolean send() {
            MimeMessage msg 
    = javaMailSender.createMimeMessage();
            MimeMessageHelper helper 
    = new MimeMessageHelper(msg);
            
            
    try {
                helper.setFrom(from);
                helper.setSubject(title);
                helper.setTo(toEmails);
                helper.setText(getMessage(), 
    true);   //如果發(fā)的不是html內(nèi)容去掉true參數(shù)
                javaMailSender.send(msg);
                
            } 
    catch (MessagingException e) {
                
    // TODO 自動生成 catch 塊
                if(logger.isWarnEnabled()) {
                    logger.warn(
    "郵件信息導常! 郵件標題為: "+title);
                }
                
    return false;
                
    //e.printStackTrace();
            } catch (MailException me) {
                
    // TODO: handle exception
                if(logger.isWarnEnabled()) {
                    logger.warn(
    "發(fā)送郵件失敗! 郵件標題為: "+title);
                }
                
    return false;
            }
            
    return true;    
        }
        
        
        
    /**
         * 郵件模板中得到信息
         * 
    @return 返回特發(fā)送的內(nèi)容
         
    */
        
    private String getMessage() {
            
    return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, 
                    templateLocation, encoding, model);
        }

        
    private String[] createToEmail(String to) {
            
    return new String[] {to};
        }
        
        
    public void setToEmail(String to) {
            setToEmails(createToEmail(to));
        }
        
        
    public void setJavaMailSender(JavaMailSender javaMailSender) {
            
    this.javaMailSender = javaMailSender;
        }
        
        
    public void setVelocityEngine(VelocityEngine velocityEngine) {
            
    this.velocityEngine = velocityEngine;
        }

        
    public void setEncoding(String encoding) {
            
    this.encoding = encoding;
        }

        
    public void setModel(Map model) {
            
    this.model = model;
        }

        
    public void setTemplateLocation(String templateLocation) {
            
    this.templateLocation = templateLocation;
        }

        
    public void setTitle(String title) {
            
    this.title = title;
        }

        
    public void setToEmails(String[] toEmails) {
            
    this.toEmails = toEmails;
        }

        
    public void setFrom(String from) {
            
    this.from = from;
        }

        
    public String getTemplateLocation() {
            
    return templateLocation;
        }
    }

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

    <!-- 屬性文件加載 -->
        
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            
    <property name="locations">
              
    <list>
                  
    <value>classpath:mail.properties</value>
              
    </list>
          
    </property>
        
    </bean>

    <!-- 郵件發(fā)送器 -->
        
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
            
    <property name="host" value="${mail.host}" />
            
    <property name="username" value="${mail.username}" />
            
    <property name="password" value="${mail.password}" />
            
    <property name="defaultEncoding" value="UTF-8"></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="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
            
    <property name="resourceLoaderPath" value="classpath:velocity"></property>
        
    </bean>
        
        
    <bean id="templateMail" class="com.chenlb.mail.VelocityTemplateMailMessage">
            
    <property name="javaMailSender" ref="mailSender"></property>
            
    <property name="from" value="${mail.from}"></property>
            
    <property name="encoding" value="UTF-8"></property>
            
    <property name="templateLocation" value="test.vm"></property>
            
    <property name="velocityEngine" ref="velocityEngine"></property>
            
    <property name="title" value="wwww.tkk7.com/chenlb"></property>
        
    </bean>
        
    </beans>
    說明:模板文件放到classpath的velocity目錄下,可自行改。

    3.發(fā)送者郵件信息,mail.properties(classpath位置):
    mail.from=yourname@126.com
    mail.host
    =smtp.126.com
    mail.password
    =yourpassword
    mail.smtp.auth
    =true
    mail.smtp.timeout
    =25000
    mail.username
    =yourname

    4.模板文件,text.vm(classpath的velocity目錄下):
    你好!${me} 這是模板生成的郵件。

    5.使用:
    VelocityTemplateMailMessage vtmm = (VelocityTemplateMailMessage) context.getBean("templateMail");
    Map
    <String, String> data = new HashMap<String, String>();
    data(
    "me","yourname");
    vtmm.setModel(data);
    vtmm.setToMail(
    "yourOtherMail@163.com");
    vtmm.setTitle(
    "mail with veloctiy and spring");
    vtmm.send();

     

    看下是否收到郵件了。^_^

    posted on 2007-08-21 19:43 流浪汗 閱讀(3333) 評論(6)  編輯  收藏 所屬分類: OpenSource

    評論:
    # re: send email with velocity and spring 完整示例[未登錄] 2007-11-16 20:41 | silent

    能夠完整打包發(fā)給我嗎..非常謝謝啊...  回復  更多評論
      
    # re: send email with velocity and spring 完整示例 2008-07-03 10:41 | andyjames
    學習了 多多指教  回復  更多評論
      
    # re: send email with velocity and spring 完整示例 2009-09-04 14:03 | Wangzi
    博主不厚道,不提供下載

    我的郵箱是yhjhoo@163.com,麻煩博主發(fā)我一份吧  回復  更多評論
      
    # re: send email with velocity and spring 完整示例 2009-09-20 13:47 | 豆?jié){包子
    非常感謝,正好需要。
    測試用Google郵箱發(fā)送失敗,換了QQ郵箱就成功了。  回復  更多評論
      
    # re: send email with velocity and spring 完整示例 2010-06-10 16:34 | dylan
    能給我也傳一份么?
    hpsh2006@163.com  回復  更多評論
      
    # re: send email with velocity and spring 完整示例 2010-12-27 12:56 | 杰杰
    @dylan
    1021938637@qq.com ^^謝發(fā)一下  回復  更多評論
      
    主站蜘蛛池模板: 亚洲av无码成人精品区| 免费高清av一区二区三区| 2048亚洲精品国产| 国产精品亚洲а∨无码播放不卡 | 久久99精品视免费看| 亚洲精品国产va在线观看蜜芽| 麻豆亚洲AV成人无码久久精品 | 亚洲中文无码卡通动漫野外| 国产卡一卡二卡三免费入口| 亚洲AV一二三区成人影片| 歪歪漫画在线观看官网免费阅读| 亚洲精品视频在线观看视频| 毛片免费全部播放无码| 亚洲免费一级视频| 久久w5ww成w人免费| 亚洲女人影院想要爱| 欧美a级成人网站免费| 亚洲精品无码高潮喷水A片软| 国产精品无码免费视频二三区| 美女扒开屁股让男人桶爽免费| 亚洲日韩在线中文字幕第一页| 国产97视频人人做人人爱免费| 综合亚洲伊人午夜网| 国产一区二区三区免费| 亚洲视频免费在线播放| 成人免费无码大片A毛片抽搐| 日韩精品亚洲专区在线影视| 亚洲精品视频免费观看| 无码AV片在线观看免费| 456亚洲人成在线播放网站| 日韩免费福利视频| 免费无码作爱视频| 国产精品亚洲自在线播放页码 | 欧美好看的免费电影在线观看| 亚洲aⅴ无码专区在线观看春色| 亚洲&#228;v永久无码精品天堂久久 | 亚洲一区二区三区国产精品无码| 午夜一级毛片免费视频| 中国极品美軳免费观看| 亚洲人成网站在线观看播放动漫| 免费人成视频在线观看视频|