Free Support Forum - aspose.com

How to create MimeMessage from a RFC 822 stream

Hello,
I would like some help to build a MimeMessage struct from a RFC 822 stream message.
I’m using Java but I think an example on any language would be useful.
Thanks a lot.

@kelberuc

Aspose.Email provides the supports to load from standard stream. Can you please elaborate the requirements that what you want to achieve using Aspose.Email.

Hi @mudassir.fayyaz,

Thanks for your answer.
I have this code in my program:

		InputStream stream = new ByteArrayInputStream(rfc822messageByteArray);
		
		MailMessage mm = new MailMessage();
		mm.load(stream);
		
		MapiMessage mapiMessage = MapiMessage.fromMailMessage(mm);
		targetFolder.addMessage(mapiMessage);

It didn’t work because the message added into target folder is empty.
Basically I have a stream with a RFC 822 message (eml file format) and I want to add it in a folder into a PST folder.

Can you assist me what’s wrong in my code?
Thanks a lot

@kelberuc

Please try using following sample code on your end. If you still reproduce the issue then please first try saving loaded InputStream to a file and see if the file generated is correct or not?

            InputStream stream = new ByteArrayInputStream(rfc822messageByteArray);

            MailMessage mm = MailMessage.load(stream);

            MapiMessage mapiMessage = MapiMessage.fromMailMessage(mm);
            targetFolder.addMessage(mapiMessage);

Hi @mudassir.fayyaz

I have already done this test.
I saved the stream into a .EML file and then opened correctly in Outlook!

@kelberuc

Please provide the working example along with source stream that we may test on our end to reproduce the issue on our end.

@mudassir.fayyaz
You can use this code with the following attached stream file to reproduce the problem. If I load the contents of this file into the “rfc822messageByteArray” and run it won’t work
sourceStream.zip (492 Bytes)

@kelberuc

I have saved the text file shared by you as MSG file and tried opening that in MS Outlook.That too is unable to open it. In order to proceed further with investigation, I request you to please provide the complete example for loading byte array from file or other source and passing that to stream. Please also share how can I verify the text file shared by you by opening in MS Outlook that if that is correct file or not.

image.png (12.2 KB)

Hi @mudassir.fayyaz

You have to save it as .EML file, and not .MSG.
I did this on my example I’ve sent you before.

Thanks

@kelberuc

I have tried using following sample code to load the file on my end there is no issue.

    public static void TestFromBytearray() throws FileNotFoundException, IOException
    {
        String path="C:\\Users\\mudas\\Downloads\\sourceStream\\";
        
        File file=new File(path+"sourceStream.txt"); 

        byte[] data=new byte[(int) file.length()];
     
        FileInputStream fis = new FileInputStream(file);
        fis.read(data); //read file into bytes[]
        fis.close();

        InputStream stream = new ByteArrayInputStream(data,0,data.length);
        MailMessage mm = MailMessage.load(stream);
        MapiMessage mapiMessage = MapiMessage.fromMailMessage(mm); 
        int ss=0;
    }

Hi @mudassir.fayyaz

it worked now!

I found the problem in my code, the issue was in the following line:
InputStream stream = new ByteArrayInputStream(rfc822messageByteArray);

The correct way is:
InputStream stream = new ByteArrayInputStream(rfc822messageByteArray, 0, rfc822messageByteArray.length);

Thank you very much for your assistance!

@kelberuc

It’s good to know that suggested option has proved to be working on your end.