RTF 形式の MSG から埋め込み添付を特定・抽出する
Contents
[
Hide
]
RTF 形式の本文を持つメールメッセージは、オブジェクト全体として埋め込まれたインライン添付やアイコンとして埋め込まれたインライン添付を含むことがあります。これら二つの添付タイプを区別するために、まず添付の特定のプロパティを調査する必要があります。添付プロパティに基づく一定の条件を満たした後、ObjectData から抽出して添付を保存できます。
この記事では、RTF 形式の MSG ファイルから埋め込み添付を特定し、抽出する方法を説明します。
Java
static void ExtractInlineAttachments()
{
MapiMessage message = MapiMessage.fromFile("Test.msg");
MapiAttachmentCollection attachments = message.getAttachments();
for (Object untypedAttachment : attachments)
{
MapiAttachment attachment = (MapiAttachment) untypedAttachment;
if(IsAttachmentInline(attachment))
{
try
{
SaveAttachment(attachment, UUID.randomUUID().toString());
}
catch (IOException | FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
static boolean IsAttachmentInline(MapiAttachment attachment)
{
MapiObjectProperty objectData = attachment.getObjectData();
if (objectData == null)
return false;
for (Object prop : attachment.getObjectData().getProperties().getValues())
{
MapiProperty property = (MapiProperty)prop;
if ("\u0003ObjInfo".equals(property.getName()))
{
byte[] data = property.getData();
int odtPersist1 = data[1] << 8 | data[0];
return (odtPersist1 & 0x40) == 0;
}
}
return false;
}
static void SaveAttachment(MapiAttachment attachment, String fileName) throws IOException, FileNotFoundException
{
for (Object prop : attachment.getObjectData().getProperties().getValues())
{
MapiProperty property = (MapiProperty)prop;
if ("Package".equals(property.getName()))
{
FileOutputStream fs;
try
{
fs = new FileOutputStream(fileName);
fs.write(property.getData(), 0, property.getData().length);
}
catch (java.io.IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}