ดึงไฟล์แนบของข้อความโดยใช้ Aspose.Email และ Apache POI HSMF
Contents
[
Hide
]
Aspose.Email - ดึงไฟล์แนบของข้อความ
เพื่อบันทึกไฟล์แนบจากข้อความที่มีอยู่:
- สร้างอินสแตนซ์ของคลาส MailMessage.
- โหลดข้อความอีเมลที่มีอยู่โดยใช้เมธอด load() ของคลาส MailMessage โดยระบุ MessageFormat ที่ถูกต้อง
- สร้างอินสแตนซ์ของคลาส AttachmentCollection และเติมด้วยไฟล์แนบจากอินสแตนซ์ MaiMessage โดยใช้เมธอด getAttachments()
- วนซ้ำผ่านคอลเลกชัน AttachmentCollection
- สร้างอินสแตนซ์ของคลาส Attachment และเติมค่าด้วยค่าในตำแหน่งจาก AttachmentCollection โดยใช้เมธอด get()
- บันทึกไฟล์แนบลงดิสก์โดยใช้เมธอด save() ของคลาส Attachment
Java
MailMessage message = MailMessage.load(dataDir + \"message.msg\");
System.out.println(\"Extracting attachments....\");
for (int i = 0; i < message.getAttachments().size(); i++)
{
Attachment att = (Attachment) message.getAttachments().get_Item(i);
System.out.println("Attachment Name: " + att.getName());
String attFileName = att.getName().replace(".eml", "").replace(":", " ").replace("\\", " ").replace("/", " ").replace("?", "");
// Save the attachment to disk
att.save(dataDir + attFileName);
}Apache POI HSMF - ดึงไฟล์แนบของข้อความ
คลาส AttachmentChunks สามารถใช้เพื่อเข้าถึงไฟล์แนบของ MAPIMessage
Java
MAPIMessage msg = new MAPIMessage(dataDir + \"message.msg\");
AttachmentChunks[] attachments = msg.getAttachmentFiles();
if (attachments.length > 0)
{
File d = new File(dataDir + "attachments");
if (d.exists() || d.mkdir())
{
for (AttachmentChunks attachment : attachments)
{
String fileName = attachment.attachFileName.toString();
if (attachment.attachLongFileName != null)
{
fileName = attachment.attachLongFileName.toString();
}
File f = new File(d, fileName);
OutputStream fileOut = null;
try
{
fileOut = new FileOutputStream(f);
fileOut.write(attachment.attachData.getValue());
}
finally
{
if (fileOut != null)
{
fileOut.close();
}
}
}
}
}ดาวน์โหลดโค้ดที่ทำงาน
ดาวน์โหลดโค้ดตัวอย่าง
สำหรับรายละเอียดเพิ่มเติม, ไปที่ จัดการไฟล์แนบในข้อความอีเมล.