טעינה, צפייה ופירוש קובץ MSG

נושא זה מסביר כיצד לטעון קובץ הודעת Microsoft Outlook (*.msg). ה‑ MapiMessage המחלקה משמשת לטעינת קבצי MSG ומספקת מספר פונקציות טעינה סטטיות לתרחישים שונים. הקוד שלהלן מציג כיצד לטעון קבצי MSG מקובץ או מזרם.

טעינת קבצי MSG

הקטע הקוד שלהלן מציג כיצד לטעון קבצי 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 = RunExamples.getDataDir_Outlook();

// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");

// Get subject
System.out.println("Subject:" + msg.getSubject());

// Get from address
System.out.println("From:" + msg.getSenderEmailAddress());

// Get body
System.out.println("Body" + msg.getBody());

// Get recipients information
System.out.println("Recipient: " + msg.getRecipients());

// Get attachments
for (MapiAttachment att : msg.getAttachments())
{
    System.out.println("Attachment Name: " + att.getFileName());
    System.out.println("Attachment Display Name: " + att.getDisplayName());
}

דוגמת הקוד שלהלן מציגה כיצד להשתמש ב‑MailMessage לטעון הודעה בפורמט MSG.

MailMessage eml = MailMessage.load("message.msg");

יש לציין שההודעה המתקבלת מומרת לפורמט EML, כולל קבצי ההודעה המשובצים. אל תשתמש בשיטת טעינה זו אם ברצונך לשמר תכונות פורמט msg ספציפיות של ההודעה המקורית.

כדי לשמר את הפורמט המקורי של קבצי ההודעה המשובצים, השתמש ב‑ MsgLoadOptions.PreserveEmbeddedMessageFormat מאפיין.

MsgLoadOptions msgLoadOptions = new MsgLoadOptions();
msgLoadOptions.setPreserveEmbeddedMessageFormat(true);
MailMessage msg = MailMessage.load(stream, msgLoadOptions);

טעינה מזרם

הקוד שלהלן מציג כיצד לטעון קובץ מזרם.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of MapiMessage from file
try (FileInputStream stream = new FileInputStream(dataDir + "message.msg"))
{
    // Create an instance of MapiMessage from file
    MapiMessage msg = MapiMessage.fromStream(stream);

    // Get subject
    System.out.println("Subject:" + msg.getSubject());

    // Get from address
    System.out.println("From:" + msg.getSenderEmailAddress());

    // Get body
    System.out.println("Body" + msg.getBody());

}

המרת EML ל‑MSG תוך שמירת פורמט EML משובץ

קבצי EML ניתנים לטעינה לתוך MapiMessage מחלקה על‑ידי יצירת מופע של MailMessage אובייקט ולהעבירו ל‑ MapiMessage.fromMailMessage שיטה. אם קובץ ה‑EML מכיל קבצי EML משובצים, השתמש ב‑ MapiConversionOptions.setPreserveEmbeddedMessageFormat כדי לשמור על הפורמט של קבצי EML משובצים. הקטע הקוד שלהלן מציג כיצד לטעון קבצי EML לתוך MapiMessage במקביל לשימור הפורמט של קבצי EML משובצים.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = RunExamples.getDataDir_Email();

MailMessage eml = MailMessage.load(dataDir + "sample.eml", new EmlLoadOptions());

MapiConversionOptions options = MapiConversionOptions.getUnicodeFormat();

//Preserve Embedded Message Format
options.setPreserveEmbeddedMessageFormat(true);

//Convert EML to MSG with Options
MapiMessage msg = MapiMessage.fromMailMessage(eml, options);