Wyodrębnij załączniki wiadomości przy użyciu Aspose.Email i Apache POI HSMF

Aspose.Email – wyodrębnianie załączników wiadomości

Aby zapisać załączniki z istniejących wiadomości:

  1. Utwórz instancję klasy MailMessage.
  2. Załaduj istniejącą wiadomość e‑mail używając metody load() klasy MailMessage, podając prawidłowy MessageFormat.
  3. Utwórz instancję klasy AttachmentCollection i wypełnij ją załącznikami z obiektu MailMessage przy użyciu metody getAttachments().
  4. Iteruj po kolekcji AttachmentCollection.
  5. Utwórz instancję klasy Attachment i wypełnij ją wartością indeksowaną z kolekcji AttachmentCollection przy użyciu metody get().
  6. Zapisz załącznik na dysk używając metody save() klasy 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 – wyodrębnianie załączników wiadomości

Klasa AttachmentChunks może być użyta do dostępu do załączników 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();

				}

			}

		}

	}

}

Pobierz działający kod

Pobierz przykładowy kod