חילוץ קבצים מצורפים להודעה באמצעות 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();

				}

			}

		}

	}

}

הורד קוד רץ

הורד קוד לדוגמה