Velocity模板中的参数绑定问题,用于使用Spring MVC发送邮件

时间:2021-10-22 18:17:40

I am using velocity template for sending e-mail in my Spring MVC application , but the parameters I am sending to the template are not binding in the resulting email. Here what I am doing:

我正在使用速度模板在我的Spring MVC应用程序中发送电子邮件,但是我发送到模板的参数在生成的电子邮件中没有绑定。我在做什么:

Dependency in my pom.xml for Velocity

我的pom.xml中Velocity的依赖关系

<!-- velocity -->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-tools</artifactId>
    <version>2.0</version>
</dependency>

Configuration in my spring-config.xml

在我的spring-config.xml中配置

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="username" value="abc@gmail.com" />
<property name="password" value="fakepass" />
<property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.socketFactory.port">465</prop>
        <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
        <prop key="mail.smtp.socketFactory.fallback">false</prop>
        <prop key="mail.smtp.quitwait">false</prop>
        <prop key="mail.debug">true</prop>
    </props>
</property>
</bean>

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
 <value>
    resource.loader=class
    class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
 </value>
</property>
</bean>

Here is the velocity template (.vm) file I have:

这是我的速度模板(.vm)文件:

<html>
    <body>
        <h1 class="note-info"><strong>Forgot your application password?</strong></h1>
        <p>
            Dear ${model.userName}, <br />
            We have received a request to reset your password.
        </p>
        <p>
            To reset your password, Click on the link below or Copy and Paste the URL into your browser.<br />
            The link will be valid for next 24 hours.
        </p>
        <p>
            ${model.link}
        </p>
        <p>
            Best regards,<br />
            The AppTeam
         <br />

        </p>
    </body>
</html>

And here is the Java code sending the email

这是发送电子邮件的Java代码

public void sendEmailNotification(final EmailNotification emailNotofication)throws Exception {
  MimeMessagePreparator preparator = new MimeMessagePreparator() {
        @SuppressWarnings({ "unchecked" })
        public void prepare(MimeMessage mimeMessage) throws Exception {
             MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
             message.setTo(emailNotofication.getReceiversArr());
             if(emailNotofication.getCcArr() != null)
                 message.setCc(emailNotofication.getCcArr());
             if(emailNotofication.getBccArr() != null)
                 message.setBcc(emailNotofication.getBccArr());
             message.setFrom(new InternetAddress(emailNotofication.getSender()) );
             message.setSubject(emailNotofication.getSubject());
             message.setSentDate(new Date());
             Map<String,Object> model = new HashMap<String, Object>();
             if(emailNotofication.getParams() != null)
             {
                 Set<Map.Entry<String, Object>> entrySet = emailNotofication.getParams().entrySet();
                 for(Map.Entry<String, Object> entry : entrySet)
                 {
                     model.put(entry.getKey(), entry.getValue());
                 }
             }
             String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,emailNotofication.getMailTemplate(),"UTF-8",model);
             message.setText(text, true);
          }
   };
   mailSender.send(preparator);         
}

I am adding the variables in model as key value pair in mergeTemplateIntoString(), but still the in the resulting email I am getting ${model.userName} and ${model.link}, those are not replaced by the value passed in model.

我在模型中添加变量作为mergeTemplateIntoString()中的键值对,但仍然在生成的电子邮件中我得到$ {model.userName}和$ {model.link},这些不会被模型中传递的值替换。

1 个解决方案

#1


I fond the answer , posting the same as it may help some-one else. The problem is in .vm file. Need to use just ${link} instead of ${model.link}.

我喜欢这个答案,发布相同的内容,因为它可能会帮助其他人。问题出在.vm文件中。只需使用$ {link}而不是$ {model.link}。

#1


I fond the answer , posting the same as it may help some-one else. The problem is in .vm file. Need to use just ${link} instead of ${model.link}.

我喜欢这个答案,发布相同的内容,因为它可能会帮助其他人。问题出在.vm文件中。只需使用$ {link}而不是$ {model.link}。