ดึงไฟล์แนบของข้อความโดยใช้ Aspose.Email และ Apache POI HSMF

Aspose.Email - ดึงไฟล์แนบของข้อความ

เพื่อบันทึกไฟล์แนบจากข้อความที่มีอยู่:

  1. สร้างอินสแตนซ์ของคลาส MailMessage.
  2. โหลดข้อความอีเมลที่มีอยู่โดยใช้เมธอด load() ของคลาส MailMessage โดยระบุ MessageFormat ที่ถูกต้อง
  3. สร้างอินสแตนซ์ของคลาส AttachmentCollection และเติมด้วยไฟล์แนบจากอินสแตนซ์ MaiMessage โดยใช้เมธอด getAttachments()
  4. วนซ้ำผ่านคอลเลกชัน AttachmentCollection
  5. สร้างอินสแตนซ์ของคลาส Attachment และเติมค่าด้วยค่าในตำแหน่งจาก AttachmentCollection โดยใช้เมธอด get()
  6. บันทึกไฟล์แนบลงดิสก์โดยใช้เมธอด 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();

				}

			}

		}

	}

}

ดาวน์โหลดโค้ดที่ทำงาน

ดาวน์โหลดโค้ดตัวอย่าง