How to Extract Messages from Outlook PST & Save It in MSG Format in Android Apps
This technical tip shows how to extract messages from Outlook PST file and save to disk or stream in MSG format using Aspose.Email for Android API. This article shows how developers can read an Outlook PST file, extract messages and save them to disk in MSG format inside their Android applications. Aspose.Email is flexible and lets you extract all messages (to disk or stream), or a set number, found in a PST file. To extract and save messages, use a recursive method to browse through all the folders (including any nested folders) and call the PersonalStorage.extractMessage() method to get Outlook messages in an instance of the MapiMessage class. After that, call the MapiMessage.save() method to save the message in MSG format to either disk or stream.
//your code here...//Extract Messages from PST File
private void extractMessagesFromPst()
{
String strBaseFolder = Environment.getExternalStorageDirectory().getPath() + "/";
String pstFileName = strBaseFolder + "Outlook.pst";
//TextView to display output
final TextView tx = (TextView) findViewById(R.id.textView1);;
try
{
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile(pstFileName);
// Get the folders and messages information
FolderInfo folderInfo = pst.getRootFolder();
// Create a folder for this PST
String strRootFolderName = pst.getDisplayName().replace(".pst", "") + ".Java2";
//Create the new directory
new File(strBaseFolder + strRootFolderName).mkdir();
// Call the recursive method to extract msg files from each folder
ExtractMsgFiles(folderInfo, pst, strBaseFolder + strRootFolderName);
}
catch (Exception ex)
{
tx.setText(ex.getMessage());
}
}
private void ExtractMsgFiles(FolderInfo folderInfo, PersonalStorage pst, String strBaseFolder)
{
final TextView tx = (TextView) findViewById(R.id.textView1);
// Display the folder name
tx.setText(tx.getText() + "\n Folder: " + folderInfo.getDisplayName());
// Create folder to store the messages
String folderName = strBaseFolder + "/" + folderInfo.getDisplayName();
new File(folderName).mkdir();
// Loop through all the messages in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (int i = 0; i < messageInfoCollection.size(); i++)
{
MessageInfo messageInfo = (MessageInfo) messageInfoCollection.get(i);
tx.setText(tx.getText() + "\n Saving message " + messageInfo.getSubject() + "....");
// Get the message in MapiMessage instance
MapiMessage message = pst.extractMessage(messageInfo);
// Delete special characters which are invalid to use as windows file name
String messageName = null;
if (message.getSubject() == null || message.getSubject().isEmpty() == true)
{
messageName = GetRidOfIllegalFileNameCharacters(messageInfo.getEntryIdString());
}
else
{
messageName = GetRidOfIllegalFileNameCharacters(message.getSubject());
}
// Save this message to disk in MSG format
message.save(folderName + "/" + messageName + ".msg");
}
// Call this method recursively for each subfolder
if (folderInfo.hasSubFolders() == true)
{
for (int i = 0; i < folderInfo.getSubFolders().size(); i++)
{
FolderInfo subfolderInfo = (FolderInfo) folderInfo.getSubFolders().get(i);
ExtractMsgFiles(subfolderInfo, pst, strBaseFolder);
}
}
}
private String GetRidOfIllegalFileNameCharacters(String strName)
{
String strLegalName = strName.replace(":", " ").replace("\\", " ").replace("?", " ").replace("/", " ").replace("|", " ").replace("*", " ").replace("<", " ").replace(">", " ").replace("\t", " ").replace("\"", " ");
if (strLegalName.length() >= 100)
{
strLegalName = strLegalName.substring(0, 100);
}
return strLegalName;
}
//Saving Messages Directly from PST to Stream
String strBaseFolder = Environment.getExternalStorageDirectory().getPath() + "/";
PersonalStorage pst = PersonalStorage.fromFile(strBaseFolder + "Outlook.pst");
FolderInfo inbox = pst.getRootFolder().getSubFolder("Inbox");
MessageInfo messageInfo;
for (Object obj : inbox.enumerateMessages())
{
messageInfo = (MessageInfo)obj;
pst.saveMessageToStream(messageInfo.getEntryIdString(), new ByteArrayOutputStream());
}
String strBaseFolder = Environment.getExternalStorageDirectory().getPath() + "/";
String strFolderName = "TestMsgsFromStream";
new File(strBaseFolder + strFolderName).mkdir();
// Save message to file
PersonalStorage pst = PersonalStorage.fromFile(strBaseFolder + "Outlook.pst");
FolderInfo inbox = pst.getRootFolder().getSubFolder("Inbox");
MessageInfo messageInfo;
for (Object obj : inbox.enumerateMessages())
{
messageInfo = (MessageInfo)obj;
FileOutputStream fop = null;
File file;
file = new File(strBaseFolder + strFolderName + "/" + messageInfo.getSubject() + ".msg");
fop = new FileOutputStream(file);
pst.saveMessageToStream(messageInfo.getEntryIdString(), fop);
}
//To enumerate entryId of messages you may use FolderInfo.enumerateMessagesEntryId() method:
String strBaseFolder = Environment.getExternalStorageDirectory().getPath() + "/";
PersonalStorage pst = PersonalStorage.fromFile(strBaseFolder + "Outlook.pst");
FolderInfo inbox = pst.getRootFolder().getSubFolder("Inbox");
String entryId;
for (Object obj : inbox.enumerateMessagesEntryId())
{
entryId = (String)obj;
pst.saveMessageToStream(entryId, new ByteArrayOutputStream());
}
Url: http://www.aspose.com/android/email-component.aspx
Language: Java | User: Sheraz Khan | Created: Mar 4, 2015 | Tags: Extract Messages from Outlook PST Save It in MSG Format android email API Outlook Android component read an Outlook PST file convert PST messages to MSG get Outlook messages, Read PST files