将.pst文件解压缩到msg但未获取确切数量的msg文件

时间:2018-05-31 10:36:53

标签: java aspose

我正在尝试将pst文件解压缩到msg中。 我正在使用aspose jar。我分享了我的代码,我们在每个子文件夹中获得了确切的文件数。

public static void displayFolderAndMessageInformationForPSTFile(String dataDir) {

    // Load the Outlook PST file
    PersonalStorage pst = PersonalStorage.fromFile(dataDir + "allen.pst");

    // Get the folders information
    FolderInfoCollection folderInfoCollection = pst.getRootFolder().getSubFolders();

    // Browse through each folder to display folder name and number of messages
    for (int i = 0; i < folderInfoCollection.size(); i++) {
        FolderInfo folderInfo = (FolderInfo) folderInfoCollection.get_Item(i);
        System.out.println("FolderId: " + folderInfo.getEntryIdString());
        System.out.println("Folder: " + folderInfo.getDisplayName());
        System.out.println("Total items: " + folderInfo.getContentCount());
        System.out.println("Total unread items: " + folderInfo.getContentUnreadCount());
        System.out.println("-----------------------------------");
    }
}

FolderId:AAAAAJJu05VTxVRJlC5mJefQvVeCgAAA
文件夹:收件箱
项目总数:66
未读物品总数:0

但是当提取消息内容时,我得到不同数量的消息。它只在收件箱文件夹中提供49 msg 以下代码:

public static void main(String[] args) {

    String pstFileName = dataDir + "allen.pst";
    // 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 = "allen.pst".replace(".pst", "") + ".Java";
    new File(dataDir + strRootFolderName).mkdir();

    // Call the recursive method to extract msg files from each folder
    extractMsgFiles(folderInfo, pst, dataDir + strRootFolderName);

}

private static void extractMsgFiles(FolderInfo folderInfo, PersonalStorage pst, String strPSTFile) {

    // Display the folder name
    System.out.println("Folder: " + folderInfo.getDisplayName());

    // Create folder to store the messages
    String folderName = strPSTFile + "\\" + 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_Item(i);
        System.out.println("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_Item(i);
            extractMsgFiles(subfolderInfo, pst, strPSTFile);
        }
    }
}

帮帮我......我哪里弄错了?我是Aspose的新手。

2 个答案:

答案 0 :(得分:0)

请查看以下用于解压缩邮件文件的代码段。您可以使用此方法代替 extractMsgFiles 方法。

private static void ExtractMsgFiles(FolderInfo folderInfo, PersonalStorage pst)
{

    // display the folder name
    Console.WriteLine("Folder: " + folderInfo.DisplayName);
    Console.WriteLine("==================================");

    // loop through all the messages in this folder
    MessageInfoCollection messageInfoCollection = folderInfo.GetContents();

    foreach (MessageInfo messageInfo in messageInfoCollection)
    {
        Console.WriteLine("Saving message {0} ....", messageInfo.Subject);

        // get the message in MapiMessage instance
        MapiMessage message = pst.ExtractMessage(messageInfo);

        // save this message to disk in msg format
        message.Save(message.Subject.Replace(":", " ") + ".msg");

        // save this message to stream in msg format
        MemoryStream messageStream = new MemoryStream();

        message.Save(messageStream);
    }

    // Call this method recursively for each subfolder
    if (folderInfo.HasSubFolders == true)
    {
        foreach (FolderInfo subfolderInfo in folderInfo.GetSubFolders())
        {
            ExtractMsgFiles(subfolderInfo, pst);
        }
    }
}

如果您对更多详情感兴趣,可以访问Working with Messages in a PST File链接。 我和Aspose一起担任开发人员传道人。

答案 1 :(得分:0)

没有唯一主题名称的电子邮件可能导致此问题?在Powershell中执行类似操作时,我遇到了这个确切的问题。在文件名中添加自动编号可以帮助解决此问题。