hi! | 关于
专注于Java技术、开源项目、项目管理
今天 | RSS | RDF | Atom | 其它
 
高级搜索

标签 - 分类 : 全部 | UNIX | 随笔 | 数据库 | Java技术 | 网摘文章

applicationContext-service.xml增加

    <bean id="mailEngine" class="com.sunrise.psmis.service.MailEngine">
        <property name="mailSender" ref="mailSender"/>
        <property name="velocityEngine" ref="velocityEngine"/>
    </bean>

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.timeout">25000</prop>
            </props>
        </property>       
        <property name="host" value="${mail.host}"/>
        <property name="username" value="${mail.username}"/>
        <property name="password" value="${mail.password}"/>
    </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>

    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage" singleton="false">
        <property name="from" value="${mail.default.from}"/>
    </bean>

mailEngine.java的Sendmail方法:

    public void sendMessage(String[] emailAddresses,
            ClassPathResource resource,
            String subject, String attachmentName,String templateName,
            Map model)
 throws MessagingException {
  MimeMessage message =
  ((JavaMailSenderImpl) mailSender).createMimeMessage();
  
  // use the true flag to indicate you need a multipart message
  MimeMessageHelper helper = new MimeMessageHelper(message, true,"GBK");
     String bodyText = null;
 
     try {
      bodyText =
             VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,
                                                         templateName,"GBK", model);
     } catch (VelocityException e) {
         e.printStackTrace();
     }
  
  helper.setTo(emailAddresses);
  helper.setText(bodyText,true);//true为使用html格式
  helper.setSubject(subject);
     helper.setFrom("liupinghua@revenco.com");
     helper.addInline("identifier", resource);
 
     message.setHeader("Content-ID", "identifier");
 //    message.setFileName(MimeUtility.encodeText(fds.getName(), "UTF-8", "B"));
  
  helper.addAttachment(attachmentName, resource);
  
  ((JavaMailSenderImpl) mailSender).send(message);
 }

发送代码:

         String emailAddresses[] = new String[]{"liupinghua@revenco.com","zhangjiade@revenco.com"};
         ClassPathResource resource = new ClassPathResource("images/404.jpg");
         String subject="电费通知单";
         String attachmentName="identifier";
         HashMap model = new HashMap();
            model.put("content","电费通知单详细内容");
         mailEngine.sendMessage(emailAddresses, resource, subject, attachmentName, "test.vm", model)  ;       

test.vm脚本:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>

<!-- HTTP 1.1 -->

<meta http-equiv="Cache-Control" content="no-store"/>
<!-- HTTP 1.0 -->
<meta http-equiv="Pragma" content="no-cache"/>
<!-- Prevents caching at the Proxy Server -->
<meta http-equiv="Expires" content="0"/>      
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
</head>
<font size="20" color="#ff0000">测试</font><br/>
<img src='cid:identifier' alt="历史电量柱状图"/><br/>
${content}

</body>
</html>


标签 :




置评

标题
正文
HTML : b, i, blockquote, br, p, pre, a href="", ul, ol, li
姓名
电邮地址
网站
记住我 是  否 

电邮地址将不会发表在公开网页上,您留下的电邮地址仅用于本文有新评论时通知您(以后可以随时拿掉).

回接到 http://www.searchfull.net:80/blog/addTrackBack.action?entry=1209895326965