1

我知道这个问题类似于这里的这个问题。但是,我在 Spring Framework 中使用了不同的电子邮件方法,因此添加了一个新问题。

我有一个图像存储在我机器的 /tmp/upload/image 路径中。

从上面提到的同一问题中获取详细信息,我在 Velocity Template 中添加了以下详细信息:

<div>
   <img src="cid:${cid}" class="iconDetails" />                                           
</div>

我的电子邮件实用程序执行以下操作:

@Async
public void sendOrderMail(String emailAddress, String templateName, String link) {
    MimeMessage mimeMessage = sender.createMimeMessage();
    MimeMessageHelper helper;
    try {
        helper = new MimeMessageHelper(mimeMessage, false, "utf-8");

        helper.setTo(emailAddress);
        helper.setFrom("no-reply@abc.com");
        helper.setSubject(emailTitle);
        helper.setText("");

        mimeMessage.setContent(templateName, "text/html");
    }  catch (Exception e) {
        log.info("Error in sending mail : " + e.getMessage());
    }
    sender.send(mimeMessage);
    log.info("Mail successfully sent to : " + emailAddress);
}

这是我的服务层,我将属性绑定到我应该发送到我的 Velocity 模板的 HashMap :

    double totalPayable = total + totalServiceCharge;

    Organization org = orgRepo.findOne(order.getEvent().getOrgId());
    User user = userRepo.findUserByEmail(org.getCreatedBy());
    Event event = eventRepo.findOne(order.getEvent().getEventId());

    String image = imagePath.concat(event.getInfo().getImage());
    // String inlineHtml = "<img src=\"cid:" + image + "\">";

    // URL url = new URL(image);


    Map vmMap = new HashMap<>();
    vmMap.put("orderNo", order.getSummary().getOrderId());
    vmMap.put("totalCost", total);
    vmMap.put("totalPayable", totalPayable);
    vmMap.put("eventName", order.getEvent().getEventName());
    vmMap.put("serviceCharges", totalServiceCharge);
    vmMap.put("createdBy", user.getProfile().getFirstName() + " " + user.getProfile().getLastName());
    vmMap.put("cid", image);

    String pdfTemplate = velocityUtility.getTemplatetoText("templates/paidOrderEmail.vm", vmMap);

   emailUtility.sendOrderMail(order.getBuyer().getEmail(), pdfTemplate, link);

“图像”对象包含我的图像文件的路径,我正在尝试将它附加到速度模板中。

但是图像仍未加载到我通过邮件发送的模板中

它会附加一些随机的 Google 生成的链接,因此图像永远不会被附加到第一位。

谁能告诉我我哪里出错了?

4

1 回答 1

0

向 MimeMessage MimeMessageHelper添加内联元素

mailSender.send(new MimeMessagePreparator() {
           public void prepare(MimeMessage mimeMessage) throws MessagingException {
             MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
             helper.setFrom("me@mail.com");
             helper.setTo("you@mail.com");
             helper.setSubject("my subject");
             helper.setText("my text <img src='cid:myLogo'>", true);
             helper.addInline("myLogo", new ClassPathResource("img/mylogo.gif"));
             helper.addAttachment("myDocument.pdf", new ClassPathResource("doc/myDocument.pdf"));
    }
});
于 2021-10-04T04:49:05.670 回答