ניהול קבצי הודעות עם Aspose.Email.Outlook

המרת MSG להודעת MIME

ממשק ה‑API של Aspose.Email מספק אפשרות להמרת קובצי MSG להודעות MIME באמצעות ה- toMailMessage שיטה.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = new MapiMessage(
                            "sender@test.com",
                            "recipient1@test.com; recipient2@test.com",
                            "Test Subject",
                            "This is a body of message.");
MailConversionOptions options = new MailConversionOptions();
options.setConvertAsTnef(true);
MailMessage mail = msg.toMailMessage(options);

המרת MSG ל‑EML תוך שמירת גוף RTF

ה‑API מספק את השיטות הבאות לשימור גוף RTF בעת המרת MSG ל‑EML:

דגימות הקוד שלהלן ממחישות כיצד לשמור את גוף ה‑rtf ב‑MailMessage:

MsgLoadOptions options = new MsgLoadOptions();
options.setPreserveRtfContent(true);
MailMessage message = MailMessage.load("fileName", options);
MapiMessage mapi = MapiMessage.load("fileName");
MailConversionOptions options = new MailConversionOptions();
options.setPreserveRtfContent(true);
MailMessage message = mapi.toMailMessage(options);

המרת MSG ל‑MHTML תוך שמירת כותרת הקטגוריה

ממשק ה‑API של Aspose.Email מספק אפשרות להוסיף כותרת קטגוריה בעת המרת הודעה ל‑MHTML. תכונה זו מוגדרת על‑ידי MhtSaveOptions class כאפשרות נוספת בעת שמירת MailMessage לפורמט Mhtml.

דוגמת הקוד הבאה ממחישה כיצד ליצור קובץ MHT (MHTML) מאובייקט MapiMessage, להתאים את ההצגה והכותרות של קובץ ה‑MHT באמצעות MhtSaveOptions, להגדיר קטגוריות להודעת הדוא"ל ולאחר מכן לשנות את תבניות הפורמט והכותרות לפני שמירת הקובץ.

 MapiMessage msg = new MapiMessage("from@aaa.com", "to@aaa.com", "subj", "body");

msg.setCategories(new String[] { "Urgently", "Important" });

MhtSaveOptions saveOptions = new MhtSaveOptions();

saveOptions.getFormatTemplates().set_Item(MhtTemplateName.CATEGORIES,

    saveOptions.getFormatTemplates().get_Item(MhtTemplateName.CATEGORIES).replace("Categories", "Les catégories"));

saveOptions.getRenderingHeaders().add(MhtTemplateName.CATEGORIES);

msg.save("fileName.mhtml", saveOptions);

קריאה וכתיבה של קובץ תבנית Outlook (.OFT)

תבניות Outlook שימושיות מאוד כאשר ברצונך לשלוח הודעת דוא"ל דומה שוב ושוב. במקום להכין את ההודעה מאפס בכל פעם, תחילה הכין את ההודעה ב‑Outlook ושמור כ‑Outlook Template (OFT). לאחר ذلك, בכל פעם שצריך לשלוח את ההודעה, אפשר ליצור אותה מהתבנית, מה שחוסך זמן בכתיבת הטקסט זהה בגוף או בכותרת, קביעת עיצוב וכדומה. Aspose.Email MailMessage class ניתן להשתמש כדי לטעון ולקרוא קובץ תבנית Outlook (OFT). ברגע שתבנית Outlook נטענת במופע של MailMessage class, תוכל לעדכן את השולח, הנמען, הגוף, הנושא ונתונים נוספים. לאחר עדכון המאפיינים:

  • שלח את האימייל באמצעות SmtpClient class או
  • שמור את ההודעה כ‑MSG ובצע עדכונים/אימות נוספים באמצעות Microsoft Outlook.

במקטעי הקוד שלהלן, אנו:

  1. טען את התבנית באמצעות ה- MailMessage מחלקה.
  2. עדכן חלק מהמאפיינים.
  3. שמור את ההודעה בפורמט MSG.

קטע הקוד הבא מראה כיצד לטעון את קובץ ה‑OFT, לעדכן את ההודעה ולשמור אותו בפורמט 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/";

// Load the Outlook template (OFT) file in MailMessage's instance
MailMessage message = MailMessage.load(dataDir + "sample.oft", new MsgLoadOptions());

// Set the sender and recipients information
String senderDisplayName = "John";
String senderEmailAddress = "john@abc.com";
String recipientDisplayName = "William";
String recipientEmailAddress = "william@xzy.com";

message.setSender(new MailAddress(senderEmailAddress, senderDisplayName));
message.getTo().addMailAddress(new MailAddress(recipientEmailAddress, recipientDisplayName));
message.setHtmlBody(message.getHtmlBody().replace("DisplayName", "<b>" + recipientDisplayName + "</b>"));

// Set the name, location and time in email body
String meetingLocation = "<u>" + "Hall 1, Convention Center, New York, USA" + "</u>";
String meetingTime = "<u>" + "Monday, June 28, 2010" + "</u>";
message.setHtmlBody(message.getHtmlBody().replace("MeetingPlace", meetingLocation));
message.setHtmlBody(message.getHtmlBody().replace("MeetingTime", meetingTime));

// Save the message in MSG format and open in Office Outlook
MapiMessage mapimessage = MapiMessage.fromMailMessage(message);
mapimessage.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapimessage.save(dataDir + "ReadAndWritingOutlookTemplateFile_out.msg");

שמירת קובץ Outlook MSG כתבנית

קטע הקוד הבא מראה כיצד לשמור את קובץ ה‑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/";

try (MapiMessage mapi = new MapiMessage("test@from.to", "test@to.to", "template subject", "Template body")) {
    mapi.saveAsTemplate(dataDir + "mapiToOft.msg");
}

קביעת קטגוריית צבע לקבצי Outlook MSG

קטגוריית צבע מסמנת הודעת דוא"ל לחשיבות או לקטגוריה מסוימת. Microsoft Outlook מאפשרת למשתמשים לשייך קטגוריות צבע כדי להבדיל בין הודעות. כדי לטפל בקטגוריית הצבע, השתמש ב- FollowUpManager. הוא מכיל פונקציות כגון addCategory, removeCategory, clearCategories ו getCategories.

  • addCategory מקבל MapiMessage והמחרוזת של קטגוריית הצבע, לדוגמה "קטגוריית סגול" או "קטגוריית אדום" כפרמטרים.
  • removeCategory מקבל MapiMessage והמחרוזת של קטגוריית הצבע שתוסר מההודעה.
  • clearCategories משמש להסרת כל קטגוריות הצבע מההודעה.
  • getCategories משמש לשחזור כל קטגוריות הצבע מהודעה מסוימת.

הדוגמה שלהלן מבצעת את המשימות כמפורט להלן:

  1. הוסף קטגוריית צבע.
  2. הוסף קטגוריית צבע נוספת.
  3. קבל את רשימת כל הקטגוריות.
  4. הסר את כל הקטגוריות.
// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");

// Add Two category
FollowUpManager.addCategory(msg, "Purple Category");
FollowUpManager.addCategory(msg, "Red Category");

// Retrieve the list of available categories
IList categories = FollowUpManager.getCategories(msg);

// Remove the specified category and then Clear all categories
FollowUpManager.removeCategory(msg, "Red Category");
FollowUpManager.clearCategories(msg);

גישה למידע המשך מקובץ MSG

ממשק ה-API של Aspose.Email מספק אפשרות לגשת למידע המשך מהודעה שנשלחה או התקבלה. הוא יכול לשחזר מידע על קריאה, קבלת קבלה וקבלת תוצאות הצבעה מקובץ הודעה.

קבלת מידע על קבלת קריאה וקבלת משלוח

קטע הקוד הבא מראה כיצד לקבל מידע על קבלת קריאה וקבלת משלוח.

// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");
for (MapiRecipient recipient : msg.getRecipients()) {
    System.out.println("Recipient: " + recipient.getDisplayName());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY property
    System.out.println("Delivery time: " + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY).getDateTime());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_READ property
    System.out.println("Read time" + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_READ).getDateTime());
}

יצירת הודעות Forward ו‑Reply

Aspose.Email API מספקת אפשרות ליצור ולעצב הודעות Forward ו‑Reply. ה‑ ReplyMessageBuilder ו ForwardMessageBuilder מחלקות ה‑API משמשות ליצירת הודעות Reply ו‑Forward בהתאמה. ניתן לציין הודעת Reply או Forward שתיווצר באמצעות כל אחת מהשיטות של OriginalMessageAdditionMode enum. enum זה מכיל את הערכים הבאים:

  • OriginalMessageAdditionMode.None - ההודעה המקורית אינה נכללת בהודעת התגובה.
  • OriginalMessageAdditionMode.Attachment - ההודעה המקורית כלולה כקובץ מצורף בהודעת התגובה
  • OriginalMessageAdditionMode.Textpart - ההודעה המקורית כלולה כטקסט בגוף הודעת התגובה

יצירת הודעת Reply

קוד הדוגמה הבא מראה כיצד ליצור הודעת Reply.

// 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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ReplyMessageBuilder builder = new ReplyMessageBuilder();

// Set ReplyMessageBuilder Properties
builder.setReplyAll(true);
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
builder.setResponseText(
        "<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>");

MapiMessage replyMsg = builder.buildResponse(originalMsg);
replyMsg.save(dataDir + "reply_out.msg");

יצירת הודעת Forward

קוד הדוגמה הבא מראה כיצד ליצור הודעת Forward.

// 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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ForwardMessageBuilder builder = new ForwardMessageBuilder();
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
MapiMessage forwardMsg = builder.buildResponse(originalMsg);
forwardMsg.save(dataDir + "forward_out.msg");

שימור תאריכים ריקים בעת המרת הודעה

MapiConversionOptions.setPreserveEmptyDates(boolean) מאפיין המציין האם יש צורך לשמור תאריכים ריקים בעת המרת הודעה. API זה מופיע ב‑Aspose.Email 21.5. קוד הדוגמה הבא מראה כיצד לשמר תאריכים ריקים.

MailMessage mailMessage = MailMessage.load("message.eml");
System.out.println(mailMessage.getDate()); // zero date
MapiConversionOptions mco = MapiConversionOptions.getUnicodeFormat();
// keep empty dates when converting a message
mco.setPreserveEmptyDates(true);
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage, mco);
System.out.println(mapiMessage.getClientSubmitTime()); // zero date
// check zero date
if (mapiMessage.getClientSubmitTime().equals(JavaHelper.ZERO_DATE))
    System.out.println("ZERO DATE");