สำรองและกู้คืนข้อความ IMAP ใน Python
Aspose.Email สำหรับ Python มีวิธีการของ ImapClient คลาสสำหรับจัดการข้อความอีเมลผ่านโปรโตคอล IMAP:
- วิธีการ ‘backup’
- วิธีการ ‘restore’
บทความนี้แสดงวิธีการใช้ ImapClient คลาสและวิธีการของมันเพื่อสำรองและกู้คืนข้อความอีเมลไปยังและจากไฟล์ PST นอกจากนี้ยังครอบคลุมวิธีการเพิ่มประสิทธิภาพสำหรับกล่องจดหมายขนาดใหญ่โดยใช้โหมด multi-connection
สำรองข้อความ IMAP
เพื่อสร้างการสำรองข้อความอีเมลจากเซิร์ฟเวอร์ IMAP ใช้ backup เมธอดของ ImapClient คลาส ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสำรองโฟลเดอร์ Inbox ไปยังไฟล์ .pst:
import aspose.email as ae
# Create an instance of the ImapClient class
imap_client = ae.clients.imap.ImapClient()
# Specify host, username, password, and set port for your client
imap_client.host = "imap.gmail.com"
imap_client.username = username
imap_client.password = password
imap_client.port = 993
imap_client.security_options = ae.clients.SecurityOptions.AUTO
# Get mailbox info
mailbox_info = imap_client.mailbox_info
# Get folder info for the Inbox folder
inbox_info = imap_client.get_folder_info(mailbox_info.inbox.name)
# Create an ImapFolderInfoCollection and add the Inbox folder info
infos = ae.clients.imap.ImapFolderInfoCollection()
infos.add(inbox_info)
# Specify the path to the directory
data_dir = "path/to/your/data/directory"
# Perform the backup operation
settings = ae.clients.imap.BackupSettings
settings.execute_recursively = True
imap_client.backup(infos, data_dir + "\\ImapBackup.pst", settings)
กู้คืนข้อความ IMAP
เพื่อกู้คืนข้อความจากไฟล์ .pst กลับไปยังเซิร์ฟเวอร์ IMAP ใช้ restore เมธอดของ ImapClient คลาส:
import aspose.email as ae
# Create an instance of the ImapClient class
imap_client = ae.clients.imap.ImapClient()
# Specify host, username, password, and set port for your client
imap_client.host = "imap.gmail.com"
imap_client.username = username
imap_client.password = password
imap_client.port = 993
imap_client.security_options = ae.clients.SecurityOptions.Auto
# Create RestoreSettings with Recursive set to true
settings = ae.clients.imap.RestoreSettings()
settings.recursive = True
# Specify the path to the directory
data_dir = "path/to/your/data/directory"
# Load the PST file
pst = ae.storage.pst.PersonalStorage.from_file(data_dir + "\\ImapBackup.pst")
# Perform the restore operation
imap_client.restore(pst, settings)
เพิ่มประสิทธิภาพด้วยโหมด Multi-Connection
สำหรับงานที่เกี่ยวข้องกับข้อมูลจำนวนมากหรืออีเมลหลายฉบับ Aspose.Email มีคุณสมบัติ ‘use_multi_connection’ ของ ImapClient คลาสเพื่อเพิ่มประสิทธิภาพของการดำเนินการโดยอนุญาตให้ไคลเอนต์เปิดการเชื่อมต่อหลายครั้งกับเซิร์ฟเวอร์อีเมลพร้อมกัน เมื่อ MultiConnectionMode เมื่อเปิดใช้งาน, ไคลเอนต์ IMAP สามารถทำงานต่าง ๆ (เช่น ดึงอีเมล, ซิงโครไนซ์โฟลเดอร์, และสำรองข้อมูล) แบบขนานผ่านการเชื่อมต่อหลายช่อง นี่อาจนำไปสู่การลดเวลารวมที่ใช้ในการดำเนินการอย่างมีนัยสำคัญ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเปิดใช้งาน MultiConnection โหมดสำหรับการสำรองและกู้คืนการดำเนินการ.
หมายเหตุ: การใช้การเชื่อมต่อหลายครั้งอาจถูกจำกัดโดยข้อจำกัดและนโยบายของเซิร์ฟเวอร์อีเมลบางเซิร์ฟเวอร์อาจกำหนดข้อจำกัดจำนวนการเชื่อมต่อพร้อมกันจากบัญชีผู้ใช้คนเดียวเพื่อป้องกันการโหลดเซิร์ฟเวอร์เกิน ควรตรวจสอบข้อกำหนดการให้บริการหรือนโยบายของผู้ให้บริการอีเมลเพื่อให้แน่ใจว่าปฏิบัติตามแนวทางการใช้ก่อนเปิดใช้งาน MultiConnectionMode
สำรองข้อความด้วย MultiConnection ที่เปิดใช้งาน
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำการสำรองโดยเปิดใช้โหมด MultiConnection:
import aspose.email as ae
# Create an instance of the ImapClient class
imap_client = ae.clients.imap.ImapClient()
# Specify host, username, password, and set port for your client
imap_client.host = "imap.gmail.com"
imap_client.username = username
imap_client.password = password
imap_client.port = 993
imap_client.security_options = ae.clients.SecurityOptions.Auto
# Enable MultiConnectionMode
imap_client.use_multi_connection = ae.clients.MultiConnectionMode.ENABLE
# Get mailbox info
mailbox_info = imap_client.mailbox_info
# Get folder info for the Inbox folder
inbox_info = imap_client.get_folder_info(mailbox_info.inbox.name)
# Create an ImapFolderInfoCollection and add the Inbox folder info
infos = ae.clients.imap.ImapFolderInfoCollection()
infos.add(inbox_info)
# Specify the path to the directory
data_dir = "path/to/your/data/directory"
# Perform the backup operation
settings = ae.clients.imap.BackupSettings
settings.execute_recursively = True
imap_client.backup(infos, data_dir + "\\ImapBackup.pst", settings)
กู้คืนข้อความด้วย MultiConnection
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำการกู้คืนโดยเปิดใช้โหมด MultiConnection
import aspose.email as ae
# Create an instance of the ImapClient class
imap_client = ae.clients.imap.ImapClient()
# Specify host, username, password, and set port for your client
imap_client.host = "imap.gmail.com"
imap_client.username = username
imap_client.password = password
imap_client.port = 993
imap_client.security_options = ae.clients.SecurityOptions.Auto
# Enable MultiConnectionMode
imap_client.use_multi_connection = ae.clients.MultiConnectionMode.ENABLE
# Create RestoreSettings with Recursive set to true
settings = ae.clients.imap.RestoreSettings()
settings.recursive = True
# Specify the path to the directory
data_dir = "path/to/your/data/directory"
# Load the PST file
pst = ae.storage.pst.PersonalStorage.from_file(data_dir + "\\Outlook.pst")
# Perform the restore operation
imap_client.restore(pst, settings)