How to Convert Messages from MBox to PST Using Aspose.Email?

How can convert MBox to PST. I am following the Git hub example. But not getting the right way for conversion.

@priyankur.chauhan,

Thank you for writing to Aspose support team.

You may please give a try to the following sample code and share the feedback.

int counter = 0;
PersonalStorage storage = MailStorageConverter.MboxToPst(@".\source.mbox",
    @".\output.pst",
    delegate (MailMessage message)
    {
        counter += 1;
    });

//Verify the number of messages in the source MBOX and destination PST
using (MboxrdStorageReader reader = new MboxrdStorageReader(@".\source.mbox", false))
{
    Console.WriteLine(reader.GetTotalItemsCount());
    Console.WriteLine(counter);
}

Hi Team,
I want to convert Multiple Mbox file to single pst using java apose email.
Please tell me is there any method to convert this.

@pradeepnegi,

You may convert individual MBOX files to PST files using the code given in the previous message. After that, you may merge the generated PST files as shown in the code snippet given on the page linked below.
Merging into a single PST
We hope that this answered your question. Please feel free to reach us if additional information is required.

public static String dataDir ="C:\\Users\\DELL\\Desktop\\";
		
public static int messageCount = 0;
public static int totalAdded = 0;
public static String currentFolder = null;

public static void main(String[] args) {
	
	String sourceFileName = dataDir +  "Sources/source.pst";
	String destinationFolder = dataDir + "Destination" + File.separator;
	String destinationFileName = "destination.pst";

	// This will ensure that we can run this example as many times as we want. 
	// It will discard changes made to to the destination file "destination.pst" in last run of this example.
	deleteAndRecopySampleFiles(destinationFolder, dataDir + "MergeFoldersFromAnotherPST" + File.separator);

	final PersonalStorage destinationPst = PersonalStorage.fromFile(destinationFolder + destinationFileName);
	try {
		final PersonalStorage sourcePst = PersonalStorage.fromFile(sourceFileName);
		try {
			
			FolderInfo destFolder = destinationPst.getRootFolder().addSubFolder("FolderFromOtherPst" + (int) (Math.random() * 100));
			
			FolderInfo sourceFolder = sourcePst.getPredefinedFolder(StandardIpmFolder.Inbox);

			destFolder.ItemMoved.add(new ItemMovedEventHandler() {
				public void invoke(Object sender, ItemMovedEventArgs e) {
					destinationFolder_ItemMoved(sender, e);
				}
			});

			destFolder.mergeWith(sourceFolder);

			System.out.println("Total messages added: " + totalAdded);
		} finally {
			if (sourcePst != null)
				(sourcePst).dispose();
		}
	} finally {
		if (destinationPst != null)
			(destinationPst).dispose();
	}

	// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
	String fileName = dataDir + "source.pst";
	java.util.Calendar c = java.util.Calendar.getInstance();

	List<MailQuery> criteria = new List<MailQuery>();

	//Define a criterion based on Time
	PersonalStorageQueryBuilder pstQueryBuilder = new PersonalStorageQueryBuilder();
	c.set(2015, 1, 1, 0, 0, 0);
	pstQueryBuilder.getSentDate().since(c.getTime());
	c.set(2015, 12, 12, 0, 0, 0);
	pstQueryBuilder.getSentDate().before(c.getTime());
	criteria.addItem(pstQueryBuilder.getQuery());

	//specify some other criterion as well
	pstQueryBuilder = new PersonalStorageQueryBuilder();
	c.set(2012, 1, 1, 0, 0, 0);
	pstQueryBuilder.getSentDate().since(c.getTime());
	c.set(2012, 12, 12, 0, 0, 0);
	pstQueryBuilder.getSentDate().before(c.getTime());
	criteria.addItem(pstQueryBuilder.getQuery());

	final PersonalStorage pst = PersonalStorage.fromFile(fileName);
	try {
		deleteAllOutputFiles();
		pst.splitInto(criteria, dataDir);
	} finally {
		if (pst != null)
			pst.dispose();
	}

This code is not working, It gives an error :

Exception in thread “main” java.lang.NullPointerException
at SplitAndMergePSTFile.deleteAllFilesInDirectory(SplitAndMergePSTFile.java:114)
at SplitAndMergePSTFile.deleteAndRecopySampleFiles(SplitAndMergePSTFile.java:104)
at SplitAndMergePSTFile.main(SplitAndMergePSTFile.java:33)

@pradeepnegi,

We are investigating this issue further. In the meantime, please test the code snippet given below using the latest version of Aspose.Email for Java API. We are looking forward to your feedback.

String sourceFileName = "mergeSource.pst";
String mergeWithFolderPath = "pstFolder";
PersonalStorage pst = PersonalStorage.fromFile(sourceFileName);

FolderInfo info = pst.getRootFolder().getSubFolder("myInbox");
MessageInfoCollection messageInfoCollection = info.getContents();
System.out.println(messageInfoCollection.size());

ArrayList<String> results = new ArrayList<String>();
File[] files = new File(mergeWithFolderPath).listFiles();
if (files == null)
    return;

for (File file : files) {
    if (file.isFile() && file.getName().endsWith(".pst")) {
        results.add(file.getAbsolutePath());
    }
}

String[] fileNames = results.toArray(new String[0]);

pst.mergeWith(fileNames);

PersonalStorage personalStorage = PersonalStorage.fromFile(sourceFileName);
FolderInfo info1 = personalStorage.getRootFolder().getSubFolder("myInbox");
MessageInfoCollection messageInfoCollection1 = info1.getContents();
System.out.println(messageInfoCollection1.size());

@pradeepnegi,
You can migrate messages from MBOX files to a PST file as shown below:

MboxLoadOptions loadOptions = new MboxLoadOptions();
MboxStorageReader mboxReader = MboxStorageReader.createReader("test.mbox", loadOptions);

PersonalStorage personalStorage = PersonalStorage.create("test.pst", FileFormatVersion.Unicode);
FolderInfo inboxFolder = personalStorage.getRootFolder().addSubFolder("Inbox");

for (MailMessage mailMessage : mboxReader.enumerateMessages())
{
    MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage);
    inboxFolder.addMessage(mapiMessage);
}

mboxReader.dispose();
personalStorage.dispose();

API Reference: MboxStorageReader Class