Search code examples
javaiphonejakarta-mailattachment

Can't read attachment from iphone when using Javamail to attach inline image


Attachments appear well except on iphone when I add an image as MimeBodyPart.INLINE , what is the best way to attach an image as signature using javamail? If I remove "imagePart", all other attachments work well

I used :

    MimeMessage m = new MimeMessage(session);
    MimeMultipart content = new MimeMultipart("related");

    // ContentID is used by both parts
    String cid = ContentIdGenerator.getContentId();

    // HTML part
    String textPartSaine = Tools.convertSymbolToUTF8(emailContenu, true);
    MimeBodyPart textPart = new MimeBodyPart();
    textPart.setText("<html><head>"
    + "<title></title>"
    + "</head>\n"
    + "<body>"
    + "<div>"+ textPartSaine.replaceAll("\n", "<BR/>") +"</div><BR/><BR/>"
    + "<div><img src=\"cid:"
    + cid
    + "\" /></div><BR/><BR/>" + "</body></html>", 
    "US-ASCII", "html");
    content.addBodyPart(textPart);

    // Image part
    if(signature != null && signature.exists()){
        MimeBodyPart imagePart = new MimeBodyPart();
        imagePart.attachFile(signature);
        imagePart.setContentID("<" + cid + ">");
        imagePart.setDisposition(MimeBodyPart.INLINE);
        content.addBodyPart(imagePart);
    }

    if(fichiers != null && fichiers.length > 0) {
        for(i = 0; i < fichiers.length; i++) {
            partie = new MimeBodyPart();
            partie.attachFile(fichiers[i]);
            content.addBodyPart(partie);
        }
    }

Thanks


Solution

  • Solved multipart/mixed (Will contain text and attachments) multipart/alternative (Will contain text and HTML) multipart/related (HTML + embedded images) image1 (Content-Id: xxx) image2 ... attachment 1 attachment 2 ...