MSG ファイルの作成と保存

Aspose.Email は Outlook メッセージ (MSG) ファイルの作成をサポートします。本記事では以下の方法を説明します:

  • MSG メッセージを作成する。
  • 添付ファイル付きの MSG メッセージを作成する。
  • RTF 本文付きの MSG メッセージを作成する。
  • メッセージを下書きとして保存する。
  • 本文の圧縮を扱う。

Outlook メッセージの作成と保存

この MailMessage クラスは…を持っています 保存 Outlook MSG ファイルをディスクまたはストリームに保存できるメソッドです。以下のコードスニペットは、… のインスタンスを作成します。 MailMessage クラスで、From、To、Subject、Body などのプロパティを設定します。 保存 メソッドはファイル名を引数として受け取ります。さらに、Outlook メッセージは … で作成できます。 圧縮 RTF 本文 使用して MapiConversionOptions.

  1. 新しいインスタンスを作成します MailMessage クラスで From、To、Subject、Body プロパティを設定します。
  2. 呼び出す MapiMessage クラス fromMailMessage オブジェクトを受け取るメソッド MailMessage タイプです。 fromMailMessage メソッドは…を変換します MailMessage へ MapiMessage (MSG)。
  3. 呼び出す MapiMessage.save MSG ファイルを保存するメソッド。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");

// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);

// Save the message (MSG) file
String strMsgFile = "CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.save(dataDir + strMsgFile);

添付ファイル付き MSG ファイルの作成

上記の例では、シンプルな MSG ファイルを作成しました。Aspose.Email は添付ファイル付きのメッセージファイルの保存もサポートしています。必要なのは添付ファイルを対象に追加することだけです。 MailMessage インスタンス。addItem メソッドを呼び出すことで添付ファイルを追加します。 MailMessage.Attachments コレクション。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

String[] files = new String[2];
files[0] = "attachment.doc";
files[1] = "attachment.png";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");

// Add the attachments
for (String strFileName : files)
{
    mailMsg.getAttachments().addItem(new Attachment(strFileName));
}

// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
String strMsgFile = "CreateMessagesWithAttachments.msg";
outlookMsg.save(dataDir + strMsgFile);

RTF 本文付き MSG ファイルの作成

Aspose.Email では、リッチテキスト (RTF) 本文を持つ Outlook メッセージ (MSG) ファイルも作成できます。RTF 本文はテキストの書式設定をサポートしています。次のように設定して作成します。 MailMessage.HtmlBody プロパティ。変換する際は MailMessage インスタンスを MapiMessage インスタンスでは、HTML 本文が RTF に変換されます。この方法でメール本文の書式が保持されます。

以下の例は RTF 本文を持つ MSG ファイルを作成します。HTML 本文には見出しが1つ、太字と下線の書式が適用されています。この書式は HTML が RTF に変換されても保持されます。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setHtmlBody("<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");

MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
outlookMsg.save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");

下書きステータスでメッセージを保存

メールは編集中で後で続きを編集したい場合に下書きとして保存されます。Aspose.Email はメッセージフラグを設定することでメールを下書き状態で保存することをサポートしています。以下は Outlook のメールメッセージ (MSG) を下書きとして保存するサンプルコードです。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Change properties of an existing MSG file
String strExistingMsg = "message.msg";

// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.setSubject(msg.getSubject() + " NEW SUBJECT (updated by Aspose.Email)");
msg.setHtmlBody(msg.getHtmlBody() + " NEW BODY (udpated by Aspose.Email)");

// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.fromMailMessage(msg);
mapiMsg.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.save(dataDir + "SavingMessageInDraftStatus_out.msg");

本文圧縮の影響

RTF 本文の圧縮方法を使用すると、サイズの小さい MSG を生成できますが、作成速度が遅くなります。速度を向上させてメッセージを作成するには、フラグを false に設定してください。このフラグは作成された PST にも影響し、MSG ファイルが小さいほど PST も小さくなり、MSG ファイルが大きいほど PST の作成が遅くなります。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";

MailMessage message = MailMessage.load(fileName);
MapiConversionOptions options = new MapiConversionOptions();
options.setUseBodyCompression(true);
MapiMessage ae_mapi = MapiMessage.fromMailMessage(message, options);