Python で IMAP メッセージのバックアップと復元
Aspose.Email for Python は次のメソッドを提供します。 ImapClient IMAP プロトコルでメールメッセージを管理するクラス:
- ‘backup’ メソッド
- ‘restore’ メソッド
この記事では、次の使用方法を示します。 ImapClient クラスとそのメソッドを使用して、メールメッセージを PST ファイルにバックアップおよび復元します。また、マルチコネクションモードを使用して大容量メールボックスのパフォーマンスを向上させる方法も紹介します。
IMAP メッセージのバックアップ
IMAP サーバーからメールメッセージのバックアップを作成するには、次を使用します。 backup メソッド( ImapClient クラス。以下のコードサンプルは、受信トレイフォルダーを .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)
マルチコネクションモードでパフォーマンス向上
大量のデータや多数のメールメッセージを扱うタスク向けに、Aspose.Email は ‘use_multi_connection’ プロパティを提供します。 ImapClient クラスは、クライアントがメールサーバーに対して同時に複数の接続を開くことを可能にし、操作のパフォーマンスを最適化します。 MultiConnectionMode が有効になると、IMAP クライアントはさまざまなタスク(メールの取得、フォルダーの同期、データのバックアップなど)を複数の接続で並行して実行できます。これにより、操作完了に要する全体時間が大幅に短縮されます。以下のコードスニペットは、 MultiConnection バックアップおよび復元操作用のモードです。
注: 複数接続の使用は、メールサーバーが設定する制限やポリシーの対象になる場合があります。一部のサーバーは、サーバー過負荷を防ぐために単一ユーザーアカウントからの同時接続数に制限を課すことがあります。必ずメールプロバイダーの利用規約やポリシーを確認し、マルチコネクションモードを有効にする前に遵守してください。
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 モードを有効にした状態で復元操作を実行する方法を示しています。
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)