יצירה ושמירת קבצי 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: קבע את השולח, הנמענים, הנושא והגוף, ולאחר מכן הפוך אותו לפריט Outlook עם 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. ה הוסף מתודות של ה- MapiRecipientCollection מחלקה מחזירה את שנוצר MapiRecipient, וה- הוסף מתודות של ה- MapiAttachmentCollection מחלקה מחזירה את שנוצר 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");
צור קובצי MSG עם גוף RTF
Outlook שומר את גוף ההודעה המעוצב כRich Text Format (RTF). כאשר MailMessage כאשר גוף HTML מומר, Aspose.Email בונה את גוף ה‑RTF מה‑HTML, כך שכותרות, טקסט מודגש ומודגש בקו תחתון נשארים בקובץ 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 (עם 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 → המרה מהירה יותר, פלט גדול יותר.
Create an MSG File with Attachments
קבצים מצורפים מתווספים ל‑ 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()));