ทำงานกับไฟล์แนบของข้อความ
การแยกและบันทึกไฟล์แนบ
ไฟล์ข้อความ Outlook อาจมีไฟล์แนบหนึ่งหรือหลายไฟล์ Aspose.Email ช่วยให้ผู้พัฒนาวนลูปไฟล์แนบในไฟล์ MSG และบันทึกลงดิสก์ หัวข้อนี้อธิบายกระบวนการและวิธีฝังไฟล์แนบ.
Aspose.Email MapiMessage คลาสใช้ในการโหลดไฟล์ MSG จากดิสก์และเปิดเผย getAttachments() เมธอดที่อ้างอิงถึง MapiAttachment คอลเลกชันอ็อบเจกต์ที่เชื่อมโยงกับไฟล์ MSG. The MapiAttachment อ็อบเจกต์ยังเปิดเผยเมธอดเพิ่มเติมที่ดำเนินการกับไฟล์แนบ.
เพื่อบันทึกไฟล์แนบในไฟล์ MSG ลงดิสก์โดยใช้ชื่อและส่วนขยายเดิม:
- สร้างอินสแตนซ์ของ MapiMessage คลาสเพื่อโหลดไฟล์ MSG ด้วย Load() เมธอดสเตติก.
- เรียกใช้ MapiRecipient คลาส getAttachments() เมธอดเพื่อรับอ้างอิงถึงคอลเลกชันของ MapiAttachment อ็อบเจกต์ที่เชื่อมโยงกับไฟล์ MSG.
- วนลูปผ่าน MapiAttachmentCollection เพื่อแสดงเนื้อหาเกี่ยวกับแต่ละ MapiAttachment อ็อบเจกต์ผ่านเมธอดสาธารณะของมัน.
- เรียกใช้ MapiAttachment คลาส save() เมธอดเพื่อบันทึกไฟล์แนบลงดิสก์.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
// Load an MSG file from disk
MapiMessage outlookMessageFile = MapiMessage.load(dataDir + "WithEmbeddedMsg.msg");
// Loop through the attachments collection of the MapiMessage object
for (MapiAttachment attachment : outlookMessageFile.getAttachments()) {
// Display the attachment type
System.out.println("Att Type : " + attachment.getMimeTag());
// Display the attached file name
System.out.println("File Name : " + attachment.getLongFileName());
// Save the attachment to disk
attachment.save(dataDir + attachment.getDisplayName());
}
ฝังข้อความเป็นไฟล์แนบ
ข้อความ Microsoft Outlook สามารถมีข้อความ Microsoft Outlook อื่นเป็นไฟล์แนบได้ ไม่ว่าจะเป็นข้อความปกติตามที่อธิบายข้างต้น หรือข้อความฝัง. The MapiAttachmentCollection ให้เมธอด overloaded ของ add สำหรับสร้างข้อความ Outlook พร้อมแนบทั้งสองประเภท ไฟล์ Outlook MSG ที่ฝังในไฟล์ MSG มี PR_ATTACH_METHOD มีค่า 5
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MapiMessage msg = new MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body");
MapiMessage attachMsg = MapiMessage.load(dataDir + "message.msg");
msg.getAttachments().add("Weekly report", attachMsg);
msg.save(dataDir + "EmbeddedMessageAsAttachment.msg");
การอ่านข้อความฝังจากไฟล์แนบ
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MapiMessage mapi = MapiMessage.load(dataDir + "EmbeddedMessageAsAttachment.msg");
MapiMessage embedded = mapi.getAttachments().get_Item(0).getObjectData().toMapiMessage();
System.out.println("Embedded subject: " + embedded.getSubject());
เพิ่มไฟล์แนบ MSG
ข้อความ Outlook สามารถบรรจุข้อความ Microsoft Outlook อื่นเป็นไฟล์แนบได้ทั้งแบบปกติและแบบฝังอยู่ คำสั่ง MapiAttachmentCollection ให้สมาชิกที่มีการ overload ของ add เมธอดเพื่อสร้างข้อความ Outlook ที่มีไฟล์แนบทั้งสองประเภท:
add(String name, byte[] data)— เพิ่มไฟล์แนบทั่วไปจากอาร์เรย์ไบท์add(String name, MapiMessage message)— เพิ่มข้อความ Outlook อีกข้อความหนึ่งเป็นข้อความที่ฝังอยู่
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีเพิ่มไฟล์แนบทั้งสองประเภทลงในข้อความ:
MapiMessage message = new MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body");
// Add a regular attachment from a byte array
message.getAttachments().add("note.txt", Files.readAllBytes(Paths.get("note.txt")));
// Add another Outlook message as an embedded message
MapiMessage attachMsg = MapiMessage.load("Message.msg");
message.getAttachments().add("Weekly report.msg", attachMsg);
message.save("WithAttachments_out.msg");
MapiMessage.fromFile ไม่มีใน Aspose.Email for Java — ใช้ MapiMessage.load เพื่ออ่านไฟล์ MSG จากดิสก์
เข้าถึงไฟล์แนบที่เพิ่งเพิ่ม
นี้ add เมธอดของ MapiAttachmentCollection คลาส return the MapiAttachment อ็อบเจ็กต์ที่พวกเขาสร้างขึ้น นี้ทำให้คุณเข้าถึงไฟล์แนบใหม่ได้ทันที ดังนั้นคุณสามารถกำหนดค่ามันได้โดยตรงหลังการแทรกโดยไม่ต้องค้นหาจากคอลเลกชัน เมธอดโอเวอร์โหลดต่อไปนี้คืนไฟล์แนบที่สร้างขึ้น:
- add(String name, byte[] data)
- add(String name, MapiMessage msg)
- add(String name, ReferenceAttachmentOptions options)
โค้ดสแนปต่อไปนี้เพิ่มไฟล์แนบและเปลี่ยนชื่อแสดงผลของมัน:
MapiMessage message = new MapiMessage("from@domain.com", "to@domain.com", "Invoice", "Please find the invoice attached.");
MapiAttachment attachment = message.getAttachments().add("invoice.pdf", Files.readAllBytes(Paths.get("invoice.pdf")));
attachment.setDisplayName("Invoice #2026-001.pdf");
message.save("invoice.msg");
เพิ่มไฟล์แนบจากสตรีมหรือไฟล์
เมื่อเพิ่มไฟล์แนบจากอาร์เรย์ไบต์ เนื้อหาทั้งหมดต้องถูกเก็บไว้ในหน่วยความจำ สำหรับไฟล์ขนาดใหญ่ ให้ส่งเนื้อหาเป็นสตรีมหรือดึงโดยตรงจากไฟล์แทน: เนื้อหาจะถูกอ่านตามความต้องการขณะบันทึกข้อความและจะไม่ถูกคัดลอกเข้าหน่วยความจำเต็มรูปแบบ MapiAttachmentCollection คลาสนี้ให้เมธอดต่อไปนี้สำหรับการทำงานนี้:
- add(String name, InputStream content) - เพิ่มไฟล์แนบที่เนื้อหาถูกอ่านจากสตรีม ตั้งแต่ตำแหน่งปัจจุบันจนถึงจบ สตรีมจะอยู่ภายใต้การควบคุมของคุณและต้องเปิดอยู่จนกว่าข้อความจะถูกบันทึก
addFile(String filePath)- เพิ่มไฟล์แนบจากไฟล์และใช้ชื่อไฟล์เป็นชื่อไฟล์แนบaddFile(String filePath, String displayName)- เพิ่มไฟล์แนบจากไฟล์ที่มีชื่อแสดงผลตามที่ระบุ
ทั้งหมดของวิธีการเหล่านี้จะคืนค่าอ็อบเจ็กต์ที่สร้างขึ้น MapiAttachment อ็อบเจ็กต์. Its getContentLength() เมธอดคืนขนาดของเนื้อหาแนบเป็นไบต์โดยไม่อ่านเนื้อหา
โค้ดตัวอย่างต่อไปนี้แทรกไฟล์ขนาดใหญ่ เนื้อหาถูกอ่านจากไฟล์ขณะบันทึกข้อความ ดังนั้นให้ Dispose ข้อความเมื่อไม่ต้องการใช้ต่อ - คำสั่ง try-with-resources ทำเช่นนั้น:
try (MapiMessage msg = new MapiMessage("from@domain.com", "to@domain.com", "Quarterly report", "See the archive attached.")) {
msg.getAttachments().addFile("archive.zip");
msg.save("report.msg");
}
โค้ดตัวอย่างต่อไปนี้เพิ่มแนบจากสตรีมที่มีอยู่และพิมพ์ขนาดของมัน:
try (InputStream content = Files.newInputStream(Paths.get("presentation.pptx"))) {
MapiMessage msg = new MapiMessage("from@domain.com", "to@domain.com", "Slides", "Slides attached.");
MapiAttachment attachment = msg.getAttachments().add("presentation.pptx", content);
// The size is known without reading the content
System.out.println("Attachment size: " + attachment.getContentLength() + " bytes");
// The stream must stay open until the message is saved
msg.save("slides.msg");
}
อ่านเนื้อหาไฟล์แนบเป็นสตรีม
นี้ openRead() เมธอดของ MapiAttachment คลาสคืนสตรีมแบบอ่านอย่างเดียวของเนื้อหาแนบ ใช้เพื่อคัดลอมหรือประมวลผลแนบขนาดใหญ่โดยไม่ต้องแปลงเป็นอาร์เรย์ไบต์ ทุกการเรียกคืนสตรีมแยกอิสระ ดังนั้นจึงใช้ผู้อ่านหลายตัวพร้อมกันได้
MapiMessage msg = MapiMessage.load("message.msg");
for (MapiAttachment attachment : msg.getAttachments()) {
System.out.println(attachment.getLongFileName() + " : " + attachment.getContentLength() + " bytes");
// Stream the content to a file instead of reading it into a byte array
try (InputStream source = attachment.openRead()) {
Files.copy(source, Paths.get("out", attachment.getLongFileName()), StandardCopyOption.REPLACE_EXISTING);
}
}
เพื่อเปิดข้อความโดยไม่อ่านเนื้อหาแนบเลย ใช้การโหลดแนบแบบล่าช้า รายละเอียดใน การโหลด ดู และแยกไฟล์ MSG.
เพิ่มไฟล์แนบอ้างอิงให้กับ MapiMessages
นี้ ReferenceAttachmentOptions คลาสนี้ทำให้การเพิ่มไฟล์แนบอ้างอิงเป็นเรื่องง่ายโดยบรรจุคุณสมบัติที่จำเป็นทั้งหมดไว้ในอ็อบเจ็กท์เดียว
- sharedLink — ลิงก์ที่แชร์เต็มรูปแบบไปยังไฟล์แนบที่ให้โดยบริการเว็บโฮสต์ไฟล์
- url — ที่ตั้งไฟล์หรือ URL ของทรัพยากร
- providerName — ชื่อของผู้ให้บริการไฟล์แนบอ้างอิง (เช่น Google Drive, Dropbox)
MapiMessage msg = MapiMessage.load("message.msg");
ReferenceAttachmentOptions options = new ReferenceAttachmentOptions(
"https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
"https://drive.google.com/drive/my-drive",
"GoogleDrive");
// Add the reference attachment
msg.getAttachments().add("Document.pdf", options);
msg.save("with-reference-attachment.msg");
ตรวจสอบประเภทไฟล์แนบ (IsReference)
นี้ MapiAttachment คลาสนี้รวมถึง isReference() เมธอดที่ช่วยให้นักพัฒนาตรวจสอบไฟล์แนบอ้างอิงในข้อความ:
MapiMessage msg = MapiMessage.load("message.msg");
for (MapiAttachment attachment : msg.getAttachments()) {
if (attachment.isReference()) {
// Process the reference attachment
System.out.println("Reference attachment: " + attachment.getLongFileName());
}
}
ดึงไฟล์แนบของข้อความเมลที่ซ้อนกัน
ไฟล์แนบข้อความที่ฝังอยู่ยังปรากฏในคอลเล็กชันไฟล์แนบของ MapiMessage คลาส. เมธอด MapiMessage.fromProperties() เมธอดสแตติกนี้สร้างข้อความใหม่จากไฟล์แนบที่ฝังอยู่ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีดึงไฟล์แนบเมลที่ซ้อนกันและบันทึกลงดิสก์:
MapiMessage msg = MapiMessage.load("message.msg");
for (MapiAttachment attachment : msg.getAttachments()) {
if (attachment.getObjectData() == null) {
continue;
}
// Create a MapiMessage object from the individual attachment
MapiMessage nested = MapiMessage.fromProperties(attachment.getObjectData().getProperties());
// Convert the embedded message to a MailMessage and save it to disk
MailMessage mailMessage = nested.toMailMessage(new MailConversionOptions());
mailMessage.save("NestedMailMessageAttachments_out.eml", SaveOptions.getDefaultEml());
}
ลบไฟล์แนบ
Aspose.Email ให้ความสามารถในการลบไฟล์แนบจากไฟล์ข้อความ Microsoft Outlook (.msg) เรียกเมธอดแบบสแตติก removeAttachments() เมธอดซึ่งรับพาธของไฟล์ข้อความเป็นพารามิเตอร์:
MapiMessage.removeAttachments("AttachmentsToRemove_out.msg");
คุณสามารถเรียกใช้ destroyAttachments() วิธีการนี้ทำงานเร็วกว่า removeAttachments()เพราะตัวหลังจะทำการวิเคราะห์ไฟล์ข้อความ:
MapiMessage.destroyAttachments("AttachmentsToDestroy_out.msg");
การแทรกและแทนที่ไฟล์แนบ MSG
Aspose.Email API ให้ความสามารถในการแทรกไฟล์แนบที่ตำแหน่งเฉพาะในข้อความพาเรนท์ นอกจากนี้ยังให้ความสะดวกในการแทนที่เนื้อหาของไฟล์แนบด้วยไฟล์แนบจากข้อความอื่น.
แทรกไฟล์แนบ MSG ที่ตำแหน่งเฉพาะ
Aspose.Email API ให้ความสามารถในการแทรกไฟล์แนบ MSG ไปยัง MSG พาเรนท์โดยใช้ MapiAttachmentCollection.Insert() เมธอด.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "WithEmbeddedMsg.msg");
msg.getAttachments().get_Item(0).save(dataDir + "attachment_out.msg");
MapiMessage emb = MapiMessage.load(dataDir + "WithEmbeddedMsg.msg");
// Insert the message as the attachment at index 1
msg.getAttachments().insert(1, "new 11", emb);
msg.save(dataDir + "insertMSGAttachment_out.msg");
แทนที่เนื้อหาไฟล์แนบ MSG ที่ฝัง
สามารถใช้เพื่อแทนที่เนื้อหาไฟล์แนบที่ฝังด้วยไฟล์ใหม่โดยใช้ แทนที่ เมธอด อย่างไรก็ตาม ไม่สามารถใช้แทรกแนบที่มี PR_ATTACH_NUM = 4 (เช่น) ในคอลเลกชันที่ Count = 2
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "insertMSGAttachment_out.msg");
msg.getAttachments().get_Item(0).save(dataDir + "attachment_out.msg");
MapiMessage emb = MapiMessage.load(dataDir + "insertMSGAttachment_out.msg");
// Replace the contents of the attachment at index 1
msg.getAttachments().replace(1, "new 1", emb);
msg.save(dataDir + "replaceEmbeddedMSGAttachment_out.msg");
บันทึกไฟล์แนบจากข้อความที่เซ็นดิจิทัล
Aspose.Email API ให้ความสามารถในการรับหรือกำหนดค่าที่บ่งชี้ว่าข้อความที่เซ็นชัดจะถูกถอดรหัสหรือไม่.
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "signed.msg");
// The attachments of a clear-signed message are wrapped in the signature,
// so remove it first and read the attachments from the resulting message
if (msg.isSigned()) {
SmimeResult result = new SecureEmailManager().checkSignature(msg);
System.out.println("Signature valid: " + result.isSuccess());
msg = msg.removeSignature();
}
for (MapiAttachment attachment : msg.getAttachments()) {
System.out.println(attachment.getLongFileName());
attachment.save(dataDir + attachment.getLongFileName());
}
เปลี่ยนชื่อไฟล์แนบใน MapiMessage
Aspose.Email ทำให้สามารถแก้ไข ชื่อที่แสดง ค่าคุณสมบัติใน ไฟล์แนบของ MapiMessage.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีอัปเดตชื่อแสดงของไฟล์แนบแรกและที่สองภายใน Mapi message ที่โหลดมา:
MapiMessage msg = MapiMessage.load(fileName);
msg.getAttachments().get_Item(0).setDisplayName("New display name 1");
msg.getAttachments().get_Item(1).setDisplayName("New display name 2");
ตรวจสอบว่าไฟล์แนบเป็น Inline หรือปกติ
ความแตกต่างระหว่างไฟล์แนบแบบ inline และปกติคือวิธีการแสดงภายในอีเมล ไฟล์แนบแบบ inline ฝังอยู่ในเนื้อหาอีเมลและสามารถดูได้โดยไม่ต้องเปิดไฟล์แยกหรือดาวน์โหลด ส่วนไฟล์แนบปกติเป็นไฟล์แยกที่แนบกับอีเมลแต่ไม่แสดงโดยตรงในเนื้อหาและต้องดาวน์โหลดและเปิดภายนอก. The MapiAttachment.IsInline คุณสมบัติของ MapiAttachment คลาสรับค่าที่บ่งชี้ว่าไฟล์แนบเป็น inline หรือปกติ.
ตัวอย่างโค้ดต่อไปนี้โหลดข้อความอีเมลจากไฟล์และดึงข้อมูลเกี่ยวกับไฟล์แนบ โดยเฉพาะการพิมพ์ชื่อแสดงของแต่ละไฟล์แนบและว่ามันเป็น inline ภายในข้อความหรือไม่:
MapiMessage message = MapiMessage.load("fileName");
for (MapiAttachment attach : message.getAttachments()) {
System.out.println(attach.getDisplayName() + ": " + attach.isInline());
}
ดึงอ็อบเจ็กต์ OLE ฝังจาก oledata.mso
บางครั้งข้อมูล OLE ฝังจะแสดงเป็น oledata.mso ไฟล์แนบใน MapiAttachment และต้องดึงออกด้วยตนเอง สิ่งเหล่านี้ oledata.mso ไฟล์อยู่ในรูปแบบ Microsoft Compound Document File (MCDF) และการสนับสนุนไฟล์เหล่านี้อยู่นอกขอบเขตของ Aspose.Email อย่างไรก็ตาม Aspose.Email มี InlineAttachmentExtractor คลาสเพื่อแยกรายการแพ็กเกจ MSO ที่อยู่ในข้อมูลไบนารีของ oledata.msoซึ่งสามารถส่งต่อให้กับไลบรารีการอ่านไฟล์คอมพาวนด์ (เช่น Apache POI POIFS) สำหรับการสกัดเนื้อหา
เมื่อประเภทเนื้อความข้อความเป็น HTML (ไม่ใช่ RTF) และมีอ็อบเจ็กต์ OLE ในข้อความ, ส่วน MapiPropertyTag.PR_ATTACH_DATA_OBJ คุณสมบัติเขาสาถูกขาด ในกรณีนี้ข้อมูลเกี่ยวกับอ็อบเจ็กต์ OLE ถูกเก็บไว้ใน oledata.mso.
โค้ดด้านล่างแสดงการวนลูปแพ็กเกจ MSO และเขียนแต่ละแพ็กเกจลงไฟล์แยกไฟล์ พร้อมเปิดด้วยตัวอ่านไฟล์คอมพาวนด์:
MapiMessage msg = MapiMessage.load("double.msg");
for (MapiAttachment mapiAttachment : msg.getAttachments()) {
if (!"oledata.mso".equals(mapiAttachment.getLongFileName())) {
continue;
}
com.aspose.email.system.collections.generic.IGenericDictionary<String, byte[]> oledata =
InlineAttachmentExtractor.enumerateMsoPackage(new ByteArrayInputStream(mapiAttachment.getBinaryData()));
int index = 0;
for (com.aspose.email.system.collections.generic.KeyValuePair<String, byte[]> oleItem : oledata) {
// Each value is a compound file - read its CONTENTS stream with an MCDF library,
// or store it as is for further processing
Files.write(Paths.get(index + ".mso"), oleItem.getValue());
index++;
}
}