การทำงานกับข้อความในไฟล์ PST

Contents
[ ]

เพิ่มข้อความไปยังไฟล์ PST ของ Outlook

เพิ่มข้อความเดียวลงในไฟล์ PST

สร้างไฟล์ PST ใหม่และเพิ่มโฟลเดอร์ย่อย แสดงวิธีสร้างไฟล์ PST และเพิ่มโฟลเดอร์ย่อยลงในนั้น ด้วย Aspose.Email คุณสามารถเพิ่มข้อความไปยังโฟลเดอร์ย่อยของไฟล์ PST ที่คุณสร้างหรือโหลดได้ ตัวอย่างโค้ดด้านล่างแสดงวิธีสร้างไฟล์ PST ใหม่, เพิ่มโฟลเดอร์ "Inbox", แล้วเพิ่มข้อความไปยังโฟลเดอร์นั้น. ส่วนที่ PersonalStorage และ FolderInfo คลาสที่ใช้ในการทำงานนี้.

  1. ใช้ PersonalStorage.create เมธอดเพื่อเริ่มไฟล์ PST ใหม่โดยระบุเส้นทางไฟล์และเวอร์ชันรูปแบบไฟล์เป็น Unicode.
  2. สร้างโฟลเดอร์ใหม่ชื่อ "Inbox" ในไดเรกทอรีรากของไฟล์ PST.
  3. เพิ่มข้อความไปยังโฟลเดอร์ "Inbox" ที่สร้างใหม่โดยใช้ add_message เมธอด.
  4. โหลดข้อความโดยใช้ MapiMessage.load.
pst = PersonalStorage.create(dataDir + "AddMessagesToPst_out.pst", FileFormatVersion.UNICODE)

# Add new folder "Inbox"
inboxFolder = pst.root_folder.add_sub_folder("Inbox");

# Add message to Inbox Folder
inboxFolder.add_message(MapiMessage.load(dataDir + "MapiMsgWithPoll.msg"))

pst.dispose()

เพิ่มหลายข้อความลงในไฟล์ PST เพื่อประสิทธิภาพที่ดีกว่า

การเพิ่มข้อความแต่ละรายการลงใน PST ทำให้มีการดำเนินการ I/O ไปยังดิสก์มากขึ้นจึงอาจทำให้ประสิทธิภาพช้าลง เพื่อเพิ่มประสิทธิภาพ สามารถเพิ่มข้อความลงใน PST เป็นโหมดกลุ่มและลดการดำเนินการ I/O.

โหลดและเพิ่มข้อความจากดิสก์

นี้ add_messages เมธอดนี้อนุญาตให้คุณกำหนดช่วงของข้อความที่จะเพิ่มลงในไฟล์ PST ตัวอย่างโค้ดด้านล่างแสดงวิธีเพิ่มหลายข้อความจากดิสก์ไปยังไฟล์ PST เพื่อปรับปรุงประสิทธิภาพ:

  1. กำหนดฟังก์ชันโดยการสร้าง add_messages_in_bulk_mode พร้อมพารามิเตอร์สำหรับชื่อไฟล์ PST และโฟลเดอร์ที่มีข้อความ.
  2. เปิดไฟล์ PST ที่ระบุโดยใช้ PersonalStorage.from_file().
  3. เรียกโฟลเดอร์ย่อย "myInbox" จากโฟลเดอร์รากของ PST.
  4. เพิ่มข้อความจากโฟลเดอร์ที่ระบุเป็นกลุ่มโดยใช้ folder.add_messages().
  5. เรียก add_messages_in_bulk_mode() กับไฟล์ PST และชื่อโฟลเดอร์เป็นอาร์กิวเมนต์.
from aspose.email.storage.pst import PersonalStorage, StandardIpmFolder, FileFormatVersion

def add_messages_in_bulk_mode(file_name, msg_folder_name):
    with PersonalStorage.from_file(file_name) as personal_storage:
        folder = personal_storage.root_folder.get_sub_folder("myInbox")
        folder.add_messages(message_collection(msg_folder_name))

# Add multiple messages from the specified folder to the PST file for improved performance
add_messages_in_bulk_mode("file.pst", "folder_with_messages")

ใช้ MapiMessageEnumerator สำหรับการดำเนินการแบบกลุ่ม

เพื่อทำให้การประมวลผลข้อความจำนวนมากเป็นระบบ คุณสามารถใช้งาน MapiMessageEnumerator คลาสที่วนซ้ำอย่างมีประสิทธิภาพผ่านข้อความที่เก็บในโฟลเดอร์ที่ระบุ ตัวอย่างสคริปต์ Python ต่อไปนี้ให้แนวทางโครงสร้างในการนับและวนซ้ำข้อความ MAPI โดยใช้ไลบรารี Aspose.Email โดยกำหนดคลาสช่วยเหลือสองคลาส:

  • MapiMessageEnumerator สำหรับการอ่านข้อความจากไดเรกทอรี,

  • และ MapiMessageCollection สำหรับการจัดการพวกมันในกระบวนการทำงานเป็นชุด.

วิธีการนี้อำนวยความสะดวกในการเดินทางและจัดการไฟล์ข้อความ MAPI.

import os
from aspose.email.mapi import MapiMessage

# Define a class to enumerate through MAPI message files in a directory
class MapiMessageEnumerator:
    def __init__(self, path):
        self.files = os.listdir(path)
        self.position = -1

    def __next__(self):
        self.position += 1
        if self.position < len(self.files):
            return MapiMessage.from_file(os.path.join(self.path, self.files[self.position]))
        else:
            raise StopIteration

    def __iter__(self):
        return self

# Define a collection class for managing MAPI messages
class MapiMessageCollection:
    def __init__(self, path):
        self.path = path

    def __iter__(self):
        return MapiMessageEnumerator(self.path)


# Iterate through MAPI messages in a specific directory
msg_folder_name = "\\Files\\msg"

# Initialize a collection with the directory containing message files
message_collection = MapiMessageCollection(msg_folder_name)
for message in message_collection:
    # Process each MAPI message object as needed
    pass

เพิ่มข้อความจาก PST อื่น

เพื่อนำเข้าข้อความจากไฟล์ PST หนึ่งไปยังอีกไฟล์หนึ่ง Aspose.Email มีให้ FolderInfo.enumerate_mapi_messages() เมธอด ตัวอย่างโค้ดต่อไปนี้แสดงวิธีคัดลอกข้อความจากโฟลเดอร์ "Inbox" ในไฟล์ PST หนึ่งไปยังไฟล์ PST อีกไฟล์หนึ่ง:

ดึงข้อความจากไฟล์ PST ของ Outlook

ใน อ่านไฟล์ Outlook PST, ดึงข้อมูลโฟลเดอร์และโฟลเดอร์ย่อย, เราได้อภิปรายเกี่ยวกับการโหลดไฟล์ PST ของ Outlook และเรียกดูโฟลเดอร์ต่าง ๆ เพื่อรับชื่อโฟลเดอร์และจำนวนข้อความในแต่ละโฟลเดอร์ บทความนี้อธิบายวิธีเข้าถึงและสกัดข้อความจากไฟล์ PST ของ Outlook: ดึงรายละเอียดพื้นฐานของข้อความ, นับจำนวนรายการในโฟลเดอร์, และสกัดข้อความจำนวนหนึ่งสำหรับการประมวลผลหรือวิเคราะห์.

ดึงข้อมูลพื้นฐานของข้อความ

ตัวอย่างโค้ดด้านล่างแสดงวิธีสกัดและแสดงข้อมูลสำคัญจากข้อความ MAPI ที่เก็บไว้ในไฟล์ PST โดยใช้ไลบรารี Aspose.Email มันเริ่มต้น PersonalStorage วัตถุจากไฟล์ "Outlook.pst" ดึงเนื้อหาของโฟลเดอร์รากและวนซ้ำผ่านแต่ละข้อความ สคริปต์พิมพ์รายละเอียดเช่น หัวเรื่อง, ข้อมูลผู้ส่ง, ที่อยู่อีเมลผู้รับ, เวลาส่ง, และเนื้อความของข้อความ ให้ภาพรวมที่ครอบคลุมของแต่ละอีเมลในไฟล์ PST ที่ระบุ.

from aspose.email.storage.pst import *
from aspose.email.mapi import MapiMessage

pst = PersonalStorage.from_file("Outlook.pst")

folderInfo = pst.root_folder

messageInfoCollection = folderInfo.get_contents()

for messageInfo in messageInfoCollection:
   mapi = pst.extract_message(messageInfo)

   print("Subject: " + mapi.subject)
   print("Sender name: " + mapi.sender_name)
   print("Sender email address: " + mapi.sender_email_address)
   print("To: ", mapi.display_to)
   print("Cc: ", mapi.display_cc)
   print("Bcc: ", mapi.display_bcc)
   print("Delivery time: ", str(mapi.delivery_time))
   print("Body: " + mapi.body)

การอ่านโฟลเดอร์ซ้อนแบบเรียกซ้ำ

ไฟล์ PST ของ Outlook อาจมีโฟลเดอร์ซ้อนกัน เพื่อดึงข้อมูลข้อความจากโฟลเดอร์เหล่านี้รวมถึงโฟลเดอร์ระดับบน ใช้วิธีการเรียกซ้ำเพื่ออ่านทุกโฟลเดอร์ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอ่านไฟล์ PST ของ Outlook และแสดงเนื้อหาโฟลเดอร์และข้อความอย่างเรียกซ้ำ:

เรียกคืนจำนวนรายการทั้งหมดในโฟลเดอร์ PST

เพื่อดึงจำนวนรายการทั้งหมด (เช่น อีเมล, นัดหมาย, งาน, ผู้ติดต่อ ฯลฯ) ที่อยู่ในที่เก็บข้อความ ใช้เมธอด get_total_items_count() ของ คลังข้อความ คลาส ให้วิธีที่สะดวกในการรวบรวมข้อมูลขนาดและปริมาณของข้อมูลภายในที่เก็บ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรับจำนวนรายการทั้งหมดจากไฟล์ PST:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

count = pst.store.get_total_items_count()

สกัดจำนวนข้อความที่ระบุ

เพื่อสกัดจำนวนข้อความที่ระบุจากไฟล์ PST ใช้เมธอด get_contents(start_index, count) ของ FolderInfo คลาส เหมาะกับพารามิเตอร์สองค่า:

  • start_index - หมายเลขของข้อความเริ่มต้น, ตัวอย่างเช่นข้อความที่ 10;
  • count - จำนวนข้อความทั้งหมดที่ต้องดึง.

การดึงส่วนย่อยของข้อความที่จำเป็นเท่านั้นในแต่ละครั้งอาจเป็นประโยชน์ในการจัดการข้อมูลอีเมลปริมาณมาก ตัวอย่างโค้ดต่อไปนี้แสดงการใช้งานคุณลักษณะนี้:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

folder = pst.root_folder.get_sub_folder("Inbox")

# Extracts messages starting from 10th index top and extract total 100 messages
messages = folder.get_contents(10, 100)

ทำงานกับไฟล์แนบในไฟล์ PST

สกัดไฟล์แนบโดยไม่ต้องสกัดข้อความทั้งหมด

Aspose.Email สำหรับ Python อนุญาตให้สกัดไฟล์แนบจากข้อความ PST โดยไม่ต้องสกัดข้อความทั้งหมดก่อน ซึ่งสามารถทำได้โดยใช้ extract_attachments เมธอดของ PersonalStorage คลาส ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสกัดไฟล์แนบโดยข้ามไฟล์ .msg:

from aspose.email.storage.pst import PersonalStorage

# Open the PST file 
with PersonalStorage.from_file(data_dir + "my.pst") as personal_storage:
    # Get the "Inbox" subfolder from the root folder in the personal storage
    folder = personal_storage.root_folder.get_sub_folder("Inbox")

    # Iterate over each message entry ID in the Inbox folder
    for message_info in folder.enumerate_messages_entry_id():
        # Extract attachments for the current message
        attachments = personal_storage.extract_attachments(message_info)

        # Check if the message has any attachments
        if attachments.count > 0:
            # Iterate over each attachment in the list
            for attachment in attachments:
                # Ignore attachments that are message files (.msg)
                if attachment.long_file_name and attachment.long_file_name.endswith(".msg"):
                    continue
                # Save the attachment with its original file name
                attachment.save(data_dir + attachment.long_file_name)

เพิ่มไฟล์เป็นไฟล์แนบในข้อความ PST

Microsoft Outlook เป็นเครื่องมือที่ทรงพลังสำหรับจัดการอีเมล, ปฏิทิน, งาน, ผู้ติดต่อ, และบันทึกบันทึกเหตุการณ์ นอกเหนือจากฟังก์ชันหลักเหล่านี้ ยังอนุญาตให้เพิ่มไฟล์ลงในโฟลเดอร์ PST เพื่อให้ผู้ใช้เก็บบันทึกเอกสารที่เกี่ยวข้องอย่างครบถ้วน Aspose.Email ทำให้กระบวนการเพิ่มไฟล์ลงในโฟลเดอร์ PST ง่ายขึ้น ทำงานคล้ายกับการจัดการข้อความ, ผู้ติดต่อ, งาน, และบันทึกเหตุการณ์.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มเอกสารเข้าไปยังโฟลเดอร์ PST ด้วย Aspose.Email:

from aspose.email.storage.pst import PersonalStorage, FileFormatVersion

# Create a new PST file
personal_storage = PersonalStorage.create(data_dir + "AddFilesToPst_out.pst", FileFormatVersion.UNICODE)

# Add a new folder to store files
folder = personal_storage.root_folder.add_sub_folder("Files")

# Add a file to the PST folder
folder.add_file(data_dir + "FileToBeAddedToPST.txt", "")

ค้นหาและกรองข้อความในไฟล์ PST

ไฟล์ Personal Storage (PST) สามารถบรรจุข้อมูลจำนวนมากและจำเป็นต้องใช้การกรองหลายระดับเพื่อค้นหาข้อมูลที่ตรงกับเกณฑ์ที่กำหนด ด้วย PersonalStorageQueryBuilder คลาส, Aspose.Email ทำให้คุณสามารถค้นหารายการเฉพาะใน PST ตามเกณฑ์การค้นหาที่กำหนดได้ คุณสามารถค้นหาข้อความโดยใช้พารามิเตอร์เช่น ผู้ส่ง, ผู้รับ, หัวเรื่อง, ความสำคัญของข้อความ, การมีไฟล์แนบ, ขนาดข้อความ, และแม้แต่ ID ของข้อความ นอกจากนี้, PersonalStorageQueryBuilder สามารถใช้สำหรับการค้นหาภายในโฟลเดอร์ย่อย ส่วนต่อไปนี้ให้คำแนะนำที่ครอบคลุมเกี่ยวกับการค้นหาไฟล์ PST ของ Outlook.

ค้นหาข้อความและโฟลเดอร์ใน Outlook

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการใช้ PersonalStorageQueryBuilder คลาสสำหรับค้นหาสาระใน PST ตามเกณฑ์การค้นหาต่าง ๆ โดยเฉพาะแสดงการค้นหา PST ตาม:

  • ความสำคัญของข้อความ.
  • คลาสของข้อความ.
  • การมีไฟล์แนบ.
  • ขนาดของข้อความ.
  • ข้อความที่ยังไม่ได้อ่าน.
  • ข้อความที่ยังไม่ได้อ่านพร้อมไฟล์แนบ, และโฟลเดอร์ที่มีชื่อโฟลเดอร์ย่อยเฉพาะ.
from aspose.email.mapi import MapiMessageFlags
from aspose.email.storage.pst import PersonalStorage, PersonalStorageQueryBuilder, MapiImportance

with PersonalStorage.from_file(data_dir + "my.pst") as personal_storage:
    folder = personal_storage.root_folder.get_sub_folder("Inbox")
    builder = PersonalStorageQueryBuilder()

    # High importance messages
    builder.importance.equals(2)
    messages = folder.get_contents(builder.get_query())
    print("Messages with High Imp:", messages.count)

    builder = PersonalStorageQueryBuilder()
    builder.message_class.equals("IPM.Note")
    messages = folder.get_contents(builder.get_query())
    print("Messages with IPM.Note:", messages.count)

    builder = PersonalStorageQueryBuilder()
    # Messages with attachments AND high importance
    builder.importance.equals(2)
    builder.has_flags(MapiMessageFlags.HASATTACH)
    messages = folder.get_contents(builder.get_query())
    print("Messages with atts:", messages.count)

    builder = PersonalStorageQueryBuilder()
    # Messages with size > 15 KB
    builder.message_size.greater(15000)
    messages = folder.get_contents(builder.get_query())
    print("Messages size > 15 KB:", messages.count)

    builder = PersonalStorageQueryBuilder()
    # Unread messages
    builder.has_no_flags(MapiMessageFlags.READ)
    messages = folder.get_contents(builder.get_query())
    print("Unread:", messages.count)

    builder = PersonalStorageQueryBuilder()
    # Unread messages with attachments
    builder.has_no_flags(MapiMessageFlags.READ)
    builder.has_flags(MapiMessageFlags.HASATTACH)
    messages = folder.get_contents(builder.get_query())
    print("Unread msgs with atts:", messages.count)

    # Folder with name 'SubInbox'
    builder = PersonalStorageQueryBuilder()
    builder.folder_name.equals("SubInbox")
    folders = folder.get_sub_folders(builder.get_query())
    print("Folder having subfolder:", folders.count)

    builder = PersonalStorageQueryBuilder()
    # Folders with subfolders
    builder.has_subfolders()
    folders = folder.get_sub_folders(builder.get_query())
    print("Folders with subfolders:", folders.count)

ค้นหาด้วยการจับคู่แบบไม่สนใจตัวพิมพ์ใหญ่-เล็ก

โดยใช้ Aspose.Email PersonalStorageQueryBuilder, คุณสามารถระบุเงื่อนไขการค้นหาสำหรับข้อความอีเมลโดยไม่คำนึงถึงตัวพิมพ์ใหญ่-เล็ก ทำให้การค้นหามีความยืดหยุ่นและเป็นมิตรต่อผู้ใช้มากขึ้น ตัวอย่างโค้ดต่อไปนี้แสดงวิธีโหลดไฟล์ PST, เข้าถึงโฟลเดอร์ "Inbox", และใช้ฟิลเตอร์การค้นหาแบบไม่สนใจตัวพิมพ์ใหญ่-เล็กเพื่อค้นหาอีเมลตามข้อมูลผู้ส่ง ฟีเจอร์นี้ช่วยอย่างยิ่งเมื่อจัดการกับข้อมูลอีเมลที่มีการใช้ตัวอักษรหลากหลาย.

ค้นหาบรรทัดหัวเรื่องโดยใช้หลายคีย์เวิร์ด

ดึงข้อความหรือรายการเฉพาะจากไฟล์จัดเก็บส่วนบุคคล (PST) หรือแหล่งเก็บข้อความโดยใช้ either(query1, query2) เมธอดของ PersonalStorageQueryBuilder คลาส รับพารามิเตอร์สองตัวที่ให้คุณรวมสองคิวรีต่างกัน query1 และ query2 เพื่อค้นหาหัวเรื่องของข้อความที่ตรงกับคำใดคำหนึ่งจากสองคำที่ระบุ ดูตัวอย่างโค้ดด้านล่าง:

import aspose.email as ae

builder1 = ae.storage.pst.PersonalStorageQueryBuilder()
builder1.subject.contains("Review") # 'Review' is key word for the search

builder2 = ae.storage.pst.PersonalStorageQueryBuilder()
builder2.subject.contains("Error") # 'Error' is also key word for the search

builder = ae.storage.pst.PersonalStorageQueryBuilder()
# message subjects must contain 'Review' or 'Error' words
builder.either(builder1.get_query(), builder2.get_query())


pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

folder = pst.root_folder.get_sub_folder("Inbox")
messages = folder.get_contents(builder.get_query())

for message in messages:
    print(f"Message: {message.subject}")

กรองอีเมลใน PST ตามเกณฑ์เฉพาะ

ดึงเฉพาะข้อความที่ตรงกับตัวกรองเฉพาะ เช่น หัวเรื่อง, ผู้ส่ง หรือวันที่ โดยใช้ MailQuery คลาส ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ไลบรารี Aspose.Email เพื่อโหลดไฟล์ PST และกรองข้อความภายในโฟลเดอร์เฉพาะ ในกรณีนี้คือโฟลเดอร์ "Inbox":

import aspose.email as ae

# Load the PST file and access a folder
pst = ae.PersonalStorage.from_file("sample.pst")
inbox = pst.root_folder.get_sub_folder("Inbox")

# Create a MailQuery to filter messages by subject
query_builder = ae.MailQueryBuilder()
query_builder.subject.contains("Invoice")

query = query_builder.get_query()

# Enumerate filtered messages
for info in inbox.enumerate_mapi_messages(query):
    print("Subject:", info.subject)

ดึงข้อความตามประเภท

Aspose.Email MessageKind การโอเวอร์โหลดช่วยให้คุณดึงข้อความประเภทเฉพาะ เช่น อีเมลเท่านั้น, การนัดหมาย, หรือผู้ติดต่อ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเข้าถึงและกรองประเภทข้อความเฉพาะจากโฟลเดอร์ที่กำหนด:

import aspose.email as ae

# Load the PST and target folder
pst = ae.PersonalStorage.from_file("sample.pst")
inbox = pst.root_folder.get_sub_folder("Inbox")

# Retrieve only email messages (not calendar items, contacts, etc.)
for info in inbox.enumerate_mapi_messages(ae.MessageKind.MAPI_MESSAGE):
    print("Email Message:", info.subject)

แบ่งหน้าเพื่อนำข้อความจากไฟล์ PST ขนาดใหญ่

เมื่อต้องทำงานกับโฟลเดอร์ที่มีข้อความจำนวนมาก คุณสามารถใช้การแบ่งหน้าเพื่อโหลดข้อความเป็นส่วนๆ โดยใช้ start_index และ count พารามิเตอร์ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเข้าถึงและดึงช่วงเฉพาะของข้อความอีเมลจากไฟล์ PST แทนที่จะดึงทั้งหมดในครั้งเดียว:

import aspose.email as ae

# Load the PST and access the target folder
pst = ae.PersonalStorage.from_file("sample.pst")
inbox = pst.root_folder.get_sub_folder("Inbox")

# Retrieve messages from index 0 to 9
for info in inbox.enumerate_mapi_messages(0, 10):
    print("Paged Message:", info.subject)

อัปเดตและจัดระเบียบเนื้อหา Outlook PST

การย้ายข้อความระหว่างโฟลเดอร์

Aspose.Email ทำให้สามารถย้ายรายการจากโฟลเดอร์ต้นทางไปยังโฟลเดอร์อื่นในไฟล์ Personal Storage (PST) เดียวกันได้ สิ่งนี้รวมถึง:

  • การย้ายโฟลเดอร์ที่กำหนดไปยังโฟลเดอร์แม่ใหม่.
  • การย้ายข้อความที่ระบุไปยังโฟลเดอร์ใหม่.
  • การย้ายเนื้อหาไปยังโฟลเดอร์ใหม่.
  • การย้ายโฟลเดอร์ย่อยไปยังโฟลเดอร์แม่ใหม่.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการย้ายรายการเช่นข้อความและโฟลเดอร์จากโฟลเดอร์ต้นทางไปยังโฟลเดอร์อื่นในไฟล์ PST เดียวกัน.

อัปเดตคุณสมบัติของข้อความ

บางครั้งจำเป็นต้องแก้ไขคุณสมบัติบางอย่างของข้อความ เช่น การเปลี่ยนหัวเรื่อง การทำเครื่องหมายความสำคัญของข้อความ ฯลฯ การอัพเดตคุณสมบัติเหล่านี้ในไฟล์ PST สามารถทำได้โดยใช้ FolderInfo.change_messages เมธอดที่ Aspose.Email สำหรับ Python มอบให้ บทความนี้แสดงกระบวนการอัพเดตคุณสมบัติของข้อความเป็นกลุ่ม เพื่อการปรับเปลี่ยนอัตโนมัติในหลายข้อความภายในไฟล์ PST ด้านล่างมีตัวอย่างโค้ดที่แสดงวิธีทำการอัพเดตเป็นกลุ่มบนคุณสมบัติต่างๆ ของข้อความ

from aspose.email.storage.pst import PersonalStorage, PersonalStorageQueryBuilder
from aspose.email.mapi import MapiPropertyTag, MapiProperty, MapiPropertyCollection

pst_file_path = data_dir + "ya4demia04vb.pst"

# Load the Outlook PST file
with PersonalStorage.from_file(pst_file_path) as personal_storage:
    # Get the required subfolder
    inbox = personal_storage.root_folder.get_sub_folder("Inbox")

    # Find messages having From = "someuser@domain.com"
    query_builder = PersonalStorageQueryBuilder()
    query_builder.from_address.contains("someuser@domain.com")

    # Get contents from query
    messages = inbox.get_contents(query_builder.get_query())

    # Save (MessageInfo, EntryIdString) in a list
    change_list = [message_info.entry_id_string for message_info in messages]

    # Compose the new properties
    updated_properties = MapiPropertyCollection()
    updated_properties.add(
        MapiPropertyTag.SUBJECT_W,
        MapiProperty(MapiPropertyTag.SUBJECT_W, "New Subject".encode("utf-16le"))
    )
    updated_properties.add(
        MapiPropertyTag.IMPORTANCE,
        MapiProperty(MapiPropertyTag.IMPORTANCE, bytearray([0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]))
    )

    # Update messages having From = "someuser@domain.com" with new properties
    inbox.change_messages(change_list, updated_properties)

แก้ไขคุณสมบัติ MAPI แบบกำหนดเอง

บางครั้งคุณอาจต้องระบุและทำเครื่องหมายรายการที่ได้ประมวลผลในไฟล์ PST แล้ว API ของ Aspose.Email มีวิธีแก้สำหรับงานนี้โดยใช้ MapiProperty และ MapiNamedProperty คลาส เหล่านี้ช่วยให้คุณระบุรายการที่ประมวลผลแล้วโดยการกำหนดคุณสมบัติแบบกำหนดเองให้กับพวกมัน ด้านล่างคุณจะพบเมธอดที่มีประโยชน์ในการทำเครื่องหมายกระบวนการนี้:

from uuid import UUID
from aspose.email.storage.pst import PersonalStorage
from aspose.email.mapi import MapiNamedProperty, MapiPropertyCollection
from aspose.email.mapi import MapiPropertyType, MapiProperty, MapiPropertyTag

def generate_named_property_tag(index, data_type):
    return (((0x8000 | index) << 16) | data_type) & 0x00000000FFFFFFFF

def run():
    # Load the Outlook file
    pst_file_path = data_dir + "my.pst"

    with PersonalStorage.from_file(pst_file_path) as personal_storage:
        test_folder = personal_storage.root_folder.get_sub_folder("Inbox")

        # Create the collection of message properties for adding or updating
        new_properties = MapiPropertyCollection()

        # Normal, Custom, and PidLidLogFlags named properties
        mapi_property = MapiProperty(
            MapiPropertyTag.ORG_EMAIL_ADDR_W,
            "test_address@org.com".encode("utf-16le")
        )
        named_property1 = MapiNamedProperty(
            generate_named_property_tag(0, MapiPropertyType.LONG),
            "ITEM_ID",
            UUID("00000000-0000-0000-0000-000000000000"),
            bytearray([0x7B, 0x00, 0x00, 0x00])
        )
        named_property2 = MapiNamedProperty(
            generate_named_property_tag(1, MapiPropertyType.LONG),
            0x0000870C,
            UUID("0006200A-0000-0000-C000-000000000046"),
            bytearray([0x00, 0x00, 0x00, 0x00])
        )
        new_properties.add(named_property1.tag, named_property1)
        new_properties.add(named_property2.tag, named_property2)
        new_properties.add(mapi_property.tag, mapi_property)
        test_folder.change_messages(test_folder.enumerate_messages_entry_id(), new_properties)

# Usage
run()

ลบข้อความและโฟลเดอร์จากไฟล์ Outlook PST

การจัดการเนื้อหาของไฟล์ Outlook PST มักเกี่ยวข้องกับการลบข้อความ โฟลเดอร์ หรือหลายรายการพร้อมกัน Aspose.Email สำหรับ Python ผ่าน .NET มีเมธอดที่มีประสิทธิภาพเพื่อการลบข้อความและโฟลเดอร์จากไฟล์ PST ไม่ว่าจะเป็นการลบอีเมลเดี่ยว, FolderInfo.delete_child_item() เมธอด หรือทำการลบเป็นกลุ่ม, FolderInfo.delete_child_items() เมธอด เพื่อการจัดการไฟล์ที่ดียิ่งขึ้น.

ลบข้อความจากไฟล์ PST

Aspose.Email ให้ FolderInfo คลาสเพื่อเข้าถึงโฟลเดอร์เฉพาะในไฟล์ PST ตัวอย่างโค้ดด้านล่างแสดงวิธีใช้คลาสนี้เพื่อเข้าถึงและลบข้อความจากโฟลเดอร์ย่อย Sent ของไฟล์ PST ที่โหลดหรือสร้างไว้ก่อนหน้า โดยเฉพาะอย่างยิ่ง จะดึงจำนวนข้อความและลบรายการแรกในโฟลเดอร์ "Sent Items".

  1. อ็อบเจกต์ PST ถูกเริ่มต้นโดยการเปิดไฟล์ "Outlook.pst" ที่อยู่ในไดเรกทอรีที่ระบุโดยใช้ PersonalStorage.from_file().
  2. เข้าถึงโฟลเดอร์ Sent Items โดยใช้ pst.get_predefined_folder(StandardIpmFolder.SENT_ITEMS).
  3. จากนั้นโค้ดดึงเนื้อหาของโฟลเดอร์ "Sent Items" ด้วย folder.get_contents(), นับจำนวนและพิมพ์จำนวนข้อความทั้งหมดในโฟลเดอร์นี้.
  4. โค้ดเข้าถึงข้อความแรกในโฟลเดอร์ "Sent Items" ด้วย msgsColl[0] และลบมันโดยใช้ folder.delete_child_item(msgInfo.entry_id). ฟังก์ชันนี้ใช้ Entry ID ของข้อความเพื่อเอาออกจากโฟลเดอร์.

หลังจากการลบ โค้ดจะนับจำนวนข้อความในโฟลเดอร์ "Sent Items" อีกครั้งและพิมพ์จำนวนที่อัปเดต

ลบรายการจากไฟล์ PST

ในระบบข้อความหรือไคลเอนต์อีเมลหลายระบบ แต่ละรายการ (เช่น อีเมล, การนัดหมาย หรือ งาน) จะได้รับตัวระบุที่ไม่ซ้ำกันเรียกว่า Entry ID. delete_item(entry_id) เมธอดของ FolderInfo คลาสรับ Entry ID นี้เป็นพารามิเตอร์และลบรายการที่สอดคล้องจากแหล่งเก็บข้อความ ใช้โค้ดต่อไปนี้เพื่อลบรายการจากแหล่งเก็บข้อความ:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

pst.delete_item(entry_id)

ลบรายการเป็นกลุ่ม

API ของ Aspose.Email สามารถใช้ลบรายการเป็นกลุ่มจากไฟล์ PST ได้ ซึ่งทำได้โดยใช้ delete_child_items() เมธอดที่รับรายการ Entry ID ของรายการที่ต้องการลบ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีลบรายการเป็นกลุ่มจากไฟล์ PST

ลบโฟลเดอร์จากไฟล์ PST

ไฟล์ Outlook PST อาจมีโฟลเดอร์ที่ไม่จำเป็นอีกต่อไป Aspose.Email สำหรับ Python ผ่าน .NET ให้คุณลบโฟลเดอร์เหล่านี้ได้โดยการย้ายไปยังโฟลเดอร์ Deleted Items (ทำให้สามารถกู้คืนได้) หรือโดยการลบอย่างถาวร ตัวอย่างต่อไปนี้แสดงทั้งสองวิธี

ย้ายโฟลเดอร์ไปยัง Deleted Items (การลบแบบซอฟท์)

นี้ move_item เมธอดของ PersonalStorage คลาสนี้ทำให้โฟลเดอร์สามารถกู้คืนได้ในภายหลัง เนื่องจากไม่ได้ลบอย่างถาวร แต่ย้ายไปยังโฟลเดอร์ Deleted Items ตัวอย่างโค้ดต่อไปนี้แสดงวิธีนำเมธอดนี้ไปใช้ในโปรเจกต์ Python:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

deleted_items_folder = pst.get_predefined_folder(ae.storage.pst.StandardIpmFolder.DELETED_ITEMS)
empty_folder = pst.root_folder.get_sub_folder("Empty folder")
some_folder = pst.root_folder.get_sub_folder("Some folder")
pst.move_item(empty_folder, deleted_items_folder)
pst.move_item(some_folder, deleted_items_folder)

ข้อได้เปรียบของเมธอดนี้คือโฟลเดอร์ที่ถูกลบสามารถกู้คืนได้ง่าย.

กู้คืนโฟลเดอร์จาก Deleted Items

นี้ move_item เมธอดนี้ช่วยให้คุณกู้คืนโฟลเดอร์ หากถูกลบโดยผิดพลาด โดยย้ายกลับจาก Deleted Items ไปยังตำแหน่งเดิม

some_folder = pst.root_folder.get_sub_folder("Some folder")
pst.move_item(some_folder, pst.root_folder)

ลบโฟลเดอร์อย่างถาวรจาก Deleted Items

นี้ delete_child_item เมธอดนี้สามารถใช้กับโฟลเดอร์ใดก็ได้หากคุณต้องการลบโฟลเดอร์ย่อยอย่างทันทีและถาวรโดยข้ามโฟลเดอร์ Deleted Items ตัวอย่างโค้ดต่อไปนี้แสดงวิธีลบโฟลเดอร์ออกจาก Deleted Items อย่างสมบูรณ์ ทำให้ไม่สามารถกู้คืนได้:

deleted_items_folder.delete_child_item(empty_folder.entry_id)

ลบโฟลเดอร์อย่างถาวรโดยทันที

หากต้องการลบโฟลเดอร์โดยไม่ย้ายไปยัง Deleted Items, delete_child_item เมธอดทำให้การลบเป็นทันทีและถาวร

some_folder = pst.root_folder.get_sub_folder("Some folder")
pst.root_folder.delete_child_item(some_folder.entry_id)