简体   繁体   中英

Convert an attachment of Content-type "message/rfc822" to .msg file

I am using apache's MimeMessageParser to get a MimeMessage. It has another Email type attachment that has body content and inline images. I was able to fetch the email attachment from mimeMessage and I could convert it to .msg file successfully.

But when I try to open that .msg file, an error comes as

We can't open 'C:\\local\\1_file_ds.msg'. It's possible the file is already open, or you don't have permission to open it.

Can anyone help me with this ? I want to convert the email attachment into .msg file.

Following is the code that I am using.

    List<DataSource> attachmentList = email.getAttachmentList();
    int attachmentCount = 1;
    try {
        for (DataSource attachment : attachmentList) {
            if (attachment.getContentType().equals("message/rfc822")) {

                InputStream inputStream;
                inputStream = attachment.getInputStream();

                String destName = "C:\\local\\" + +attachmentCount + "_" + "file_ds" + ".msg";

                File file = new File(destName);
                FileUtils.copyInputStreamToFile(inputStream, file);
                attachmentCount = attachmentCount + 1;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

We received an e-mail from a client who attached some rfc822 files to the e-mail. I had to save the file to disk and change the extension to eml. After that I could open the e-mail in MS Outlook and from there I could save it to the MSG format.

So if you just want to open the rfc822 attachements save the file on disk with the eml extension and make sure you have an e-mail program that can read those files.

If you need a msg file you have to find a converter to convert from eml to msg. Also MS Outlook has an API you can use to load the eml file and save it to an msg. That way you can create your own converter.

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