Outlook MSG 파일 만들기 및 저장
**Aspose.Email for C++**은 개발자가 메시지 속성 및 형식에 대한 완전한 제어를 통해 Outlook MSG 파일을 프로그래밍 방식으로 생성, 수정 및 저장할 수 있도록 합니다. 처음부터 MSG 메시지를 생성하고, 첨부 파일을 추가하며, 풍부한 텍스트(RTF) 본문을 사용하고, 초안을 저장하며, 본문 압축 옵션을 사용해 메시지 크기를 최적화할 수 있습니다.
이 문서의 샘플은 다음 헤더와 네임스페이스를 사용합니다:
#include <MailMessage.h>
#include <MsgLoadOptions.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiMessageFlags.h>
#include <Mapi/MapiConversionOptions.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
Outlook 메시지 만들기 및 저장
메시지는 일반적으로 MailMessage: 발신자, 수신자, 제목 및 본문을 설정한 다음 MapiMessage::FromMailMessage. MapiMessage::Save 항목을 MSG 파일로 작성하면 Outlook에서 바로 엽니다.
SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"sender@domain.com");
mailMsg->set_To(u"receiver@domain.com");
mailMsg->set_Subject(u"This is a test message");
mailMsg->set_Body(u"This is a test body");
SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"CreatedMessage.msg");
Console::WriteLine(u"Saved: " + MapiMessage::Load(u"CreatedMessage.msg")->get_Subject());
MapiMessage에 수신자 및 첨부 파일 추가
A MapiMessage 변환 없이 직접 채울 수도 있습니다 MailMessage. The 추가 의 메서드 MapiRecipientCollection class return the created MapiRecipient, 그리고 추가 의 메서드 MapiAttachmentCollection class return the created MapiAttachment. 이를 통해 별도의 컬렉션 조회 없이 새 수신자나 첨부 파일을 즉시 구성할 수 있습니다.
수신자 유형은 다음 중 하나입니다 MapiRecipientType 값을 설정할 수 있습니다. 다음 코드 예제는 수신자와 첨부 파일을 추가한 후 표시 이름을 변경합니다:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiRecipientType.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email::Mapi;
auto message = MakeObject<MapiMessage>();
message->set_Subject(u"Invoice");
// Add a recipient and configure it
SharedPtr<MapiRecipient> recipient = message->get_Recipients()->Add(u"alice.johnson@example.com", u"SMTP", u"Alice Johnson", MapiRecipientType::MAPI_TO);
recipient->set_DisplayName(u"Alice Johnson (Accounting)");
// Add an attachment from file bytes and configure it
ArrayPtr<uint8_t> fileBytes = IO::File::ReadAllBytes(u"invoice.pdf");
SharedPtr<MapiAttachment> attachment = message->get_Attachments()->Add(u"invoice.pdf", fileBytes);
attachment->set_DisplayName(u"Invoice #2026-001.pdf");
message->Save(u"invoice.msg");
RTF 본문이 있는 MSG 파일 생성
Outlook은 메시지의 형식이 지정된 본문을 Rich Text Format (RTF) 로 유지합니다. when a MailMessage HTML 본문을 변환하면 Aspose.Email이 HTML에서 RTF 본문을 생성하므로 헤딩, 굵게 및 밑줄 텍스트가 MSG 파일에 유지됩니다. RTF 텍스트는 다음을 통해 사용할 수 있습니다. get_BodyRtf.
SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"from@domain.com");
mailMsg->set_To(u"to@domain.com");
mailMsg->set_Subject(u"RTF example");
mailMsg->set_HtmlBody(u"<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");
SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"MessageWithRtfBody.msg");
String rtf = MapiMessage::Load(u"MessageWithRtfBody.msg")->get_BodyRtf();
Console::WriteLine(u"RTF body starts with: " + rtf.Substring(0, 6));
메시지를 초안 상태로 저장
Outlook은 메시지를 초안으로 표시합니다 – 편집 가능하고 Send 버튼이 있습니다 – MSGFLAG_UNSENT 플래그가 설정됩니다. 샘플은 기존 MSG 파일을 로드하여 MailMessage (with MsgLoadOptions), 이를 변경하고 플래그를 전달하여 초안으로 다시 저장합니다. SetMessageFlags.
SharedPtr<MailMessage> msg = MailMessage::Load(u"Message.msg", MakeObject<MsgLoadOptions>());
msg->set_Subject(msg->get_Subject() + u" (updated by Aspose.Email)");
msg->set_HtmlBody(u"<p>NEW BODY (updated by Aspose.Email)</p>");
SharedPtr<MapiMessage> mapiMsg = MapiMessage::FromMailMessage(msg);
mapiMsg->SetMessageFlags(MapiMessageFlags::MSGFLAG_UNSENT);
mapiMsg->Save(u"DraftMessage.msg");
bool isDraft = (MapiMessage::Load(u"DraftMessage.msg")->get_Flags() & MapiMessageFlags::MSGFLAG_UNSENT) == MapiMessageFlags::MSGFLAG_UNSENT;
Console::WriteLine(u"Saved as a draft: " + Convert::ToString(isDraft));
본문 압축을 이용한 최적화
MSG 파일의 본문은 압축된 형태로 저장될 수 있습니다. MapiConversionOptions::set_UseBodyCompression 변환 시 압축을 켭니다; 결과 파일과 해당 메시지에서 만든 PST 파일은 약간의 추가 처리 시간과 교환하여 더 작아집니다.
SharedPtr<MailMessage> message = MailMessage::Load(u"Message.eml");
SharedPtr<MapiConversionOptions> options = MakeObject<MapiConversionOptions>();
options->set_UseBodyCompression(true);
MapiMessage::FromMailMessage(message, options)->Save(u"CompressedBody.msg");
MapiMessage::FromMailMessage(message)->Save(u"UncompressedBody.msg");
Console::WriteLine(u"Compressed body: " + Convert::ToString(IO::File::ReadAllBytes(u"CompressedBody.msg")->get_Length()) + u" bytes");
Console::WriteLine(u"Uncompressed body: " + Convert::ToString(IO::File::ReadAllBytes(u"UncompressedBody.msg")->get_Length()) + u" bytes");
- UseBodyCompression = true → 파일 크기가 작아지고, 성능이 느려집니다.
- UseBodyCompression = false → 변환이 더 빠르고, 출력 파일이 더 큽니다.
첨부 파일이 있는 MSG 파일 만들기
첨부 파일이 MailMessage 변환 전에: 모든 Attachment 에 구성합니다 첨부 파일 컬렉션이 결과 Outlook 메시지의 첨부 파일이 됩니다. 항목을 직접 MapiMessage 다음에 설명되어 있습니다. Outlook MSG 파일에서 첨부 파일 관리.
SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"sender@domain.com");
mailMsg->set_To(u"receiver@domain.com");
mailMsg->set_Subject(u"This is a test message");
mailMsg->set_Body(u"This is a test body");
// every file added here becomes an attachment of the MSG
mailMsg->get_Attachments()->Add(MakeObject<Attachment>(u"1.jpg"));
mailMsg->get_Attachments()->Add(MakeObject<Attachment>(u"description.html"));
SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"MessageWithAttachments.msg");
Console::WriteLine(u"Attachments in the saved message: " +
Convert::ToString(MapiMessage::Load(u"MessageWithAttachments.msg")->get_Attachments()->get_Count()));