简体   繁体   中英

Open in outllok using javamail api an attached email containing email as attachment

I am able to open attached emails in outlook using javamail api. But I have a problem when this attached email contains also an eml as attachment.

In outlook, as below in the pic, the main mail(mail1 left side) contains mail2 as attachment, when opening mail2, we have Re:completion as attachment(right side). 展望邮件

In my application, i am displaying mail1 with its attachments in a specific template, when clicking on the attachment mail2, i am trying to open it with outlook using this code

try {
                //in case mMail.getPart(iPart) is attachment
                if (mMail.getPart(iPart).isMimeType("Message/*")) {
                    MimePart mimePart = mMail.getPart(iPart);

                    mMail = new Mail(); // class where we set the server and a new MimeMessage
                    mMail.setAllHeaders(mimePart); //we add all headers 
                    mMail.addAllBodyParts(mimePart);// check below

                    sCD = mimePart.getDisposition();
                    sCT = mimePart.getContentType();
                    if(sCD.startsWith(Part.ATTACHMENT)){
                        String filename = mimePart.getFileName();
                        filename = MimeUtility.decodeText(filename);
                        sCD = Part.ATTACHMENT + ";filename=" + filename;
                    }

                    iPart = -1;
                    bMsg = true;
                }
}
catch (Exception e) {
  e.printStackTrace();
} 

//here continue the code which open the mail

public void addAllBodyParts(Part part) throws Exception {

    Object content = part.getContent();

    if (content instanceof String) {
        mmMsg.setContent(content, part.getContentType());

    } else if (content instanceof Multipart) {

        Multipart innerMultiPart = (Multipart) content;
        int count = innerMultiPart.getCount();

        for (int i = 0; i < count; i++) {
            BodyPart innerBodyPart = innerMultiPart.getBodyPart(i);
            String sCT = innerBodyPart.getContentType();
            if (sCT != null) {
                String disposition = innerBodyPart.getDisposition();
                if (disposition != null && (disposition.equals(Part.ATTACHMENT))) {
                    Multipart multipart = new MimeMultipart();
                    //multipart.addBodyPart(innerBodyPart);
                    DataHandler handler = innerBodyPart.getDataHandler();
                    BodyPart messageBodyPart = new MimeBodyPart();

                    messageBodyPart.setDataHandler(handler);
                    messageBodyPart.setFileName(innerBodyPart.getFileName());
                    messageBodyPart.setContent(innerBodyPart.getContent(), innerBodyPart.getContentType());
                    multipart.addBodyPart(messageBodyPart);
                    //innerMultiPart.addBodyPart(innerBodyPart);
                    mmMsg.setContent(multipart);

                }else{
                    if(content instanceof MimeBodyPart){

                        MimeBodyPart mbp = (MimeBodyPart)content;
                        if (mbp.isMimeType("text/plain")) {
                            mmMsg.setContent(mbp.getContent(), sCT);
                        } 
                    }else{
                        addAllBodyParts(innerBodyPart);
                    }
                }

            }else{
                addAllBodyParts(innerBodyPart);
            }
        }
    } else if (content instanceof MimeMessage){

        MimeMessage msg = (MimeMessage) content;
        addAllBodyParts(msg);
    }
}

i got the following when mail2 is opened:(onthe left side, the body is empty, on right side, the header is empty and the text in the body is wrong) 附件邮件

Can anyone help to find what is the error. Thank you

I'm not quite sure what you're trying to accomplish with addAllBodyParts. It sems that you're just creating a new message with the same content as the original message. It's not clear what you do with that new message after you create it, but why not just use the original (attachment) message object?

MimeMessage attachedMsg = (MimeMessage)mimePart.getContent();

I don't know how you tell Outlook to display a single message, but you can write the message to a file using the writeTo method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM