Trích xuất tệp đính kèm tin nhắn bằng Aspose.Email và Apache POI HSMF

Aspose.Email - Trích xuất Tệp đính kèm Thông điệp

Để lưu tệp đính kèm từ các tin nhắn hiện có:

  1. Tạo một thể hiện của lớp MailMessage.
  2. Tải tin nhắn email hiện có bằng phương thức load() của lớp MailMessage, chỉ định MessageFormat đúng.
  3. Tạo một thể hiện của lớp AttachmentCollection và điền các tệp đính kèm từ thể hiện MaiMessage bằng phương thức getAttachments().
  4. Duyệt qua bộ sưu tập AttachmentCollection.
  5. Tạo một thể hiện của lớp Attachment và điền giá trị theo chỉ mục từ AttachmentCollection bằng phương thức get().
  6. Lưu tệp đính kèm vào đĩa bằng phương thức save() của lớp 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 - Trích xuất Tệp đính kèm Thông điệp

Lớp AttachmentChunks có thể được sử dụng để truy cập các tệp đính kèm của 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();

				}

			}

		}

	}

}

Tải mã đang chạy

Tải mã mẫu