การโหลด ดู และแยกไฟล์ 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);