Search code examples
javaiosemailmime

Apple or Mac Mail opens all attachment images even when they are embedded


When we send an email from our tomcat server, implementing MimeMultiPart, it opens in most mail software just fine, e.g. Gmail, Outlook, & Android Mail. But when it is opened on Apple Mail, it automatically opens PDF and images, which is permanent in mobile(phone and tablet, as laptops can be changed in command).

This is how it is designed for Apple, as I have read in a couple of websites. The problem is, even the embedded, supposedly a hidden attachment, is also shown. This results in double image, as we call the embedded via html in the mail.

The image is a logo, so this gets emailed always. I was hoping that there is a different protocol I can use that also works well in Apple mail. I haven't seen a similar issue in the web, so I have hope that we are just using some different protocol.

    BodyPart messageBodyPart = new MimeBodyPart();
    String htmlText = message + "<img src=\"cid:image123\">";
    messageBodyPart.setContent(htmlText, "text/html; charset=UTF-8");

    MimeMultipart mp = new MimeMultipart("mixed");
    mp.addBodyPart(messageBodyPart);

    BodyPart imageBodyPart = new MimeBodyPart();
    String file = this.getClass().getClassLoader().getResource("images/Logo.gif").getFile();
    DataSource fds = new FileDataSource(file);
    imageBodyPart.setFileName("Logo.gif");
    imageBodyPart.setHeader("Content-ID","<image123>");
    imageBodyPart.setDisposition(Part.INLINE);

    mp.addBodyPart(imageBodyPart);

When I remove the HTML code, it still shows the attached image in Apple mail, however, It will not show completely in other email software.


Solution

  • Defect finally on its way to production. With slight variation in MIME structure to https://stackoverflow.com/a/23853079/4558510

    What I did was to construct,

    • mixed +
      • related +
      • html
      • inline image
    • attachment
    • attachment

    Leaving out alternative with attachments because somehow, in the time of writing, Yahoo online client was not displaying them. Sticking them in mixed worked fine.

    Tested and works with,

    • Apple/IOS Mail (Tablet Ipad 2)
    • Outlook Windows 7 client
    • Outlook mobile (Android)
    • Gmail Web Client
    • Gmail mobile (Android)
    • Android mobile Email (Lollipop)
    • Yahoo web client
    • Yahoo mobile Email (Android)
    • Lotus Notes Windows 7 client

    Note: Android used is Samsung Note 4 Lollipop.

    Code:

        BodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = message + "<img src=\"cid:image123\">";
        messageBodyPart.setContent(htmlText, "text/html; charset=UTF-8");
    
        MimeMultipart mpRelated = new MimeMultipart("relative");
        mpRelated.addBodyPart(messageBodyPart);
    
        BodyPart imageBodyPart = new MimeBodyPart();
        String file = this.getClass().getClassLoader().getResource("images/Logo.gif").getFile();
        DataSource fds = new FileDataSource(file);
        imageBodyPart.setFileName("Logo.gif");
        imageBodyPart.setHeader("Content-ID","<image123>");
        imageBodyPart.setDisposition(Part.INLINE);
    
        mpRelated.addBodyPart(imageBodyPart);
    
        MimeMultipart mpMixed = new MimeMultipart("mixed");
        //Nest Related into mixed
        BodyPart relatedInMixed = new MimeBodyPart();
        relatedInMixed.setContent(mpRelated);
        mpMixed.addBodyPart(relatedInMixed);
    
        //TODO Add attachement to mpMixed