javaを使ってoutlookメールから添付ファイルを保存したいのですが、調べても情報が少なく苦戦しています。
outlookのメール情報が取得できて、その中に添付ファイルの情報が取得できているようなのですが、
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipfile));
を使うとFileNotFoundExceptionが発生するため、
Exchange Web Services APIを使って実現しなくてはいけないようなのですが、その情報が見つかりません。
参考情報やサンプルソースなどがありましたらご教授いただけると助かります。
(以下のソースに「添付ファイル」の情報を指定するのかと思うのですが…。)
public void savetmpFile(ExchangeService service, Item item) throws Exception {
// AttachmentCollection tmpData = item.getAttachments();
// List<Attachment> tmpFiles = tmpData.getItems();
// とりあえず上記で添付ファイルのデータが取得できているようだが…。
// 添付ファイルを保存したい EmailMessage message = EmailMessage.bind(service, item.getId()); // message.getAttachments()の中には添付ファイルの情報があるが…。 message.load(new PropertySet(ItemSchema.MimeContent)); MimeContent mc = message.getMimeContent(); FileOutputStream fos = new FileOutputStream("保存先のパス"); fos.write(mc.getContent(), 0, mc.getContent().length); fos.close();
}
~追記~
バイト列を取得し保存する方法を模索
【ソース】
public void savetmpFile(ExchangeService service, Item item) throws Exception {
Stream<Attachment> fis = item.getAttachments().getItems().stream();
byte[] bytes = fis.toString().getBytes();
FileOutputStream fos = new FileOutputStream(saveFilePath);
for (byte b : bytes) {
fos.write(b);
}
fos.flush();
fos.close();
}
【実行結果(zipファイル)】
zipファイルの中身が空です。(解凍不能)
【実行結果(テキストファイル)】
java.util.stream.ReferencePipeline$Head@32193bea
また、上記ソースのbytesを以下に変更して実行
byte[] bytes = item.getAttachments().getItems().toString().getBytes();
【実行結果(zipファイル)】
zipファイルの中身が空です。(解凍不能)
【実行結果(テキストファイル)】
[microsoft.exchange.webservices.data.property.complex.FileAttachment@1bb5a082]
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/22 11:24
2019/07/24 00:59
2019/07/24 03:00 編集
2019/07/24 04:26
2019/07/24 04:49
2019/07/24 06:42
2019/07/24 09:12
2019/07/24 09:56
2019/07/24 11:38
2019/07/25 12:05