Java MimeBodyPart wrongfully showing as attachments

63 Views Asked by At

I am having a really weird problem when adding more than one MimeBodyPart to a MimeMultiPart. Essentially, if the MimeMultiPart is mixed, then all MimeBodyPart objects are added as attachments, except for the last one, which is added to the body. If the MimeMultiPart is alternative, then only the last MimeBodyPart is added to the body.

I am just trying to write a simple email which should have an opening (text), followed by a table (html), and finally a closing (text).

Code

Here is the code for my function creating the email. It generates the MimeMessage and then opens it in Outlook as a draft.

public void writeEmail() throws Exception {
        //Create message envelope.
        MimeMessage msg = new MimeMessage((Session) null);
        
        //Set destination and cc
        msg.addFrom(InternetAddress.parse("SELECT@ACCOUNT"));
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
        msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse("[email protected]"));
        
        //Create and set the subject
        String subject = "subject"; 
        
        msg.setSubject(subject);
        
        //Make the message as a draft
        msg.setHeader("X-Unsent", "1");

        
        //Create the body
        MimeMultipart mmp = new MimeMultipart("alternative");
        
        //Tests
        String opening = "Opening";
        String html = "<h1>" + table + "</h1>";
        String closing = "Closing";
        
        MimeBodyPart openingPart = new MimeBodyPart();
        openingPart.setDisposition(MimeBodyPart.INLINE);
        openingPart.setText(opening, "utf-8");
        
        MimeBodyPart htmlPart = new MimeBodyPart();
        htmlPart.setDisposition(MimeBodyPart.INLINE);
        htmlPart.setContent(html, "text/html; charset=utf-8");
        
        MimeBodyPart closingPart = new MimeBodyPart();
        closingPart.setDisposition(MimeBodyPart.INLINE);
        closingPart.setText(closing, "utf-8");
        
        mmp.addBodyPart(openingPart);
        mmp.addBodyPart(htmlPart);
        mmp.addBodyPart(closingPart);
        
        msg.setContent(mmp);
        msg.saveChanges();

        //Save as a temporary file
        File resultEmail = File.createTempFile("email", ".eml");
        
        try (FileOutputStream fs = new FileOutputStream(resultEmail)) {
            msg.writeTo(fs);
            fs.flush();
            fs.getFD().sync();
        }

        System.out.println(resultEmail.getCanonicalPath());

        ProcessBuilder pb = new ProcessBuilder();
        pb.command("cmd.exe", "/C", "start", "outlook.exe", "/eml", resultEmail.getCanonicalPath());
        Process p = pb.start();
        
        try {
            p.waitFor();
        } finally {
            p.getErrorStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();
            p.destroy();
        }
}

Question

How can I render all the MimeBodyPart as text inside of the email body, instead of them being wrongfully added as attachments?

0

There are 0 best solutions below