<< 我收藏的链接(18) | 首页 | 什么是专业软件工程师? >>

Spring架构下发送带图片的html格式邮件

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("[email protected]");
     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[]{"[email protected]","[email protected]"};
         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>

 

标签 : , ,



发表评论 发送引用通报