添付ファイルと埋め込みオブジェクトの操作
メール添付ファイルの管理
メール添付ファイルは、メールメッセージと一緒に送信されるファイルです。ファイルは別個のメッセージとして、または添付先メッセージの一部として送信されることがあります。 Attachment クラスは以下と共に使用されます: MailMessage クラスです。すべてのメッセージは本文を含みます。本文に加え、追加ファイルを送信したい場合があります。これらは添付ファイルとして送信され、以下のインスタンスとして表されます: Attachment クラスです。添付ファイルは任意の数送信できますが、サイズはメールサーバーで制限されます。たとえば Gmail は 10MB を超えるファイルをサポートしていません。
Try it out!
無料でオンラインのメール添付ファイルを追加または削除します Aspose.Email エディタ アプリ.
添付ファイルの追加
メールに添付ファイルを添付するには、以下の手順に従ってください:
- インスタンスを作成します MailMessage クラス。
- インスタンスを作成します Attachment クラス。
- 添付ファイルを次のにロードする Attachment インスタンス。
- 追加する Attachment インスタンスを MailMessage インスタンス。
以下のコードスニペットは、メールに添付ファイルを追加する方法を示しています。
// Create an instance of MailMessage class
MailMessage message = new MailMessage();
message.setFrom(new MailAddress("sender@from.com"));
message.getTo().add("receiver@to.com");
message.setSubject("This is message");
message.setBody("This is body");
// Load an attachment
Attachment attachment = new Attachment("1.txt");
// Add Multiple Attachment in instance of MailMessage class and Save message to disk
message.getAttachments().addItem(attachment);
message.addAttachment(new Attachment("1.jpg"));
message.addAttachment(new Attachment("1.doc"));
message.addAttachment(new Attachment("1.rar"));
message.addAttachment(new Attachment("1.pdf"));
message.save("AddAttachments.eml");
前述のように、Aspose.Email を使用してメールメッセージに添付ファイルを追加する方法を説明しました。以下では、添付ファイルを削除し、画面に情報を表示する方法を示します。
添付ファイルの削除
添付ファイルを削除するには、以下の手順に従ってください:
- インスタンスを作成する Attachment クラス。
- インスタンスに添付ファイルをロードする Attachment クラス。
- インスタンスに添付ファイルを追加する MailMessage クラス。
- インスタンスから添付ファイルを削除する Attachment クラスを使用して MailMessage クラスのインスタンスです。
以下のコードスニペットは、添付ファイルを削除する方法を示しています。
// Create an instance of MailMessage class
MailMessage eml = new MailMessage();
eml.setFrom(new MailAddress("sender@from.com"));
eml.getTo().add("receiver@to.com");
// Load an attachment
Attachment attachment = new Attachment("1.txt");
eml.getAttachments().addItem(attachment);
// Remove attachment from your MailMessage
eml.getAttachments().removeItem(attachment);
添付ファイル名の表示
添付ファイル名を表示するには、以下の手順に従ってください:
- メールメッセージの添付ファイルをループ処理し、
- 各添付ファイルを保存する。
- 各添付ファイル名を画面に表示する。
以下のコードスニペットは、添付ファイル名を画面に表示する方法を示しています。
MailMessage eml = MailMessage.load("Attachments.eml");
for (Attachment attachment : eml.getAttachments()) {
// Display the attachment file name
System.out.println(attachment.getName());
}
メール添付の抽出
このトピックでは、メールファイルから添付ファイルを抽出する方法を説明します。メール添付は、メールメッセージと共に送信されるファイルです。ファイルは別のメッセージとして送信されることも、添付されたメッセージの一部として送信されることもあります。すべてのメールメッセージは追加ファイルを送信するオプションを含んでおり、これらは添付ファイルとして送信され、次のクラスのインスタンスとして表されます。 Attachment クラス。 Attachment クラスは以下と共に使用されます: MailMessage 添付ファイルを操作するクラスです。メールメッセージから添付ファイルを抽出するには、以下の手順に従ってください:
- インスタンスを作成します MailMessage クラス。
- メールファイルを次のにロードする MailMessage インスタンス。
- インスタンスを作成します Attachment クラスを使用してループで全ての添付ファイルを抽出する。
- 添付ファイルを保存し、画面に表示する。
|メールから抽出された添付ファイル| | :- | |
| 以下のコードスニペットは、メール添付を抽出する方法を示します。
MailMessage eml = MailMessage.load("Message.eml", new MsgLoadOptions());
for (Attachment attachment : eml.getAttachments()) {
attachment.save("MessageEmbedded_out.eml");
System.out.println(attachment.getName());
}
添付から Content-Description を取得
Aspose.Email API は、添付ヘッダーから Content-Description を読み取る機能を提供します。以下のコードスニペットは、添付ファイルからコンテンツ記述を取得する方法を示します。
MailMessage eml = MailMessage.load("EmailWithAttachEmbedded.eml");
System.out.println(eml.getAttachments().get_Item(0).getHeaders().get_Item("Content-Description"));
添付が埋め込みメッセージかどうかの判定
以下のコードスニペットは、添付ファイルが埋め込みメッセージかどうかを判定する方法を示しています。
MailMessage eml = MailMessage.load("EmailWithAttachEmbedded.eml");
System.out.println(eml.getAttachments().get_Item(0).isEmbeddedMessage()
? "Attachment is an embedded message."
: "Attachment isn't an embedded message.");
TNEF 形式の添付ファイルを判定
この Attachment.isTnef Aspose.Email Java API のプロパティは、メッセージ添付が TNEF 形式かどうかを示します。
以下のコードスニペットは、添付ファイルが TNEF 形式かどうかを判定する方法を示しています。
MailMessage eml = MailMessage.load(fileName);
for (Attachment attachment : eml.getAttachments()) {
System.out.println("Is Attachment TNEF?: " + attachment.isTnef());
}
TNEF 添付ファイルのロードと保存
Aspose.Email for Java は次のメソッドを提供します MapiAttachment 以下の操作を実行するクラス:
TNEF からロード
-
static MapiAttachment loadFromTnef(String fileName)– TNEF ファイルから添付ファイルを読み込みます。 -
static MapiAttachment loadFromTnef(InputStream stream)– TNEF ストリームから添付ファイルを読み込みます。
TNEF に保存
-
void saveToTnef(String fileName)– 添付ファイルを TNEF ファイルに保存します。 -
void saveToTnef(OutputStream stream)– 添付ファイルを TNEF ストリームに保存します。
以下のコードサンプルは、メッセージから TNEF 添付ファイルを抽出し、ストリームまたはファイルに保存し、そしてそれをメッセージに再度ロードする方法を示しています。 MapiAttachment:
// Load message containing a TNEF attachment (winmail.dat)
MapiMessage msg = MapiMessage.load("message.eml");
// Save the first attachment to a TNEF stream
ByteArrayOutputStream bos = new ByteArrayOutputStream();
msg.getAttachments().get(0).saveToTnef(bos);
// Load attachment back from the TNEF stream
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
MapiAttachment fromtnefAttachment = MapiAttachment.loadFromTnef(bis);
msg.getAttachments().addMapiAttachment(fromtnefAttachment);
// Load TNEF attachment directly from a file
fromtnefAttachment = MapiAttachment.loadFromTnef("winmail.dat");
msg.getAttachments().addMapiAttachment(fromtnefAttachment);
添付が URI 添付の場合の URI の抽出
以下のコードスニペットは、添付ファイルの URI を抽出する方法を示しています。
MailMessage eml = MailMessage.load("fileName");
Attachment attachment = eml.getAttachments().get_Item(0);
if (attachment.isUri()) {
InputStream inputStream = attachment.getContentStream();
String uri = new String(IOUtils.toByteArray(inputStream), Charset.forName("utf-8"));
System.out.println("Attachment URI: " + uri);
}
参照添付の追加
参照添付はローカルファイル添付の代替手段です。場合によっては、アクセスを管理したい場合などに参照添付が好まれることがあります。以下のクラスは、メールメッセージとその添付ファイルを管理・操作するために使用されます。
- ReferenceAttachment - 参照添付ファイルを表します。
- AttachmentPermissionType - Web 参照添付に関連する権限タイプデータ。
- AttachmentProviderType - 添付ファイルを操作するウェブサービスの種類。
以下のコードサンプルは、ファイルからメールメッセージをロードし、特定のプロパティを持つ参照添付ファイルを作成し、メールメッセージに添付する方法を示しています。
MailMessage eml = MailMessage.load("fileName");
ReferenceAttachment refAttach = new ReferenceAttachment("https://[attach_uri]")
refAttach.setName("Document.docx");
refAttach.setProviderType(AttachmentProviderType.OneDrivePro);
refAttach.setPermissionType(AttachmentPermissionType.AnyoneCanEdit);
eml.getAttachments().addItem(refAttach);
埋め込みオブジェクトの操作
埋め込みオブジェクトとは、あるアプリケーションで作成され、別のアプリケーションで作成された文書やファイルに含まれるオブジェクトです。たとえば、Microsoft Excel のスプレッドシートを Microsoft Word のレポートに埋め込んだり、動画ファイルを Microsoft PowerPoint のプレゼンテーションに埋め込んだりできます。ファイルが埋め込まれると、別の文書に挿入したり貼り付けたりするのではなく、元の形式が保持されます。埋め込みドキュメントは元のアプリケーションで開き、編集できます。
メールへのオブジェクト埋め込み
この LinkedResource クラスは以下と共に使用されます: MailMessage メールメッセージにオブジェクトを埋め込むためのクラスです。埋め込みオブジェクトを追加するには、以下の手順に従ってください
- インスタンスを作成します MailMessage クラス。
- From、To、Subject の値を次ので指定する MailMessage インスタンス。
- インスタンスを作成します AlternateView クラス。
- インスタンスを作成します LinkedResource クラス。
- 埋め込みオブジェクトを次のにロードする LinkedResourceCollection.
- ロードされた埋め込みオブジェクトを次のに追加する MailMessage クラスのインスタンスです。
- 追加する AlternateView インスタンスを MailMessage クラスのインスタンスです。
以下のコードスニペットは、プレーンテキストと HTML 本文の両方を持ち、HTML に画像が埋め込まれたメールメッセージを生成します。
|メールに埋め込まれた画像| | :- | |
| 埋め込みオブジェクトは任意の数だけ送信できます。添付ファイルのサイズはメールサーバーによって制限されます。たとえば Gmail は 10MB を超えるファイルをサポートしていません。以下のコードスニペットは、メールにオブジェクトを埋め込む方法を示しています。
// Create an instance of the MailMessage class and Set the addresses and Set the content
MailMessage mail = new MailMessage();
mail.setFrom(new MailAddress("sender@from.com"));
mail.getTo().add("receiver@to.com");
mail.setSubject("This is an email");
// Create the plain text part It is viewable by those clients that don't support HTML
AlternateView plainView = AlternateView.createAlternateViewFromString("This is my plain text content", null, "text/plain");
// Create the HTML part.To embed images, we need to use the prefix 'cid' in the img src value.
// The cid value will map to the Content-Id of a Linked resource.
// Thus <img src='cid:barcode'> will map to a LinkedResource with a ContentId of //'barcode'.
AlternateView htmlView = AlternateView.createAlternateViewFromString("Here is an embedded image.<img src=cid:barcode>", null, "text/html");
// Create the LinkedResource (embedded image) and Add the LinkedResource to the appropriate view
LinkedResource barcode = new LinkedResource("1.jpg", MediaTypeNames.Image.JPEG);
barcode.setContentId("barcode");
mail.getLinkedResources().addItem(barcode);
mail.getAlternateViews().addItem(plainView);
mail.getAlternateViews().addItem(htmlView);
mail.save("EmbeddedImage_out.msg", SaveOptions.getDefaultMsgUnicode());
メールから埋め込みオブジェクトを削除する
LinkedResourceCollection 経由でアクセスできる MailMessage.LinkedResources プロパティです。 LinkedResourceCollection このコレクションは、メールメッセージに追加された埋め込みオブジェクトを完全に削除するメソッドを提供します。オーバーロードされたバージョンを使用してください LinkedResourceCollection.removeAt メールメッセージから埋め込みオブジェクトのすべての痕跡を削除するメソッド。
以下のサンプルコードは、メールメッセージから埋め込みオブジェクトを削除する方法を示しています。
// Load the test message with Linked Resources
MailMessage msg = MailMessage.load("EmlWithLinkedResources.eml");
// Remove a LinkedResource
msg.getLinkedResources().removeAt(0, true);
// Now clear the Alternate View for linked Resources
msg.getAlternateViews().get_Item(0).getLinkedResources().clear(true);
埋め込みオブジェクトの抽出
このトピックでは、メールファイルから埋め込みオブジェクトを抽出する方法を説明します。埋め込みオブジェクトとは、あるアプリケーションで作成され、別のアプリケーションで作成された文書やファイルに含まれるオブジェクトです。たとえば、Microsoft Excel のスプレッドシートを Microsoft Word のレポートに埋め込んだり、動画ファイルを Microsoft PowerPoint のプレゼンテーションに埋め込んだりできます。ファイルが埋め込まれると、別の文書に挿入したり貼り付けたりするのではなく、元の形式が保持されたままになります。埋め込みドキュメントは元のアプリケーションで開いて編集できます。メールメッセージから埋め込みオブジェクトを抽出するには、以下の手順に従ってください:
- インスタンスを作成します MailMessage クラス。
- メールファイルを次のにロードする MailMessage インスタンス。
- ループを作成し、次のインスタンスを生成する Attachment それに含まれるクラス。
- 添付ファイルを保存し、画面に表示する。
- 送信者と受信者のアドレスを次ので指定します MailMessage インスタンス。
- 次のを使用してメールを送信する SmtpClient クラス。
以下のコードスニペットは、メールから埋め込みオブジェクトを抽出します。
|メールから抽出された埋め込みオブジェクト| | :- | |
| 以下のコードスニペットは、埋め込みオブジェクトを抽出する方法を示します。
MailMessage mailMsg = MailMessage.load("Message.msg", new MsgLoadOptions());
for (Attachment attachment : mailMsg.getAttachments()) {
attachment.save("MessageEmbedded_out.msg");
System.out.println(attachment.getName());
}
RTF 形式の MSG から埋め込み添付ファイルを識別・抽出
以下のコードは、RTF 形式のメッセージでインラインまたはアイコンとして表示される添付ファイルを識別・抽出するために使用できます。以下のコードスニペットは、RTF 形式の MSG から埋め込み添付ファイルを識別し抽出する方法を示します。
public static void extractInlineAttachments() {
MapiMessage message = MapiMessage.load("MSG file with RTF Formatting.msg");
for (MapiAttachment attachment : message.getAttachments()) {
if (isAttachmentInline(attachment)) {
try {
saveAttachment(attachment, UUID.randomUUID().toString());
} catch (Exception ex) {
System.err.println(ex);
}
}
}
}
static boolean isAttachmentInline(MapiAttachment attachment) {
for (MapiProperty property : attachment.getObjectData().getProperties().get_Values()) {
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 {
for (MapiProperty property : attachment.getObjectData().getProperties().get_Values()) {
if ("Package".equals(property.getName())) {
try (FileOutputStream fs = new FileOutputStream(fileName)) {
fs.write(property.getData(), 0, property.getData().length);
}
}
}
}
署名付きメールから添付ファイルを取得
署名されたメールは単一の smime.p7m 添付ファイルを含みます。これはメールが SMIME によって暗号化されていることを意味します。smime.p7m ファイル形式はデジタル署名です。このメールの内容を見るには、以下を使用します: RemoveSignature メソッドです。このメソッドは以下を返します: MailMessage デジタル署名のないオブジェクト。
MailMessage signedEml = MailMessage.load("signed.eml");
if (signedEml.isSigned()) {
for (int i = 0; i < signedEml.getAttachments().size(); i++) {
System.out.println("Signed email attachment" + i + ": " + signedEml.getAttachments().get_Item(i).getName());
}
// The email is signed. Remove a signature.
MailMessage eml = signedEml.removeSignature();
System.out.println("Signature removed.");
for (int i = 0; i < eml.getAttachments().size(); i++) {
System.out.println("Email attachment" + i + ": " + eml.getAttachments().get_Item(i).getName());
}
}
Content-Type と Content-Disposition の操作
Aspose.Email API は添付ファイルの操作機能を提供します Content-Type および Content-Disposition 添付ヘッダーから。以下のコードスニペットは、添付ファイルからコンテンツの説明を取得・変更する方法を示しています。
Content-Type と Content-Disposition パラメータの表示
以下のコードスニペットは、画面に Content-Type と Content-Disposition のパラメータを表示する方法を示しています:
void run(MailMessage message) {
// Attachments
for (Attachment attachment : message.getAttachments()) {
ContentDisposition contentDisposition = attachment.getContentDisposition();
printContentDisposition(contentDisposition);
ContentType contentType = attachment.getContentType();
printContentType(contentType);
}
// Linked Resources
for (LinkedResource attachment : message.getLinkedResources()) {
ContentDisposition contentDisposition = attachment.getContentDisposition();
printContentDisposition(contentDisposition);
ContentType contentType = attachment.getContentType();
printContentType(contentType);
}
}
void printContentType(ContentType contentType) {
System.out.println("media-type: " + contentType.getMediaType());
System.out.println("charset: " + contentType.getCharSet());
System.out.println("name: " + contentType.getName());
}
void printContentDisposition(ContentDisposition contentDisposition) {
System.out.println("disposition-type: " + contentDisposition.getDispositionType());
System.out.println("is-inline: " + contentDisposition.getInline());
System.out.println("filename: " + contentDisposition.getFileName());
System.out.println("creation-date: " + contentDisposition.getCreationDate());
System.out.println("modification-date: " + contentDisposition.getModificationDate());
System.out.println("read-date: " + contentDisposition.getReadDate());
System.out.println("size: " + contentDisposition.getSize());
}
添付ファイルで Content-Type と Content-Disposition パラメータを使用する
以下のコードスニペットは、添付ファイルで Content-Type と Content-Disposition パラメータを使用する方法を示しています:
MailMessage eml = MailMessage.load(fileName);
Attachment attachment = new Attachment(pdfFileName, new ContentType("application/octet-stream"));
attachment.getContentDisposition().setDispositionType("attachment");
attachment.getContentDisposition().setFileName(fileName);
eml.addAttachment(attachment);