עבודה עם הודעות בקובץ PST
הוספת הודעות לקבצי PST
צור קובץ PST חדש והוסף תיקיות משנה הראה כיצד ליצור קובץ PST ולהוסיף לו תיקיית משנה. עם Aspose.Email ניתן להוסיף הודעות לתיקיות משנה של קובץ PST שיצרת או טענת. מאמר זה מוסיף שתי הודעות מהדיסק לתיקיית המשנה Inbox של PST. השתמש ב‑ PersonalStorage ו FolderInfo מחלקות להוספת הודעות לקבצי PST. כדי להוסיף הודעות לתיקיית Inbox של קובץ PST:
- צור מופע של המחלקה FolderInfo וטען אותו בתכולת תיקיית Inbox.
- הוסף הודעות מהדיסק לתיקיית Inbox באמצעות קריאה ל‑ FolderInfo.addMessage() מתודה. ה‑ FolderInfo המחלקה מציגה את addMessages שיטה המאפשרת להוסיף מספר גדול של הודעות לתיקייה, מה שמפחית פעולות קלט/פלט לדיסק ומשפר ביצועים. דוגמה מלאה ניתן למצוא למטה, ב הוספת הודעות גורפות.
קטעי הקוד למטה מראים כיצד להוסיף הודעות לתיקית משנה של PST בשם Inbox.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create new PST
PersonalStorage personalStorage = PersonalStorage.create(dataDir, FileFormatVersion.Unicode);
// Add new folder "Inbox"
personalStorage.getRootFolder().addSubFolder("Inbox");
// Select the "Inbox" folder
FolderInfo inboxFolder = personalStorage.getRootFolder().getSubFolder("Inbox");
// Add some messages to "Inbox" folder
inboxFolder.addMessage(MapiMessage.fromFile(dataDir + "MapiMsgWithPoll.msg"));
הוספת הודעות גורפות
הוספת הודעות בודדות ל‑PST גורמת ליותר פעולות קלט/פלט לדיסק ולכן עשויה להאט את הביצועים. לשיפור הביצועים, ניתן להוסיף הודעות ל‑PST במצב גורף כדי למזער פעולות קלט/פלט. ה addMessages(Iterable
טעינת הודעות מהדיסק
קטטע הקוד הבא מראה לך כיצד לטעון הודעות מהדיסק.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
private static void addMessagesInBulkMode(String fileName, String msgFolderName) {
try (PersonalStorage personalStorage = PersonalStorage.fromFile(fileName)) {
FolderInfo folder = personalStorage.getRootFolder().getSubFolder("myInbox");
folder.MessageAdded.add(new MessageAddedEventHandler() {
public void invoke(Object sender, MessageAddedEventArgs e) {
onMessageAdded(sender, e);
}
});
folder.addMessages(new MapiMessageCollection(msgFolderName));
}
}
static void onMessageAdded(Object sender, MessageAddedEventArgs e) {
System.out.println(e.getEntryId());
System.out.println(e.getMessage().getSubject());
}
מימוש Iterable
קטטע הקוד הבא מראה לך כיצד ליצור מימוש Iterable.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public class MapiMessageCollection implements Iterable<MapiMessage> {
private final File folder;
public MapiMessageCollection(String folder) {
this.folder = new File(folder);
}
public Iterator<MapiMessage> iterator() {
return new MapiMessageIterator(folder.listFiles());
}
}
public class MapiMessageIterator implements Iterator<MapiMessage> {
private Queue<String> queue = new LinkedList<String>();
public MapiMessageIterator(File[] listOfFiles) {
for (File file : listOfFiles) {
queue.offer(file.getAbsolutePath());
}
}
public boolean hasNext() {
return !queue.isEmpty();
}
public MapiMessage next() {
return MapiMessage.fromFile(queue.poll());
}
}
הוספת הודעות מ‑PST אחר
להוספת הודעות מ‑PST אחר, השתמש ב‑ FolderInfo.enumerateMapiMessages() שיטה המחזירה Iterable
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
private static void bulkAddFromAnotherPst(String source) {
// The path to the File directory.
String dataDir = "data/";
try (PersonalStorage pst = PersonalStorage.fromFile(source, false)) {
try (PersonalStorage pstDest = PersonalStorage.fromFile(dataDir + "PersonalStorageFile1.pst")) {
// Get the folder by name
FolderInfo folderInfo = pst.getRootFolder().getSubFolder("Contacts");
MessageInfoCollection ms = folderInfo.getContents();
// Get the folder by name
FolderInfo f = pstDest.getRootFolder().getSubFolder("myInbox");
f.MessageAdded.add(new MessageAddedEventHandler() {
public void invoke(Object sender, MessageAddedEventArgs e) {
onMessageAdded(sender, e);
}
});
f.addMessages(folderInfo.enumerateMapiMessages());
FolderInfo fi = pstDest.getRootFolder().getSubFolder("myInbox");
MessageInfoCollection msgs = fi.getContents();
}
}
}
// Handles the MessageAdded event.
static void onMessageAdded(Object sender, MessageAddedEventArgs e) {
System.out.println(e.getEntryId());
System.out.println(e.getMessage().getSubject());
}
קבלת מידע על הודעות מקובץ Outlook PST
ב‑ קריאת קובץ Outlook PST וקבלת מידע על תיקיות ותיקיות משנה, דנו בטעינת קובץ Outlook PST ובדפדוף בתיקיות שלו כדי לקבל את שמות התיקיות ומספר ההודעות שבהם. מאמר זה מסביר כיצד לקרוא את כל התיקיות ותיקיות המשנה בקובץ PST ולהציג מידע על הודעות, לדוגמה נושא, שולח ונמענים. ה‑ FolderInfo.getContents() שיטה משמשת להצגת מידע קצר על הודעה כגון נושא, שולח, נמענים. מבחינת ביצועים, זו האפשרות המתאימה ביותר לקבלת מידע ראשוני על הודעות. כדי חילוץ נתוני ההודעה המלאים, ה‑ PersonalStorage.extractMessage() מסופקת שיטה. קובץ Outlook PST עשוי להכיל תיקיות מקוננות. כדי לקבל מידע על הודעות מהן, וכן מהתיקיות ברמה העליונה, השתמש בשיטה ריקורסיבית לקריאת כל התיקיות. קטע הקוד הבא מראה כיצד לקרוא קובץ Outlook PST ולהציג את תוכן התיקיות וההודעות בצורה רקורסיבית.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void run() {
// The path to the File directory.
String dataDir = "data/";
// Load the Outlook file
String path = dataDir + "PersonalStorage.pst";
try {
// Load the Outlook PST file
PersonalStorage personalStorage = PersonalStorage.fromFile(path);
// Get the Display Format of the PST file
System.out.println("Display Format: " + personalStorage.getFormat());
// Get the folders and messages information
FolderInfo folderInfo = personalStorage.getRootFolder();
// Call the recursive method to display the folder contents
displayFolderContents(folderInfo, personalStorage);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
// This is a recursive method to display contents of a folder
private static void displayFolderContents(FolderInfo folderInfo, PersonalStorage pst) {
// Display the folder name
System.out.println("Folder: " + folderInfo.getDisplayName());
System.out.println("==================================");
// Display information about messages inside this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (MessageInfo messageInfo : messageInfoCollection) {
System.out.println("Subject: " + messageInfo.getSubject());
System.out.println("Sender: " + messageInfo.getSenderRepresentativeName());
System.out.println("Recipients: " + messageInfo.getDisplayTo());
System.out.println("------------------------------");
}
// Call this method recursively for each subfolder
if (folderInfo.hasSubFolders() == true) {
for (FolderInfo subfolderInfo : folderInfo.getSubFolders()) {
displayFolderContents(subfolderInfo, pst);
}
}
}
חילוץ הודעות מקבצי PST
מאמר זה מראה כיצד לקרוא קבצי PST של Microsoft Outlook ו‑ לחצל הודעות. ההודעות נשמרות לאחר מכן לדיסק בפורמט MSG. המאמר גם מראה כיצד לחלץ מספר ספציפי של הודעות מקובץ PST. השתמש בשיטה ריקורסיבית לעבור על כל התיקיות (כולל תיקיות מקוננות) וקרא ל‑ PersonalStorage.extractMessage() שיטה לקבל הודעות Outlook לתוך מופע של MapiMessage מחלקה. לאחר ذلك, קרא ל‑ MapiMessage.save() שיטה לשמור את ההודעה אל דיסק או זרם בפורמט MSG. קטע הקוד הבא מראה כיצד לחלץ הודעות מקובץ PST.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void run() {
// The path to the File directory.
String dataDir = "data/";
// Load the Outlook file
String path = dataDir + "PersonalStorage.pst";
try {
// load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile(path);
// get the Display Format of the PST file
System.out.println("Display Format: " + pst.getFormat());
// get the folders and messages information
FolderInfo folderInfo = pst.getRootFolder();
// Call the recursive method to extract msg files from each folder
extractMsgFiles(folderInfo, pst);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
// This is a recursive method to display contents of a folder
private static void extractMsgFiles(FolderInfo folderInfo, PersonalStorage pst) {
// display the folder name
System.out.println("Folder: " + folderInfo.getDisplayName());
System.out.println("==================================");
// loop through all the messages in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (MessageInfo messageInfo : messageInfoCollection) {
System.out.println("Saving message {0} ...." + messageInfo.getSubject());
// get the message in MapiMessage instance
MapiMessage message = pst.extractMessage(messageInfo);
// save this message to disk in msg format
message.save(message.getSubject().replace(":", " ") + ".msg");
// save this message to stream in msg format
ByteArrayOutputStream messageStream = new ByteArrayOutputStream();
message.save(messageStream);
}
// Call this method recursively for each subfolder
if (folderInfo.hasSubFolders() == true) {
for (FolderInfo subfolderInfo : folderInfo.getSubFolders()) {
extractMsgFiles(subfolderInfo, pst);
}
}
}
שמירת הודעות ישירות מ‑PST ל‑Stream
כדי לשמור הודעות מקובץ PST ישירות ל‑stream, ללא חילוץ MsgInfo עבור ההודעות, השתמש ב‑ saveMessageToStream() שיטה. קטע הקוד הבא מראה כיצד לשמור הודעות ישירות מ‑PST ל‑stream.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";
// Load the Outlook file
String path = dataDir + "PersonalStorage.pst";
// Save message to MemoryStream
try (PersonalStorage personalStorage = PersonalStorage.fromFile(path)) {
FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");
for (MessageInfo messageInfo : inbox.enumerateMessages()) {
try (ByteArrayOutputStream memeorystream = new ByteArrayOutputStream()) {
personalStorage.saveMessageToStream(messageInfo.getEntryIdString(), memeorystream);
}
}
}
// Save message to file
try (PersonalStorage pst = PersonalStorage.fromFile(path)) {
FolderInfo inbox = pst.getRootFolder().getSubFolder("Inbox");
for (MessageInfo messageInfo : inbox.enumerateMessages()) {
try (FileOutputStream fs = new FileOutputStream(new File(dataDir + messageInfo.getSubject() + ".msg"))) {
pst.saveMessageToStream(messageInfo.getEntryIdString(), fs);
}
}
}
try (PersonalStorage pst = PersonalStorage.fromFile(path)) {
FolderInfo inbox = pst.getRootFolder().getSubFolder("Inbox");
// To enumerate entryId of messages you may use FolderInfo.EnumerateMessagesEntryId() method:
for (String entryId : inbox.enumerateMessagesEntryId()) {
try (ByteArrayOutputStream ms = new ByteArrayOutputStream()) {
pst.saveMessageToStream(entryId, ms);
}
}
}
חילוץ n הודעות מקובץ PST
קטע הקוד הבא מראה כיצד לחלץ מספר מסוים של הודעות מ‑PST. פשוט ספק את האינדקס של ההודעה הראשונה ואת מספר ההודעות הכולל שברצונך לחלץ.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");
// Extracts messages starting from 10th index top and extract total 100 messages
MessageInfoCollection messages = inbox.getContents(10, 100);
קבלת המספר הכולל של פריטים מקובץ PST
Aspose.Email מספקת את GetTotalItemsCount() שיטה של PersonalStorage.Store מאפיין. הוא מחזיר את המספר הכולל של פריטי הודעה הכלולים ב‑PST.
דוגמת הקוד הבאה מראה כיצד לאחזר את הספירה הכוללת של פריטים (הודעות, פגישות, אנשי קשר וכו’) המאוכסנים בקובץ PST:
try (PersonalStorage pst = PersonalStorage.fromFile("my.pst", false)) {
int count = pst.getStore().getTotalItemsCount();
}
מחיקת פריטים מקבצי PST
Add Messages to PST Files הראה כיצד להוסיף הודעות לקבצי PST. כמובן שניתן גם למחוק פריטים (תוכן) מקובץ PST ולעיתים רצוי למחוק הודעות במקצה. ניתן למחוק פריטים מקובץ PST באמצעות FolderInfo.deleteChildItem() שיטה. ה‑API גם מציע FolderInfo.deleteChildItems() שיטה למחיקת פריטים במקצה מתוך קובץ PST.
מחיקת הודעות מקבצי PST
מאמר זה מראה כיצד להשתמש ב‑ FolderInfo מחלקה לגישה לתיקיות ספציפיות בקובץ PST. למחיקת הודעות מתת‑תיקיית Sent של קובץ PST שכבר נטען או נוצר:
- צור מופע של FolderInfo מחלקה וטעון אותה בתוכן של תת‑תיקיית Sent.
- מחק הודעות מתיקיית Sent על ידי קריאה ל‑ FolderInfo.deleteChildItem() שיטה והעברת ה‑ MessageInfo.EntryId כפרמטר. קטע הקוד הבא מראה כיצד למחוק הודעות מתיקיית תת‑תיקיית Sent בקובץ PST.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/" + "Sub.pst";
// Load the Outlook PST file
PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir);
// Get the Sent items folder
FolderInfo folderInfo = personalStorage.getPredefinedFolder(StandardIpmFolder.SentItems);
MessageInfoCollection msgInfoColl = folderInfo.getContents();
for (MessageInfo msgInfo : msgInfoColl) {
System.out.println(msgInfo.getSubject() + ": " + msgInfo.getEntryIdString());
if (msgInfo.getSubject().equals("some delete condition")) {
// Delete this item
folderInfo.deleteChildItem(msgInfo.getEntryId());
System.out.println("Deleted this message");
}
}
מחיקת תיקיות מקבצי PST
ניתן למחוק תיקיית PST על ידי העברתها לתיקיית הפריטים המחוקים.
try (PersonalStorage pst = PersonalStorage.fromFile("test.pst")) {
FolderInfo deletedItemsFolder = pst.getPredefinedFolder(StandardIpmFolder.DeletedItems);
FolderInfo emptyFolder = pst.getRootFolder().getSubFolder("Empty folder");
FolderInfo someFolder = pst.getRootFolder().getSubFolder("Some folder");
pst.moveItem(emptyFolder, deletedItemsFolder);
pst.moveItem(someFolder, deletedItemsFolder);
}
היתרון של שיטה זו הוא שניתן לשחזר בקלות את התיקייה המחוקה.
FolderInfo someFolder = deletedItemsFolder.getSubFolder("Some folder");
pst.moveItem(someFolder, pst.getRootFolder());
אתה יכול גם להסיר לצמיתות תיקייה מתיקיית הפריטים המחוקים, אם יש צורך.
deletedItemsFolder.deleteChildItem(emptyFolder.getEntryId());
ה deleteChildItem() שיטה זו ניתנת לשימוש עבור כל תיקייה אם ברצונך למחוק מיידית ובצורה קבועה תיקיית משנה, תוך עקיפת תיקיית הפריטים המחוקים.
FolderInfo someFolder = pst.getRootFolder().getSubFolder("Some folder");
pst.getRootFolder().deleteChildItem(someFolder.getEntryId());
מחיקת פריטים במקצה מתוך קובץ PST
ניתן להשתמש ב‑Aspose.Email API למחיקת פריטים במקצה מתוך קובץ PST. זה מתבצע באמצעות ה‑ deleteChildItems() שיטה שמקבלת רשימה של פריטי Entry ID המתייחסים לפריטים שברצונך למחוק. קטע הקוד הבא מראה כיצד למחוק פריטים במקצה מתוך קובץ PST.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/" + "Sub.pst";
try (PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir)) {
// Get Inbox SubFolder from Outlook file
FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");
// Create instance of PersonalStorageQueryBuilder
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.getFrom().contains("someuser@domain.com");
MessageInfoCollection messages = inbox.getContents(queryBuilder.getQuery());
List<String> deleteList = new ArrayList<String>();
for (MessageInfo messageInfo : messages) {
deleteList.add(messageInfo.getEntryIdString());
}
// delete messages having From = "someuser@domain.com"
inbox.deleteChildItems(deleteList);
}
סיוג ושחזור הודעות שנמחקו רך מ‑PST
ניתן לאחזר הודעות מנוחות שנמחקו לצמיתות באמצעות Aspose.Email for Java. ה‑API שלו מאפשר למנות הודעות שנמחקו רך בקבצי PST. פריטים שנמחקו רך הם הודעות שנמחקו פעמיים - תחילה הועברו לתיקיית פריטים מחוקים ואז הוסרו משם. הודעות אלה עדיין ניתנות לשחזור, ו‑Aspose.Email מציע דרך נוחה לגשת אליהן. ה‑ PersonalStorage.findAndEnumerateSoftDeletedItems() השיטה מחזירה אוסף של RestoredItemEntry אובייקטים. כל ערך מכיל:
- MapiMessage item — ההודעה המשוחזרת.
- String FolderId - המזהה של התיקייה שאליה המקורית ההודעה השתייכה.
דוגמת הקוד הבאה מדגימה כיצד למנות פריטי אימייל שנמחקו באופן רך (ניתנים לשחזור) בקבצי PST:
// Load the PST file
try (PersonalStorage pst = PersonalStorage.fromFile(fileName)) {
// Enumerate soft-deleted items
for (RestoredItemEntry entry : pst.findAndEnumerateSoftDeletedItems()) {
MapiMessage message = entry.getItem();
String folderId = entry.getFolderId();
System.out.println("Subject: " + message.getSubject());
System.out.println("Deleted from Folder ID: " + folderId);
System.out.println("-----------------------------------");
}
}
חיפוש הודעות ותיקיות ב‑PST לפי קריטריון
קובצי Personal Storage (PST) יכולים להכיל כמות עצומה של נתונים וחיפוש אחר נתונים העונים על קריטריונים ספציפיים בקבצים גדולים כאלה דורש לכלול נקודות בדיקה מרובות בקוד כדי לסנן את המידע. עם ה‑ PersonalStorageQueryBuilder מחלקה, Aspose.Email מאפשר לחפש רשומות ספציפיות ב‑PST על בסיס קריטריונים מוגדרים. ניתן לחפש הודעות ב‑PST לפי פרמטרי חיפוש כגון שולח, נמען, נושא, חשיבות הודעה, קיום קבצים מצורפים, גודל הודעה ואף מזהה הודעה. ה‑ PersonalStorageQueryBuilder גם ניתן להשתמש בו לחיפוש תיקיות משנה.
חיפוש הודעות ותיקיות ב‑PST
קטעת הקוד הבא מראה לך כיצד להשתמש ב- PersonalStorageQueryBuilder מחלקה לחיפוש תוכן ב‑PST בהתבסס על קריטריונים שונים. לדוגמה, מציג חיפוש PST על בסיס:
- חשיבות ההודעה.
- מחלקת הודעה.
- קיום קבצים מצורפים.
- גודל הודעה.
- תאריך הודעה.
- הודעות שלא נקראו.
- הודעות שלא נקראו עם קבצים מצורפים, ו-
- תיקיות עם שם תיקיית משנה ספציפית.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";
try (PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir + "Outlook.pst")) {
FolderInfo folder = personalStorage.getRootFolder().getSubFolder("Inbox");
PersonalStorageQueryBuilder builder = new PersonalStorageQueryBuilder();
// High importance messages
builder.getImportance().equals((int) MapiImportance.High);
MessageInfoCollection messages = folder.getContents(builder.getQuery());
System.out.println("Messages with High Imp:" + messages.size());
builder = new PersonalStorageQueryBuilder();
builder.getMessageClass().equals("IPM.Note");
messages = folder.getContents(builder.getQuery());
System.out.println("Messages with IPM.Note:" + messages.size());
builder = new PersonalStorageQueryBuilder();
// Messages with attachments AND high importance
builder.getImportance().equals((int) MapiImportance.High);
builder.hasFlags(MapiMessageFlags.MSGFLAG_HASATTACH);
messages = folder.getContents(builder.getQuery());
System.out.println("Messages with atts: " + messages.size());
builder = new PersonalStorageQueryBuilder();
// Messages with size > 15 KB
builder.getMessageSize().greater(15000);
messages = folder.getContents(builder.getQuery());
System.out.println("messags size > 15Kb:" + messages.size());
java.util.Calendar c = java.util.Calendar.getInstance();
builder = new PersonalStorageQueryBuilder();
// Messages by Current Date
// (Note that queries by date are not supported for Calendar Items in the Appointments folder)
builder.getSentDate().on(c.getTime(), DateComparisonType.ByDate);
messages = folder.getContents(builder.getQuery());
System.out.println("Messages by Current Date: " + messages.size());
builder = new PersonalStorageQueryBuilder();
// Messages between Dates
// (Note that queries by date are not supported for Calendar Items in the Appointments folder)
c.set(2020, 0, 1, 0, 0, 0);
builder.getSentDate().since(c.getTime());
c.set(2021, 0, 1, 0, 0, 0);
builder.getSentDate().before(c.getTime());
messages = folder.getContents(builder.getQuery());
System.out.println("Messages between Dates: " + messages.size());
builder = new PersonalStorageQueryBuilder();
// Unread messages
builder.hasNoFlags(MapiMessageFlags.MSGFLAG_READ);
messages = folder.getContents(builder.getQuery());
System.out.println("Unread:" + messages.size());
builder = new PersonalStorageQueryBuilder();
// Unread messages with attachments
builder.hasNoFlags(MapiMessageFlags.MSGFLAG_READ);
builder.hasFlags(MapiMessageFlags.MSGFLAG_HASATTACH);
messages = folder.getContents(builder.getQuery());
System.out.println("Unread msgs with atts: " + messages.size());
// Folder with name of 'SubInbox'
builder = new PersonalStorageQueryBuilder();
builder.getFolderName().equals("SubInbox");
FolderInfoCollection folders = folder.getSubFolders(builder.getQuery());
System.out.println("Folder having subfolder: " + folders.size());
builder = new PersonalStorageQueryBuilder();
// Folders with subfolders
builder.hasSubfolders();
folders = folder.getSubFolders(builder.getQuery());
System.out.println(folders.size());
}
חיפוש מחרוזת ב‑PST עם פרמטר ההתעלמות מרגישות לאותיות
קטע הקוד הבא מראה כיצד לחפש מחרוזת ב‑PST עם פרמטר ההתעלמות מרגישות לאותיות גדולות/קטנות.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";
try (PersonalStorage personalStorage = PersonalStorage.create(dataDir + "CaseSensitivity.pst", FileFormatVersion.Unicode)) {
FolderInfo folderinfo = personalStorage.createPredefinedFolder("Inbox", StandardIpmFolder.Inbox);
folderinfo.addMessage(MapiMessage.fromMailMessage(MailMessage.load("Sample.eml")));
PersonalStorageQueryBuilder builder = new PersonalStorageQueryBuilder();
// IgnoreCase is True
builder.getFrom().contains("automated", true);
MailQuery query = builder.getQuery();
MessageInfoCollection coll = folderinfo.getContents(query);
System.out.println(coll.size());
}
חיפוש נושאי הודעות לפי מילות מפתח מרובות בקובץ PST
אתה יכול להשתמש ב MailQueryBuilder.or שיטה למציאת הודעות עם נושא המכיל לפחות אחד מהמילים המוגדרות כפי שמוצג למטה:
PersonalStorageQueryBuilder builder1 = new PersonalStorageQueryBuilder();
builder1.getSubject().contains("Review"); // 'Review' is key word for the search
PersonalStorageQueryBuilder builder2 = new PersonalStorageQueryBuilder();
builder2.getSubject().contains("Error"); // 'Error' is also key word for the search
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.or(builder1.getQuery(), builder2.getQuery()); // message subjects must contain 'Review' or 'Error' words
try (PersonalStorage storage = PersonalStorage.fromFile("example.pst"))
{
FolderInfo folderInfo = storage.getRootFolder().getSubFolder("Inbox");
MessageInfoCollection messageInfos = folderInfo.getContents(queryBuilder.getQuery());
for (MessageInfo messageInfo : messageInfos)
{
System.out.println(messageInfo.getSubject());
}
}
שליפה מדורגת של תוכן תיקיית PST
שפר את ביצועי האפליקציה והשליטה בעת ניווט בתיקיות גדולות. שלוף את תוכן התיקייה בצורה מדורגת באמצעות העמסת Aspose.Email של getContents שיטה. שיטה זו - FolderInfo.getContents(MailQuery query, int startIndex, int count) - מאחזר תת‑קבוצה של הודעות המתאימות לשאילתה המוגדרת, מתחיל מאינדקס מסוים ומוגבל במספר. דוגמת הקוד הבאה מדגימה שליפה בעמודים ועיבוד של הודעות מסוננות מתיקיית PST:
// Load the PST file
try (PersonalStorage pst = PersonalStorage.fromFile(fileName)) {
// Access a specific subfolder
FolderInfo folder = pst.getRootFolder().getSubFolder("Inbox");
// Build a query to filter messages by sender address
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.getFrom().contains("report-service", true);
// Define the page size
int pageSize = 5;
// Retrieve and process messages in pages
for (int pageIndex = 0; pageIndex < 6; pageIndex++) {
int startIndex = pageIndex * pageSize;
// Get a page of messages
MessageInfoCollection messages = folder.getContents(queryBuilder.getQuery(), startIndex, pageSize);
for (MessageInfo messageInfo : messages) {
// Output basic info about each message
System.out.println("Subject: " + messageInfo.getSubject() + ", Sender: " + messageInfo.getSenderRepresentativeName());
}
}
}
העברת פריטים לתיקיות אחרות בקובץ PST
Aspose.Email מאפשר להעביר פריטים מתיקייה מקור לתיקייה אחרת באותו קובץ אחסון אישי (PST). זה כולל:
- העברת תיקייה ספציפית לתיקיית אב חדשה.
- העברת הודעה ספציפית לתיקייה חדשה.
- העברת התוכן לתיקייה חדשה.
- העברת תיקיות משנה לתיקיית אב חדשה.
קטע הקוד הבא מראה כיצד להעביר פריטים כגון הודעות ותיקיות מתיקייה מקור לתיקייה אחרת באותו קובץ PST.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
try (PersonalStorage personalStorage = PersonalStorage.fromFile("test.pst")) {
FolderInfo inbox = personalStorage.getPredefinedFolder(StandardIpmFolder.Inbox);
FolderInfo deleted = personalStorage.getPredefinedFolder(StandardIpmFolder.DeletedItems);
FolderInfo subfolder = inbox.getSubFolder("Subfolder");
// Move folder and message to the Deleted Items
personalStorage.moveItem(subfolder, deleted);
MessageInfoCollection contents = subfolder.getContents();
personalStorage.moveItem(contents.get(0), deleted);
// Move all inbox subfolders and subfolder contents to the Deleted Items
inbox.moveSubfolders(deleted);
subfolder.moveContents(deleted);
}
עדכון מאפייני הודעה בקובץ PST
לעיתים יש צורך לעדכן תכונות מסוימות של הודעות כגון שינוי הנושא, סימון חשיבות ההודעה וכדומה. עדכון הודעה בקובץ PST עם שינויים אלו בתכונות ההודעה ניתן לבצע באמצעות FolderInfo.changeMessages מתודה. מאמר זה מציג כיצד לעדכן הודעות במצב גורף בקובץ PST עבור שינויי תכונות. קוד הדוגמה הבא מראה כיצד לעדכן תכונות של הודעות במצב גורף למספר הודעות בקובץ PST.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/" + "Sub.pst";
// Load the Outlook PST file
PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir);
// Get Requierd Subfolder
FolderInfo inbox = personalStorage.getRootFolder().getSubFolder("Inbox");
// find messages having From = "someuser@domain.com"
PersonalStorageQueryBuilder queryBuilder = new PersonalStorageQueryBuilder();
queryBuilder.getFrom().contains("someuser@domain.com");
// Get Contents from Query
MessageInfoCollection messages = inbox.getContents(queryBuilder.getQuery());
// Save (MessageInfo,EntryIdString) in List
List<String> changeList = new ArrayList<String>();
for (MessageInfo messageInfo : messages) {
changeList.add(messageInfo.getEntryIdString());
}
// Compose the new properties
MapiPropertyCollection updatedProperties = new MapiPropertyCollection();
updatedProperties.add(MapiPropertyTag.PR_SUBJECT_W, new MapiProperty(MapiPropertyTag.PR_SUBJECT_W, "New Subject".getBytes(Charset.forName("utf-16le"))));
updatedProperties.add(MapiPropertyTag.PR_IMPORTANCE, new MapiProperty(MapiPropertyTag.PR_IMPORTANCE, BitConverter.getBytesInt64(2)));
// update messages having From = "someuser@domain.com" with new properties
inbox.changeMessages(changeList, updatedProperties);
עדכון תכונות מותאמות אישית בקובץ PST
לעיתים יש צורך לסמן פריטים שעובדו בתוך קובץ PST. API של Aspose.Email מאפשר לבצע זאת באמצעות MapiProperty ו‑MapiNamedProperty. השיטות הבאות מסייעות למימוש זאת.
- constructor MapiNamedProperty(long propertyTag, String nameIdentifier, UUID propertyGuid, byte[] propertyValue)
- constructor MapiNamedProperty(long propertyTag, long nameIdentifier, UUID propertyGuid, byte[] propertyValue)
- FolderInfo.changeMessages(MapiPropertyCollection updatedProperties) - changes all messages in folder
- PersonalStorage.changeMessage(String entryId, MapiPropertyCollection updatedProperties) - change message properties
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
public static void run() {
// The path to the File directory.
String dataDir = "data/" + "Sub.pst";
try (PersonalStorage personalStorage = PersonalStorage.fromFile(dataDir)) {
FolderInfo testFolder = personalStorage.getRootFolder().getSubFolder("Inbox");
// Create the collection of message properties for adding or updating
MapiPropertyCollection newProperties = new MapiPropertyCollection();
// Normal, Custom and PidLidLogFlags named property
MapiProperty property = new MapiProperty(MapiPropertyTag.PR_ORG_EMAIL_ADDR_W, "test_address@org.com".getBytes(Charset.forName("utf-16le")));
MapiProperty namedProperty1 = new MapiNamedProperty(generateNamedPropertyTag(0L, MapiPropertyType.PT_LONG), "ITEM_ID", UUID.randomUUID(),
BitConverter.getBytesInt64(123));
MapiProperty namedProperty2 = new MapiNamedProperty(generateNamedPropertyTag(1L, MapiPropertyType.PT_LONG), 0x0000870C,
UUID.fromString("0006200A-0000-0000-C000-000000000046"), BitConverter.getBytesInt64(0));
newProperties.add(namedProperty1.getTag(), namedProperty1);
newProperties.add(namedProperty2.getTag(), namedProperty2);
newProperties.add(property.getTag(), property);
testFolder.changeMessages(testFolder.enumerateMessagesEntryId(), newProperties);
}
}
private static long generateNamedPropertyTag(long index, int dataType) {
return (((0x8000 | index) << 16) | (long) dataType) & 0x00000000FFFFFFFFL;
}
חילוץ קבצים מצורפים ללא חילוץ ההודעה המלאה
ניתן להשתמש ב‑API של Aspose.Email כדי לחלץ קבצים מצורפים מהודעות PST ללא חילוץ ההודעה המלאה תחילה. ה‑ ExtractAttachments מתודה של PersonalStorage יכולים לשמש לכך. קוד הדוגמה הבא מראה כיצד לחלץ קבצים מצורפים בלי לחלץ את ההודעה המלאה.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";
try (PersonalStorage personalstorage = PersonalStorage.fromFile(dataDir + "Outlook.pst")) {
FolderInfo folder = personalstorage.getRootFolder().getSubFolder("Inbox");
for (String messageInfo : folder.enumerateMessagesEntryId()) {
MapiAttachmentCollection attachments = personalstorage.extractAttachments(messageInfo);
if (attachments.size() != 0) {
for (MapiAttachment attachment : attachments) {
if (attachment.getLongFileName() != null && !attachment.getLongFileName().isEmpty()) {
if (attachment.getLongFileName().contains(".msg")) {
continue;
} else {
attachment.save(dataDir + "Attachments/" + attachment.getLongFileName());
}
}
}
}
}
}
הוספת קבצים ל‑PST
הפונקציונליות המרכזית של Microsoft Outlook היא ניהול אימיילים, לוחות שנה, משימות, אנשי קשר ורשומות ביומן. בנוסף, ניתן להוסיף קבצים לתיקיית PST וה‑PST שנוצר מתעד את המסמכים שנוספו. Aspose.Email מספקת אפשרות להוסיף קבצים לתיקייה באותו האופן בנוסף להוספת הודעות, אנשי קשר, משימות ורשומות ביומן ל‑PST. קוד הדוגמה הבא מראה כיצד להוסיף מסמכים לתיקיית PST בעזרת Aspose.Email.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "data/";
try (PersonalStorage personalStorage = PersonalStorage.create(dataDir + "Ps1_out.pst", FileFormatVersion.Unicode)) {
FolderInfo folder = personalStorage.getRootFolder().addSubFolder("Files");
// Add Document.doc file with the "IPM.Document" message class by default.
folder.addFile(dataDir + "attachment_1.doc", null);
}