Aspose.Email.Outlook을 사용한 메시지 파일 관리
PST 폴더에서 MAPI 항목 유형 결정
PST 파일에는 일반적으로 이메일 메시지, 캘린더 이벤트, 연락처 등 다양한 유형의 데이터가 포함됩니다. MapiItemType Aspose.Email의 클래스는 PST 파일 내에서 다양한 유형의 MAPI 항목에 접근하고 분류할 수 있게 합니다. 아래 코드는 PST 폴더 내 MAPI 항목의 유형을 판단하는 방법을 보여줍니다:
import aspose.email as ae
pst = ae.storage.pst.PersonalStorage.from_file("test.pst")
folder = pst.root_folder.get_sub_folder("Calendar")
for messageInfo in folder.enumerate_messages():
msg = pst.extract_message(messageInfo)
# Get the class type based on msg.SupportedType
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 = 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 = 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 = 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 = 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 = 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 = msg.to_mapi_message_item()
# You can access to MapiTask properties there
MSG를 MIME 메시지로 변환
Aspose.Email API는 ToMailMessage 메서드를 사용하여 MSG 파일을 MIME 메시지로 변환하는 기능을 제공합니다.
메시지 변환 및 로드 시 타임아웃 설정
메시지 변환 시간(밀리초)을 제한하려면(기본값 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 템플릿(OFT) 파일 처리
OFT 파일 로드, 수정 및 저장
Outlook 템플릿(OFT)은 반복적인 이메일 전송 과정을 간소화하는 편리한 방법을 제공합니다. 매번 처음부터 동일한 이메일을 작성하는 대신 Outlook에서 메시지를 만들고 Outlook 템플릿(OFT)으로 저장할 수 있습니다. 이후 유사한 메시지를 보낼 때 템플릿에서 빠르게 생성할 수 있어 메시지 본문, 제목, 서식 등을 다시 작성하는 수고를 줄일 수 있습니다.
Aspose.Email의 MailMessage 클래스는 Outlook 템플릿 파일(OFT)을 로드하고 조작할 수 있는 강력한 도구를 제공합니다. Outlook 템플릿이 MailMessage 클래스 인스턴스로 로드되면 보낸 사람, 받는 사람, 메시지 본문, 제목 및 다양한 속성을 손쉽게 업데이트할 수 있습니다.
import aspose.email as ae
# Load the OFT file
oft_file_path = "your_template.oft"
mail_message = ae.MailMessage.load(oft_file_path)
# Update properties as needed
mail_message.subject = "Updated Subject"
mail_message.body = "Updated body text."
# Save the updated message as an MSG file
msg_file_path = "updated_message.msg"
mail_message.save(msg_file_path, ae.MailMessageSaveType.outlook_message_format_unicode)
print(f"Updated message saved to {msg_file_path}")
필요한 업데이트를 마친 후, 다음을 사용하여 이메일을 보낼 수 있습니다 SmtpClient 클래스:
# Send the email
smtpClient.send(message)
Outlook MSG 파일을 템플릿으로 저장
이를 위해서는 다음을 사용할 수 있습니다: MailMessage 클래스는 이메일을 생성한 후 OFT 파일로 저장합니다. 다음 코드 샘플은 이메일을 Outlook 템플릿으로 저장하는 방법을 보여줍니다.
import aspose.email as ae
# Create a new MailMessage
message = ae.MailMessage()
message.subject = "Sample Outlook Template"
message.html_body = "<html><body>This is the body of the email.</body></html>"
message.from_address = ae.MailAddress("your.email@example.com", "Your Name")
# Save the MailMessage as an Outlook Template (OFT) file
oft_file_path = "sample_template.oft"
message.save(oft_file_path, ae.SaveOptions.default_oft)
print(f"Outlook Template saved as {oft_file_path}")
OFT 또는 MSG: MapiMessage 유형 결정
다음 코드 스니펫은 로드된 MAPI 메시지가 일반 이메일인지 Outlook 템플릿(OFT)인지 판단하는 방법을 보여줍니다.
msg = ae.mapi.MapiMessage.Load("message.msg");
isOft = msg.is_template # returns false
msg = ae.mapi.MapiMessage.Load("message.oft");
isOft = msg.IsTemplate; # returns true
MSG 파일을 로드한 후, 코드는 is_template 속성을 확인합니다 MapiMessaage 클래스는 True 또는 False를 반환합니다. false를 반환하면 로드된 메시지는 표준 이메일 메시지이며 Outlook 템플릿이 아닙니다. 반대로 true를 반환하면 해당 메시지는 표준 이메일이 아니라 Outlook 템플릿입니다.
MapiMessage 또는 MailMessage를 OFT로 저장
다음은 SaveOptions 특정 형식으로 MailMessage를 저장할 때 추가 옵션을 지정할 수 있게 하는 추상 기본 클래스입니다. 다음 코드 샘플은 메시지를 OFT 형식으로 저장하는 방법을 보여줍니다:
import aspose.email as ae
# Save the MailMessage to OFT format
eml = ae.MailMessage.load("message.eml")
eml.save("message.oft", ae.SaveOptions.default_oft)
# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)
# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)
# Save the MapiMessage to OFT format
msg = ae.mapi.MapiMessage.load("message.msg")
msg.save("message.oft", ae.SaveOptions.default_oft)
# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)
# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)
디지털 서명이 있는 메시지 관리
이 라이브러리는 다음을 제공합니다 LoadOptions 클래스는 특정 형식에서 MailMessage를 로드할 때 추가 옵션을 지정할 수 있게 하는 추상 기본 클래스입니다. 서명 보존 옵션이 기본적으로 설정되어 있습니다.
EML을 MSG로 변환하면서 서명 보존
아래 코드 예제는 메시지를 로드하고, MSG 형식으로 변환한 뒤 MSG로 저장하는 방법을 보여줍니다(서명은 기본적으로 보존됩니다):
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
message = ae.MailMessage.load("Message.eml", loadOptions)
# Save as MSG
message.save("ConvertEMLToMSG_out.msg", ae.SaveOptions.default_msg_unicode)
MSG에서 EML로 S/MIME 메시지 변환
Aspose.Email은 아래 코드 조각에 표시된 대로 디지털 서명을 유지하면서 MSG를 EML로 변환할 수 있습니다.
import aspose.email as ae
# Load mail message
loadOptions = ae.EmlLoadOptions()
smime_eml = ae.MailMessage.load("smime.eml", loadOptions)
conversion_options = ae.mapi.MapiConversionOptions(ae.mapi.OutlookMessageFormat.UNICODE)
msg = ae.mapi.MapiMessage.from_mail_message(smime_eml, conversion_options)
# Save File to disk
msg.save("ConvertMIMEMessagesFromMSGToEML_out.msg")
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)
MSG 파일에서 Follow Up 정보 접근
읽음 및 배달 확인 정보 추출
아래 코드 샘플은 Outlook 메시지(MSG) 파일에서 수신자 정보와 추적 상태를 추출하는 방법을 보여줍니다:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("received.msg")
for recipient in msg.recipients:
print(f"Recipient: {recipient.display_name}")
print(f"Delivery time: {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_DELIVERY]}")
print(f"Read time: {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_READ]}")
전달 및 회신 메시지 만들기
Aspose.Email은 기존 메시지를 기반으로 전달 및 답장 메시지를 만드는 간단한 방법을 제공합니다.
전달 메시지 만들기
다음을 사용할 수 있습니다 ForwardMessageBuilder 클래스를 사용하여 원본 메시지, 발신자, 수신자, 제목 및 본문을 설정함으로써 전달 메시지를 생성합니다. 전달 메시지는 원본 메시지를 첨부 파일로 포함하거나 전달 메시지 본문에 포함할 수 있습니다. 첨부 파일, 헤더 및 서식 옵션과 같은 추가 속성을 자유롭게 커스터마이징할 수 있습니다. 아래 코드 샘플은 전달 메시지를 만드는 방법을 보여줍니다:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("original.msg")
builder = ae.tools.ForwardMessageBuilder()
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
forwardMsg = builder.build_response(msg)
forwardMsg.save("forward_out.msg")
답장 메시지 생성
다음은 ReplyMessageBuilder 클래스는 원본 메시지, 발신자, 수신자, 회신 모드, 제목 접두사 및 회신 메시지 본문 등 회신 설정을 구성하는 데 사용됩니다. 요구 사항에 따라 "보낸 사람에게 회신" 또는 "전체 회신"과 같은 다양한 회신 모드로 회신 메시지를 만들 수 있습니다. 첨부 파일, 헤더 및 서식 옵션과 같은 다양한 속성을 회신 메시지에 맞게 커스터마이징할 수 있습니다. 아래 코드 샘플은 전달 메시지를 만드는 방법을 보여줍니다:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("original.msg")
builder = ae.tools.ReplyMessageBuilder()
# Set ReplyMessageBuilder Properties
builder.reply_all = True
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
builder.response_text = "<p><b>Dear Friend,</b></p> I want to do is introduce my co-author and co-teacher. <p><a href=\"www.google.com\">This is a first link</a></p><p><a href=\"www.google.com\">This is a second link</a></p>";
replyMsg = builder.build_response(msg)
replyMsg.save("reply_out.msg")