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 백업 및 복원 작업을 위한 모드.

참고: 다중 연결 사용은 이메일 서버가 설정한 제한 및 정책의 적용을 받을 수 있습니다. 일부 서버는 서버 과부하를 방지하기 위해 단일 사용자 계정에서 동시에 연결할 수 있는 수를 제한할 수 있습니다. 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 모드를 사용해 복원 작업을 수행하는 방법을 보여줍니다.

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)