Aspose.Email と Apache POI HSMF でメッセージ添付ファイルを抽出

Aspose.Email - メッセージ添付ファイルの抽出

既存メッセージから添付ファイルを保存するには:

  1. MailMessage クラスのインスタンスを作成します。
  2. MailMessage クラスの load() メソッドを使用し、正しい MessageFormat を指定して既存のメールメッセージをロードします。
  3. AttachmentCollection クラスのインスタンスを作成し、MailMessage インスタンスから getAttachments() メソッドで取得した添付ファイルで設定します。
  4. AttachmentCollection コレクションを反復処理します。
  5. Attachment クラスのインスタンスを作成し、AttachmentCollection の get() メソッドで取得したインデックス値で設定します。
  6. Attachment クラスの save() メソッドを使用して、添付ファイルをディスクに保存します。

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();

				}

			}

		}

	}

}

実行コードをダウンロード

サンプルコードをダウンロード