We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

[Python] How can I copy a subset of messages to a new PST file given a MessageInfoCollection

I’ve run a query on a PST which returns a MessageInfoCollection object. I want to take all the emails referenced by this and add them to a new PST file with FolderInfo.AddMessages(). How can I achieve this?
The examples all refer to just running sourceFolder.enumerate_mapi_messages(), but I don’t want to take all the messages from a folder, just the ones returned by my search in that MessageInfoCollection object.

I also don’t want to move the originals around into a new folder either.

@PawelSzpak,

I have observed your requirements and request you to please visit this documentation section for your kind reference for possible options using Aspose.Email for Python via .NET. You can consider option of splitting PST based on query or even access the messages in PST and add them to new one.

Thank you for that link, unfortunately all the code examples on that documentation section appear to be C# rather than Python despite it being a python documentation section.

Also, I don’t actually want to split a pst file. I need to copy out a subset of messages from a PST file to a new PST file without making any changes to the original pst file.

@PawelSzpak,

In this case then I suggest you to please visit this documentation link for your kind reference. The example will allow you to copy messages from one PST to another. We have official documentation available in C# and you may port it to respective as per your requirements.

I think I’m failing to understand something here. I don’t really know C# at all.

I think I see how I can loop through my MessageInfoCollection and get a MapiMessage, but in your API documentation I can see
https://apireference.aspose.com/net/email/aspose.email.mapi/mapimessage
but I cannot find any reference for MapiMessageCollection. How do I create that in Python? Where is that in the API reference?

From what I can figure out from the code examples here, to add a large number of messages, I need to use FolderInfo.AddMessages()

This requires a parameter of type IEnumerable. I assume that MapiMessageCollection provides a method to create that?
Failing that, could I just create a python list of MapiMessages and pass that into FolderInfo.AddMessages()

Apologies if I’m missing something super obvious here.

@PawelSzpak,

I have observed your comments and like to share that official documentation available of API are in C# and Java. We may share the appropriate examples with you in these languages and you may consider porting them on your end. As far as class MapiMessageCollection is concerned, there is no such class existing in API. FolderInfo class has method EnumerateMapiMessages that actually return the collection of MapiMessage. The message example that I have shared reads the bulk of messages in the form of collection from PST folder and that to desired PST folder. If you are having trouble with this, you can consider the option of reading individual messages from PST folders and add them to respective PST folders. You can please visit this documentation section where you will find examples for reading messages from PST and also examples for adding Messages in PST. You will be using combination of both in your implementation.

PS: There are many examples available over the internet for managing IEnumearable. You may please visit link 1, link 2 and link 3 for your convenience, if required.

Thank you for this.

I’ve managed to get something to work using just FolderInfo.add_message(), it does take quite some time to run for a large number of emails, but that’s ok in this instance.

I didn’t realise this ienumerable thing was something more generic than just something used in your libraries. It looks pretty similar to a python generator, but creating a list of MapiMessages and then trying to pass a generator on those to .add_messages() just made the python kernel crash. I may just stick with .add_message() for now.

@PawelSzpak,

It’s good to know things are fine on your end. Please fell free to share if I may help you further in this regard.