base64 encoded images in email signatures

后端 未结 3 1578
野的像风
野的像风 2020-11-22 16:28

I have to include some images (company logo\'s etc) in email signatures. I\'ve had all sorts of issues using the embedded images produced by the email system in question (th

3条回答
  •  星月不相逢
    2020-11-22 16:37

    Recently I had the same problem to include QR image/png in email. The QR image is a byte array which is generated using ZXing. We do not want to save it to a file because saving/reading from a file is too expensive (slow). So both of the answers above do not work for me. Here's what I did to solve this problem:

    import javax.mail.util.ByteArrayDataSource;
    import org.apache.commons.mail.ImageHtmlEmail;
    ...
    ImageHtmlEmail email = new ImageHtmlEmail();
    byte[] qrImageBytes = createQRCode(); // get your image byte array
    ByteArrayDataSource qrImageDataSource = new ByteArrayDataSource(qrImageBytes, "image/png");
    String contentId = email.embed(qrImageDataSource, "QR Image");
    

    Let's say the contentId is "111122223333", then your HTML part should have this:

    There's no need to convert the byte array to Base64 because Commons Mail does the conversion for you automatically. Hope this helps.

提交回复
热议问题