יצירה ושמירת קבצי MSG

Aspose.Email תומך ביצירת קבצי הודעת Outlook (MSG). מאמר זה מסביר כיצד:

  • צור הודעות MSG.
  • צור הודעות MSG עם קבצים מצורפים.
  • צור הודעת MSG עם גוף RTF.
  • שמור הודעה כטיוטה.
  • עבודה עם דחיסת גוף.

יצירת ושמירת הודעות Outlook

ה MailMessage מחלקה מכילה את שמירה מתודה שיכולה לשמור קבצי Outlook MSG לכונן או ל‑stream. הקטעי קוד למטה יוצרים מופע של ה‑ MailMessage מחלקה, הגדר מאפיינים כמו from, to, subject ו‑body. ה‑ שמירה מתודה מקבלת את שם הקובץ כארגומנט. בנוסף, ניתן ליצור הודעות Outlook עם a גוף RTF דחוס באמצעות ה- MapiConversionOptions.

  1. צור מופע חדש של ה‑ MailMessage מחלקה ומגדירה את המאפיינים From, To, Subject ו‑Body.
  2. הפעל את MapiMessage מחלקה fromMailMessage מתודה שמקבלת את האובייקט של ה‑ MailMessage סוג. ה‑ fromMailMessage מתודה ממירה את MailMessage ל‑ MapiMessage (MSG).
  3. הפעל את MapiMessage.save מתודה לשמירת קובץ MSG.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");

// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);

// Save the message (MSG) file
String strMsgFile = "CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.save(dataDir + strMsgFile);

יצירת קבצי MSG עם קבצים מצורפים

בדוגמה למעלה, יצרנו קובץ MSG פשוט. Aspose.Email גם תומך בשמירת קבצי הודעות עם קבצים מצורפים. כל שעליך לעשות הוא להוסיף את הקבצים המצורפים ל‑ MailMessage מופע. הוסף קבצים מצורפים על‑ידי קריאה למתודה addItem על ה‑ MailMessage.Attachments אוסף.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

String[] files = new String[2];
files[0] = "attachment.doc";
files[1] = "attachment.png";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");

// Add the attachments
for (String strFileName : files)
{
    mailMsg.getAttachments().addItem(new Attachment(strFileName));
}

// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
String strMsgFile = "CreateMessagesWithAttachments.msg";
outlookMsg.save(dataDir + strMsgFile);

יצירת קבצי MSG עם גוף RTF

ניתן גם ליצור קבצי Outlook Message (MSG) עם גופי טקסט עשיר (RTF) באמצעות Aspose.Email. גוף ה‑RTF תומך בעיצוב טקסט. צור אחד על‑ידי הגדרת ה‑ MailMessage.HtmlBody מאפיין. כאשר ממירים a MailMessage מופע ל‑ MapiMessage במקרה זה, גוף ה‑HTML מומר ל‑RTF. כך נשמר העיצוב של גוף האימייל.

הדוגמה הבאה יוצרת קובץ MSG עם גוף RTF. יש כותרת אחת, ועיצוב מודגש וקו תחתי מיושמים בגוף ה‑HTML. עיצוב זה נשמר כאשר ה‑HTML מומר ל‑RTF.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setHtmlBody("<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");

MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
outlookMsg.save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");

שמירת הודעה במצב טיוטה

הודעות אימייל נשמרות כטיוטות כאשר מישהו החל לערוך אותן אך רוצה לחזור אליהן כדי להשלים אותן מאוחר יותר. Aspose.Email תומך בשמירת הודעות אימייל במצב טיוטה על ידי הגדרת דגל הודעה. להלן קוד לדוגמה לשמירת הודעת Outlook (MSG) כטיוטה.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Change properties of an existing MSG file
String strExistingMsg = "message.msg";

// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.setSubject(msg.getSubject() + " NEW SUBJECT (updated by Aspose.Email)");
msg.setHtmlBody(msg.getHtmlBody() + " NEW BODY (udpated by Aspose.Email)");

// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.fromMailMessage(msg);
mapiMsg.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.save(dataDir + "SavingMessageInDraftStatus_out.msg");

השלכות של דחיסת גוף

שיטת דחיסת גוף RTF יכולה לשמש ליצירת MSG בגודל קטן יותר. עם זאת, היא גורמת למהירות יצירה איטית יותר. כדי ליצור הודעות במהירות משופרת, הגדר את הדגל ל‑false. דגל זה, בתורו, משפיע על קבצי PST שנוצרו: קבצי MSG קטנים יותר מניבים PST קטן יותר, וקבצי MSG גדולים יותר גורמים ליצירת PST איטית יותר.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";

MailMessage message = MailMessage.load(fileName);
MapiConversionOptions options = new MapiConversionOptions();
options.setUseBodyCompression(true);
MapiMessage ae_mapi = MapiMessage.fromMailMessage(message, options);