获取速度模板资源未找到异常

时间:2019-02-14 11:12:40

标签: javamail velocity email-templates

我正在使用Java 1.8,Java Mail 1.5.5和Velocity 1.7。我已经成功实现了电子邮件发送,而没有集成速度模板。但是,当我集成Velocity时,似乎无法正确找到模板。 Unable to find resource 'templates/email-template.vm'

电子邮件发件人代码段:

public class Utils {
    MimeMessage msg = new MimeMessage(session);
    msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
    msg.addHeader("format", "flowed");
    msg.addHeader("Content-Transfer-Encoding", "8bit");
    msg.setSubject(subject, "UTF-8");
    BodyPart body = new MimeBodyPart();

    VelocityEngine ve = new VelocityEngine();
    ve.init();
    Template t = ve.getTemplate("templates/email-template.vm");
    VelocityContext context = new VelocityContext();
    context.put("message", message);
    StringWriter out = new StringWriter();
    t.merge( context, out );

    body.setContent(out.toString(),"text/html");
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(body);
    body = new MimeBodyPart();
    multipart.addBodyPart(body);
    msg.setContent(multipart,"text/html");
    msg.setSentDate(new Date());
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
    Transport.send(msg);
}

文件位置与图片中的位置相同:

file locations

当我在这里实现API时,我没有webapps文件夹来保存模板。知道为什么Velocity无法检测到此文件位置吗?另外,即使我给出了模板文件的绝对路径,这也不起作用。

email-template.vm:

<html>
 <body style="background-color: #F8F8F8; margin: 0; padding: 0;">
   <div style="width: 630px; margin: 0 auto;">
    <table style="border-collapse: collapse; border-spacing: 0; width: 100%;">
     <tbody>
       <tr>
        <td height="20" style="background-color:#F8F8F8"></td>
       </tr>
       <tr>
        <td style="background-color: #85929E;font-size: 18px; padding: 10px; border-radius: 5px 5px 0 0; border-bottom: 1px solid #cccccc">
            An error occurred while creating entities
        </td>
       </tr>
       <tr>
        <td style="padding: 10px; background-color: #ffffff;  border: 1px solid #85929E; ">
            <p>
                $message
            </p>

        </td>
       </tr>
       <tr>
        <td style="background-color: #85929E; font-size: 12px; text-align: center; padding: 10px; border-radius: 0 0 5px 5px; border-top: 1px solid #cccccc " align="center">
            Appitizer Inc
        </td>
       </tr>
       <tr>
        <td height="60"  style="background-color:#F8F8F8"></td>
       </tr>
    </tbody>
  </table>
 </div>
</body>
</html>

0 个答案:

没有答案