MAPI 속성 작업
PST 폴더 내 MAPI 항목 유형 결정
PST 파일에는 일반적으로 이메일 메시지, 캘린더 이벤트, 연락처 등 다양한 유형의 데이터가 포함됩니다. MapiItemType Aspose.Email의 클래스는 PST 파일 내에서 다양한 유형의 MAPI 항목에 접근하고 분류할 수 있게 합니다. 아래 코드는 PST 폴더 내 MAPI 항목의 유형을 판단하는 방법을 보여줍니다:
import aspose.email as ae
import aspose.pycore as pycore
pst = ae.storage.pst.PersonalStorage.from_file("test.pst")
folder = pst.root_folder.get_sub_folder("Calendar")
for message_info in folder.enumerate_messages():
msg = pst.extract_message(message_info)
# Get the class type based on msg.supported_type
item_type = msg.supported_type
# Non-supported type. It cannot be accessed as appropriate item type.
if item_type == ae.mapi.MapiItemType.NONE:
print("Item type not supported")
# An email message.
elif item_type == ae.mapi.MapiItemType.MESSAGE:
# You can access to MapiMessage properties there.
# A subject for example
print(msg.subject)
# A contact item. Can be accessed as MapiContact.
elif item_type == ae.mapi.MapiItemType.CONTACT:
contact = pycore.cast(ae.mapi.MapiContact, msg.to_mapi_message_item())
# You can access to MapiContact properties there.
# A name_info.display_name for example.
print(contact.name_info.display_name)
# A calendar item. Can be accessed as MapiCalendar.
elif item_type == ae.mapi.MapiItemType.CALENDAR:
calendar = pycore.cast(ae.mapi.MapiCalendar, msg.to_mapi_message_item())
# You can access to MapiCalendar properties there.
# A location for example.
print(calendar.location)
# A distribution list. Can be accessed as MapiDistributionList.
elif item_type == ae.mapi.MapiItemType.DIST_LIST:
dlist = pycore.cast(ae.mapi.MapiDistributionList, msg.to_mapi_message_item())
# You can access to MapiDistributionList properties there
# A Journal entry. Can be accessed as MapiJournal.
elif item_type == ae.mapi.MapiItemType.JOURNAL:
journal = pycore.cast(ae.mapi.MapiJournal, msg.to_mapi_message_item())
# You can access to MapiJournal properties there
# A StickyNote. Can be accessed as MapiNote.
elif item_type == ae.mapi.MapiItemType.NOTE:
note = pycore.cast(ae.mapi.MapiNote, msg.to_mapi_message_item())
# You can access to MapiNote properties there
# A Task item. Can be accessed as MapiTask.
elif item_type == ae.mapi.MapiItemType.TASK:
task = pycore.cast(ae.mapi.MapiTask, msg.to_mapi_message_item())
# You can access to MapiTask properties there
MAPI 속성 접근 및 관리
다음은 MapiProperty 클래스는 다음을 포함하는 MAPI 속성을 나타냅니다:
- Name: 속성 이름을 나타내는 문자열.
- Tag: 속성 태그를 나타내는 긴 정수.
- Data: 속성 데이터를 나타내는 바이트 배열.
속성 태그를 사용하여 MAPI 속성 가져오기
아래 코드 샘플은 MAPI 메시지를 로드하고, 제목 및 인터넷 코드 페이지와 같은 주요 속성을 가져오며, 이러한 속성이 없을 경우를 안전하게 처리합니다. MAPI 속성을 가져오려면:
- 다음 방법을 사용하여 파일에서 MAPI 메시지를 로드합니다 로드 메서드.
- 다음 사용 MapiPropertyTag 클래스는 MAPI 메시지에서 제목 및 인터넷 코드 페이지와 같은 특정 속성에 접근합니다.
- 누락된 속성을 처리합니다.
from aspose.email.mapi import MapiMessage, MapiPropertyTag
# Load from file
msg = MapiMessage.load("message.msg")
# Access the MapiPropertyTag.PR_SUBJECT property
prop = msg.properties[MapiPropertyTag.SUBJECT]
# If the property is not found, check the MapiPropertyTag.PR_SUBJECT_W (which is a // Unicode peer of the MapiPropertyTag.PR_SUBJECT)
if prop is None:
prop = msg.properties[MapiPropertyTag.SUBJECT_W]
# Cannot found
if prop is None:
print("No property found!")
# Get the property data as string
subject = prop.get_string()
print("Subject:" + subject)
# Read internet code page property
prop = msg.properties[MapiPropertyTag.INTERNET_CPID]
if prop is not None:
print("CodePage:" + str(prop.get_long()))
MAPI 속성 설정
다음 코드 스니펫은 MAPI 속성을 설정하는 방법을 보여줍니다.
명명된 MAPI 속성 읽기
Aspose.Email은 명명된 MAPI 속성 추출을 포함한 MSG 파일 작업을 위한 다양한 API를 제공합니다.
MSG 파일에서 명명된 MAPI 속성 읽기
명명된 MAPI 속성을 읽으려면 다음을 사용할 수 있습니다. named_properties 속성 MapiMessage 클래스. 다음 코드 샘플은 메시지를 로드하고, 모든 명명된 MAPI 속성을 검색한 뒤, 각 속성을 반복하여 값을 확인하는 방법을 보여줍니다.
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Get all named MAPI properties
properties = msg.named_properties.values
# Read all properties in foreach loop
for prop in properties:
# Read any specific property
if prop.descriptor.canonical_name == "PidLidSideEffects":
print(f"{prop.descriptor.canonical_name} = {prop}")
if prop.descriptor.canonical_name == "PidLidInternetAccountName":
print(f"{prop.descriptor.canonical_name} = {prop}")
첨부 파일에서 명명된 MAPI 속성 읽기
Aspose.Email은 첨부 파일의 모든 명명된 MAPI 속성을 검색할 수 있게 합니다. 다음 코드 샘플은 첨부 파일에서 명명된 MAPI 속성을 읽는 방법을 보여줍니다:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Get all named MAPI properties
attach_properties = msg.attachments[0].named_properties.values
# Read all properties in foreach loop
for prop in attach_properties:
# Read any specific property
if prop.descriptor.name == "AttachmentOriginalUrl":
print(f"{prop.descriptor.name} = {prop.get_string()}")
if prop.descriptor.name == "AttachmentWasSavedToCloud":
print(f"{prop.descriptor.name} = {prop.get_boolean()}")
속성 관리
MSG 파일 및 첨부 파일에서 속성 제거
아래 코드 샘플은 본문 내용과 첨부된 메시지 파일을 포함하는 Outlook MSG 메시지를 만드는 방법을 보여줍니다. 또한 생성된 메시지에서 첨부 파일 속성을 삭제하는 방법도 시연합니다.
import aspose.email as ae
# create an MSG
msg = ae.mapi.MapiMessage("from@domain.com", "to@domain.com", "subject", "body")
msg.set_body_content("<html><body><h1>This is the body content</h1></body></html>", ae.mapi.BodyContentType.HTML)
# load message and add it to created MSG as attachment
attachment = ae.mapi.MapiMessage.load("attach.msg")
msg.attachments.add("Outlook2 Test subject.msg", attachment)
# count of attachment properties before removal
print(f"Before removal = {msg.attachments[0].properties.count}")
# Delete anyone attachment property
msg.attachments[0].remove_property(923467779)
# count of attachment properties after removal
print(f"After removal = {msg.attachments[0].properties.count}")
MSG를 MIME 메시지로 변환
Aspose.Email API는 MSG 파일을 MIME 메시지로 변환하는 기능을 제공합니다 to_mail_message 메서드.
메시지 변환 및 로딩에 대한 제한시간 설정
메시지 변환 시간(밀리초)을 제한하려면(기본값 3초), timeout 속성을 사용합니다. MailConversionOptions 클래스가 사용됩니다.
다음은 메시지 변환 및 로딩 프로세스에 제한시간을 설정하는 방법입니다:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
options = ae.mapi.MailConversionOptions()
options.timeout = 5000
mailMessage = msg.to_mail_message(options)
메시지 변환 및 로딩 프로세스에 제한시간을 설정하면 이러한 작업이 허용되는 최대 시간을 제어할 수 있습니다. 제한시간을 설정한 후에는 메시지 변환 및 로딩 프로세스를 진행할 수 있습니다.
Outlook MSG 파일에 색상 카테고리 설정
때때로 특정 중요도의 이메일을 구분하고 시각적으로 정리해야 할 때가 있습니다. 라이브러리는 메시지 항목에 특정 색상을 지정하여 이를 수행하는 방법을 제공합니다. 항목에 색상 카테고리를 설정하면 한눈에 관련 항목을 쉽게 식별하고 찾을 수 있습니다. 사용 FollowUpManager 다음 코드 예제와 같이 메시지의 색상 카테고리를 설정하는 클래스:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Add Two categories
ae.mapi.FollowUpManager.add_category(msg, "Purple Category")
ae.mapi.FollowUpManager.add_category(msg, "Red Category")
# Retrieve the list of available categories
categories = ae.mapi.FollowUpManager.get_categories(msg)
# Remove the specified category and then Clear all categories
ae.mapi.FollowUpManager.remove_category(msg, "Red Category")
ae.mapi.FollowUpManager.clear_categories(msg)