การโหลด ดู และแยกไฟล์ 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 = "outlook/";
// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.load(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.load(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());
}
โหลดไฟล์ MSG โดยไม่อ่านเนื้อหาแนบ
โดยค่าเริ่มต้น MapiMessage.load อ่านเนื้อหาของทุกแนบเข้าในหน่วยความจำพร้อมข้อความ สำหรับข้อความที่มีแนบขนาดใหญ่ หน่วยความจำที่ต้องใช้เพื่อเปิดข้อความจะเพิ่มตามขนาดของแนบ แม้ว่าคุณต้องการเพียงหัวเรื่อง ผู้รับ หรือชื่อแนบเท่านั้น
ด้วยการโหลดแบบล่าช้า เนื้อหาแนบจะคงอยู่ในแหล่งข้อมูลและจะอ่านเมื่อคุณเข้าถึงเท่านั้น เปิดใช้งานโดยใช้คุณสมบัติต่อไปนี้ของ MsgLoadOptions คลาส:
setDeferAttachmentContent(boolean)- เมื่อtrue, เนื้อหาแนบจะอ่านจากแหล่งข้อมูลตามความต้องการแทนการโหลดพร้อมข้อความ ค่าเริ่มต้นคือfalse.setDeferralThreshold(int)- ขนาดเนื้อหาอย่างต่ำ (ไบต์) ที่จะใช้การโหลดแบบล่าช้า แนบไฟล์ที่เล็กกว่า จะถูกโหลดพร้อมกับข้อความ ค่าเริ่มต้นคือ 4096
การโหลดแบบล่าช้าประยุกต์กับเนื้อหาไฟล์แนบแบบไบต์ ไฟล์แนบที่เก็บเป็นอ็อบเจกต์ OLE หรือเป็นข้อความฝังจะถูกแยกเมื่อโหลดข้อความ
โค้ดตัวอย่างต่อไปนี้ตรวจสอบข้อความโดยไม่อ่านแนบ:
MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);
// The file stays open until the message is disposed
try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
System.out.println("Subject: " + msg.getSubject());
System.out.println("Recipients: " + msg.getRecipients().size());
for (MapiAttachment attachment : msg.getAttachments()) {
// Names and sizes are available without reading the content
System.out.println(" " + attachment.getLongFileName() + " : " + attachment.getContentLength() + " bytes");
}
}
โค้ดตัวอย่างต่อไปนี้ดึงเฉพาะแนบ PDF ของข้อความ เนื้อหาแนบอื่นจะไม่ถูกอ่านจากไฟล์เลย:
MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);
try (MapiMessage msg = MapiMessage.load("large.msg", options)) {
for (MapiAttachment attachment : msg.getAttachments()) {
if (!attachment.getLongFileName().endsWith(".pdf")) {
continue;
}
try (InputStream source = attachment.openRead()) {
Files.copy(source, Paths.get("out", attachment.getLongFileName()), StandardCopyOption.REPLACE_EXISTING);
}
}
}
โค้ดตัวอย่างต่อไปนี้โหลดข้อความจากสตรีมที่เป็นของผู้เรียกใช้:
MsgLoadOptions options = new MsgLoadOptions();
options.setDeferAttachmentContent(true);
// Keep the stream open while the message is in use
try (InputStream source = Files.newInputStream(Paths.get("large.msg"))) {
MapiMessage msg = MapiMessage.load(source, options);
byte[] data = msg.getAttachments().get(0).getBinaryData();
System.out.println("Read " + data.length + " bytes");
}
เพื่ออ่านเนื้อหาแนบเป็นสตรีมและเพิ่มแนบขนาดใหญ่โดยไม่โหลดเข้าในหน่วยความจำ ดู ทำงานกับไฟล์แนบของข้อความ.
แปลง EML เป็น MSG พร้อมคงรูปแบบ EML ที่ฝังอยู่
ไฟล์ EML สามารถโหลดเข้าไปใน MapiMessage คลาสโดยการสร้าง MailMessage อ็อบเจ็กต์และส่งผ่านไปยัง MapiMessage.fromMailMessage เมธอด หากไฟล์ EML มีไฟล์ EML ฝังอยู่ ให้ใช้ MapiConversionOptions.setPreserveEmbeddedMessageFormat เพื่อรักษารูปแบบของไฟล์ EML ที่ฝังอยู่ โค้ดสแนปด้านล่างแสดงวิธีโหลดไฟล์ EML เข้าไปใน MapiMessage พร้อมคงรูปแบบของไฟล์ EML ที่ฝังอยู่
ลองใช้ดู!
แปลงอีเมลและคลังข้อความออนไลน์ด้วยเวอร์ชันฟรี แอปแปลง Aspose.Email.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "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);