如何使用电子邮件 org.apache.velocity.VelocityContext 发送图像

How to Send image using org.apache.velocity.VelocityContext for email

MimeMessagePreparator mimeMessagePreparator=new MimeMessagePreparator() {

@Override
public void prepare(MimeMessage mimeMessage) throws Exception {

MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage);
                    mimeMessageHelper.setTo(requestQuotationPojo.getEmailId().toString());
                    mimeMessageHelper.setFrom("vikasglobaljapan@gmail.com");
                    mimeMessageHelper.setText("Your Request Quotation Number"+requestQuotationPojo.getRfqId());
                    mimeMessageHelper.setSubject("Request Quotation Details");
    VelocityContext velocityContext = new VelocityContext();

    requestQuotationPojo.setImage(new StringBuffer(application_url.concat(requestQuotationPojo.getImage().toString())));
    requestQuotationPojo.setRfqId(new StringBuffer(rfqId));

    velocityContext.put("image", requestQuotationPojo.getImage());
    velocityContext.put("rfqDetails", requestQuotationPojo);
                    velocityContext.put("image",application_url.concat(requestQuotationPojo.getImage().toString()));

   StringWriter stringWriter = new StringWriter();
   velocityEngine.mergeTemplate("com/gjcp/dao/daoImpl/customer/templateName.vm", "UTF-8", velocityContext, stringWriter);
                    mimeMessageHelper.setText(stringWriter.toString(), true);
                }

template.vm

<!doctype html>
<html lang="en">
 <head>
 <style>
table, th, td {
    border: 1px solid black;
}
</style>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
 <table style="width:100%">
  <tr>
    <th>Product Name</th>
    <th>Quote Number</th>
    <th>Image</th>
    <th>Quantity</th>
  </tr>
  <tr>
    <td>${rfqDetails.productName}</td>
    <td> ${rfqDetails.rfqId}</td>
    <td><img src="${rfqDetails.image}" border="0" width="50" height="50"></td>
    <td>${rfqDetails.quantity}</td>
  </tr>

</table>


 </body>
</html>
  • 我想你的图像是一个字节数组。因此,您应该将图像作为 base64 字符串添加到速度上下文中。

    velocityContext.put("image", Base64.encode(requestQuotationPojo.getImage()));

  • 并在您的模板文件中使用此 img 标签。

    <img src="data:image/jpg;base64,${image}" border="0" width="50" height="50"/>

${websitename} : 写下您的网站名称。

${rfqDetails.image} : 你的图片路径。

Like Your image url: http://test.com/folder/abc.png

#set($imgsrc="http://${websitename}/${rfqDetails.image})

 <img width="50" src="$imgsrc" border="0" height="50" alt="test">