Extrahovat přílohy zprávy pomocí Aspose.Email a Apache POI HSMF

Aspose.Email - Extrahovat přílohy zprávy

Pro uložení příloh z existujících zpráv:

  1. Vytvořte instanci třídy MailMessage.
  2. Načtěte existující e‑mailovou zprávu pomocí metody load() třídy MailMessage a uveďte správný MessageFormat.
  3. Vytvořte instanci třídy AttachmentCollection a naplňte ji přílohami z instance MailMessage pomocí metody getAttachments().
  4. Procházejte kolekci AttachmentCollection.
  5. Vytvořte instanci třídy Attachment a naplňte ji indexovanou hodnotou z AttachmentCollection pomocí metody get().
  6. Uložte přílohu na disk pomocí metody save() třídy 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 - Extrahovat přílohy zprávy

Třída AttachmentChunks může být použita k přístupu k přílohám 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();

				}

			}

		}

	}

}

Stáhnout běžící kód

Stáhnout ukázkový kód