Витягти вкладення повідомлення за допомогою Aspose.Email та Apache POI HSMF

Aspose.Email - Витягнення вкладень листа

Щоб зберегти вкладення з існуючих листів:

  1. Створіть екземпляр класу MailMessage.
  2. Завантажте існуючий лист, використовуючи метод load() класу MailMessage, вказавши правильний MessageFormat.
  3. Створіть екземпляр класу AttachmentCollection і заповніть його вкладеннями з об’єкта MailMessage за допомогою методу 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();

				}

			}

		}

	}

}

Завантажити працюючий код

Завантажити зразковий код