Thunderbird 프로그래밍

메시지 읽기

Mozilla Thunderbird는 Mozilla Foundation이 개발한 오픈 소스, 크로스 플랫폼 이메일 클라이언트입니다. 자체 파일 구조에 이메일을 저장하며, 독점 파일 형식을 통해 메시지 인덱스와 하위 폴더를 관리합니다. Aspose.Email는 Thunderbird 메일 저장 구조와 함께 사용할 수 있습니다. MboxrdStorageReader 클래스는 개발자가 Mozilla Thunderbird의 메일 저장 파일에서 메시지를 읽을 수 있게 합니다. 이 문서는 Thunderbird 이메일 저장소에서 메시지를 읽는 방법을 보여줍니다:

  1. Thunderbird 저장 파일을 엽니다
  2. MboxrdStorageReader 클래스를 인스턴스화하고 위 스트림을 생성자에 전달합니다.
  3. read_next_message()를 호출하여 첫 번째 메시지를 가져옵니다.
  4. while 루프에서 동일한 read_next_message()를 사용하여 모든 메시지를 읽습니다.
  5. 모든 스트림을 닫습니다.

다음 코드 스니펫은 Thunderbird 메일 저장소에서 모든 메시지를 읽는 방법을 보여줍니다.

메시지 속성 가져오기

Mbox 파일에서 정보를 읽고 검색하기 위해 Aspose.Email은 MboxStorageReader Mbox 파일에 대한 리더 객체를 생성하는 클래스와 MboxLoadOptions 파일을 로드하는 클래스. 다음 속성은 MboxMessageInfo 클래스를 사용하여 특정 메시지 세부 정보를 액세스하고 표시할 수 있습니다:

  • ‘date’ - 메시지의 날짜를 가져옵니다.
  • ‘from_address’ - 보낸 사람 주소를 가져옵니다.
  • ‘subject’ - 메시지 제목을 가져옵니다.
  • ’to’ - 메시지 수신자를 포함하는 주소 컬렉션을 가져옵니다.
  • ‘cc’ - CC 수신자를 포함하는 주소 컬렉션을 가져옵니다.
  • ‘bcc’ - 메시지의 BCC 수신자를 포함하는 주소 컬렉션을 가져옵니다.

다음 코드 예제는 이러한 속성을 사용해 Mbox 파일에서 메시지 정보를 읽고 추출하는 방법을 보여줍니다:

import aspose.email as ae

reader = ae.storage.mbox.MboxStorageReader.create_reader(file_name, ae.storage.mbox.MboxLoadOptions())

for mbox_message_info in reader.enumerate_message_info():
    print(f"Subject: {mbox_message_info.subject}")
    print(f"Date: {mbox_message_info.date}")
    print(f"From: {mbox_message_info.from_address}")
    print(f"To: {mbox_message_info.to}")
    print(f"CC: {mbox_message_info.cc}")
    print(f"Bcc: {mbox_message_info.bcc}")

식별자를 사용하여 MBOX에서 메시지 추출

MBOX 파일에서 메시지를 읽기 위해 Aspose.Email는 ‘create_reader()’ 메서드를 제공합니다. MboxStorageReader 클래스 - 파일에 대한 리더 객체를 생성합니다. 파일 이름과 MboxLoadOptions 인수로 사용되어 필요에 따라 사용자가 특정 옵션으로 MBOX 파일을 로드할 수 있도록 합니다.

메시지를 추출하기 위해 다음 메서드와 속성이 사용됩니다:

  • ’enumerate_message_info()’ 메서드 of the MboxStorageReader 클래스 - MBOX 파일의 각 메시지를 반복합니다.
  • ’extract_message()’ 메서드 of the MboxStorageReader 클래스 - 엔트리 ID로 각 메시지를 추출합니다.
  • ’entry_id’ 속성 of the MboxMessageInfo 클래스 - 엔트리 식별자를 가져옵니다.

마지막으로, 메시지는 다음을 사용해 EML 형식으로 변환됩니다: EmlLoadOptions.

아래 코드 예제는 이러한 기능을 사용해 MBOX 파일에서 메시지를 읽고 추출하는 방법을 보여줍니다:

import aspose.email as ae

reader = ae.storage.mbox.MboxStorageReader.create_reader("my.mbox", ae.storage.mbox.MboxLoadOptions())

for mbox_message_info in reader.enumerate_message_info():
    eml = reader.extract_message(mbox_message_info.entry_id, ae.EmlLoadOptions())

MBOX에서 메시지를 읽을 때 로드 옵션 구성

Aspose.Email와 함께 EmlLoadOptions 클래스로, Eml 형식에서 MailMessage를 로드할 때 추가 옵션을 지정할 수 있습니다. 예를 들어, ‘preserve_tnef_attachments’ 속성을 사용해 EML 파일을 로드할 때 TNEF 첨부 파일을 보존하도록 옵션을 설정할 수 있습니다. EmlLoadOptions 클래스.

지정된 로드 옵션을 사용하여 mbox 파일에서 다음 이메일 메시지를 ‘read_next_message’ 메서드로 읽을 수 있습니다. MboxStorageReader 클래스이며, ‘mbox_to_pst’ 메서드를 사용해 파일을 PST 형식으로 변환합니다. MailStorageConverter 클래스 .

아래 코드 예제는 mbox 형식에서 메시지를 읽고, TNEF 첨부 파일을 보존하며, mbox에서 pst 형식으로 메시지를 변환하는 등 이메일 저장 파일 작업에 이러한 메서드와 속성을 사용하는 방법을 보여줍니다:

import aspose.email as ae

reader = ae.storage.mbox.MboxrdStorageReader(fileName, ae.storage.mbox.MboxLoadOptions())
# Read messages preserving tnef attachments.
load_options = ae.EmlLoadOptions()
load_options.preserve_tnef_attachments = True
eml = reader.read_next_message(load_options)
ae.storage.MailStorageConverter.MboxMessageOptions(load_options)
# Convert messages from mbox to pst preserving tnef attachments.
pst = ae.storage.MailStorageConverter.mbox_to_pst("Input.mbox", "Output.pst")

Mbox 파일을 읽을 때 기본 텍스트 인코딩 설정

MBOX 파일을 로드할 때 사용할 텍스트 인코딩을 지정할 수 있습니다. ‘preferred_text_encoding’ 속성은 MboxLoadOptions 클래스는 추가 옵션을 설정하고 인코딩된 내용이 있는 메시지가 올바르게 읽히고 처리되도록 보장합니다.

다음 코드 스니펫은 프로젝트에서 이 기능을 사용하는 방법을 보여줍니다:

import aspose.email as ae

load_options = ae.storage.mbox.MboxLoadOptions()
load_options.preferred_text_encoding = 'utf-8'
reader = ae.storage.mbox.MboxrdStorageReader("sample.mbox", load_options)
message = reader.read_next_message()

MBOX를 PST로 변환하면서 서명 보존 또는 제거

변환 과정에서 파일의 서명을 제거하려면 MboxToPstConversionOptions.remove_signature 속성을 true로 설정합니다.

다음 코드 예제는 이 속성을 활용하는 방법을 보여줍니다:

import aspose.email as ae

personalStorage = ae.storage.pst.PersonalStorage.create("target.pst", ae.storage.pst.FileFormatVersion.UNICODE)
conversion_options = ae.storage.MboxToPstConversionOptions()
conversion_options.remove_signature = True
ae.storage.MailStorageConverter.mbox_to_pst( ae.storage.mbox.MboxrdStorageReader("source.mbox", ae.storage.mbox.MboxLoadOptions()), personalStorage, "Inbox", conversion_options)

메시지 쓰기

MboxrdStorageWriter 클래스는 Thunderbird 메일 저장 파일에 새 메시지를 쓸 수 있는 기능을 제공합니다. 메시지를 쓰려면:

  1. FileStream에서 Thunderbird 저장 파일을 엽니다.
  2. MboxrdStorageWriter 클래스를 인스턴스화하고 위 스트림을 생성자에 전달합니다.
  3. MailMessage 클래스를 사용해 새 메시지를 준비합니다.
  4. write_message() 메서드를 호출하고 위 MailMessage 인스턴스를 전달하여 메시지를 Thunderbird 저장소에 추가합니다.
  5. 모든 스트림을 닫습니다.

다음 코드 스니펫은 Thunderbird 메일 저장소에 메시지를 쓰는 방법을 보여줍니다.

MBox 파일에서 총 메시지 수 가져오기

The MboxrdStorageReader 클래스는 MBox 파일에서 사용 가능한 항목 수를 읽을 수 있는 기능을 제공합니다. 이는 파일을 처리하는 동안 작업 진행 상황을 표시하는 애플리케이션을 개발하는 데 사용할 수 있습니다.

현재 메시지 크기 가져오기