1

I know this question resembles to this question here. But, I am using a different email methodology with Spring Framework so adding a new question.

I have an image stored at /tmp/upload/image path in my machine.

Taking details from the same question mentioned above, I have added following details in my Velocity Template :

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

My Email Utility does the following :

@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);
}

And here is my service layer from where I bind the properties to the HashMap I am supposed to send to my Velocity Template :

    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);

"image" object contains the path of my image file and I am trying to attach it in the Velocity Template.

But the image is still not getting loaded in my template I am sending in the mail

It appends some random Google generated link to it, so the image is never attached at the first place.

Can anyone please tell me where am I going wrong ?

Vishal A
  • 144
  • 3
  • 17

1 Answers1

0

Add an inline element to the 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"));
    }
});
Jaka
  • 1
  • 2