Aspose.Email.Outlook を使用したメッセージファイルの管理

MSG を MIME メッセージに変換

Aspose.Email API は、MSG ファイルを MIME メッセージに変換する機能を提供します: toMailMessage メソッド。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = new MapiMessage(
                            "sender@test.com",
                            "recipient1@test.com; recipient2@test.com",
                            "Test Subject",
                            "This is a body of message.");
MailConversionOptions options = new MailConversionOptions();
options.setConvertAsTnef(true);
MailMessage mail = msg.toMailMessage(options);

RTF 本文を保持したまま MSG を EML に変換

API は、MSG を EML に変換する際に RTF 本文を保持するための以下のメソッドを提供します:

以下のコードサンプルは、MailMessage の rtf 本文を保持する方法を示しています:

MsgLoadOptions options = new MsgLoadOptions();
options.setPreserveRtfContent(true);
MailMessage message = MailMessage.load("fileName", options);
MapiMessage mapi = MapiMessage.load("fileName");
MailConversionOptions options = new MailConversionOptions();
options.setPreserveRtfContent(true);
MailMessage message = mapi.toMailMessage(options);

カテゴリヘッダーを保持したまま MSG を MHTML に変換

Aspose.Email API は、メッセージを MHTML に変換する際にカテゴリヘッダーを追加する機能を提供します。この機能は次によって指定されます: MhtSaveOptions MailMessage を MHTML 形式で保存するときの追加オプションとしてクラスを使用します。

以下のコードサンプルは、MapiMessage オブジェクトから MHT(MHTML)ファイルを作成し、MhtSaveOptions を使用して MHT ファイルの書式とヘッダーをカスタマイズし、メールメッセージにカテゴリを設定し、保存前にフォーマットテンプレートとレンダリングヘッダーを変更する方法を示しています。

 MapiMessage msg = new MapiMessage("from@aaa.com", "to@aaa.com", "subj", "body");

msg.setCategories(new String[] { "Urgently", "Important" });

MhtSaveOptions saveOptions = new MhtSaveOptions();

saveOptions.getFormatTemplates().set_Item(MhtTemplateName.CATEGORIES,

    saveOptions.getFormatTemplates().get_Item(MhtTemplateName.CATEGORIES).replace("Categories", "Les catégories"));

saveOptions.getRenderingHeaders().add(MhtTemplateName.CATEGORIES);

msg.save("fileName.mhtml", saveOptions);

Outlook テンプレートファイル(.OFT)の読み書き

Outlook テンプレートは、同様のメールメッセージを何度も送信したい場合に非常に便利です。毎回最初からメッセージを作成する代わりに、まず Outlook でメッセージを作成し、Outlook テンプレート(OFT)として保存します。その後、メッセージを送信する必要があるたびにテンプレートから作成でき、本文や件名の同じテキストを書いたり、書式設定を行ったりする時間を節約できます。Aspose.Email MailMessage クラスは Outlook テンプレート(OFT)ファイルをロードして読み取るために使用できます。Outlook テンプレートが次のインスタンスにロードされたら MailMessage クラスを使用すると、送信者、受信者、本文、件名、その他のプロパティを更新できます。プロパティを更新した後:

  • 次を使用してメールを送信します: SmtpClient クラスまたは
  • メッセージを MSG として保存し、Microsoft Outlook を使用してさらに更新/検証を行います。

以下のコードサンプルでは、次を行います:

  1. テンプレートを次を使用してロードします: MailMessage クラス。
  2. いくつかのプロパティを更新します。
  3. メッセージを MSG 形式で保存します。

以下のコードスニペットは、OFT ファイルを読み込み、メッセージを更新し、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/";

// Load the Outlook template (OFT) file in MailMessage's instance
MailMessage message = MailMessage.load(dataDir + "sample.oft", new MsgLoadOptions());

// Set the sender and recipients information
String senderDisplayName = "John";
String senderEmailAddress = "john@abc.com";
String recipientDisplayName = "William";
String recipientEmailAddress = "william@xzy.com";

message.setSender(new MailAddress(senderEmailAddress, senderDisplayName));
message.getTo().addMailAddress(new MailAddress(recipientEmailAddress, recipientDisplayName));
message.setHtmlBody(message.getHtmlBody().replace("DisplayName", "<b>" + recipientDisplayName + "</b>"));

// Set the name, location and time in email body
String meetingLocation = "<u>" + "Hall 1, Convention Center, New York, USA" + "</u>";
String meetingTime = "<u>" + "Monday, June 28, 2010" + "</u>";
message.setHtmlBody(message.getHtmlBody().replace("MeetingPlace", meetingLocation));
message.setHtmlBody(message.getHtmlBody().replace("MeetingTime", meetingTime));

// Save the message in MSG format and open in Office Outlook
MapiMessage mapimessage = MapiMessage.fromMailMessage(message);
mapimessage.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapimessage.save(dataDir + "ReadAndWritingOutlookTemplateFile_out.msg");

Outlook MSG ファイルをテンプレートとして保存

以下のコードスニペットは、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/";

try (MapiMessage mapi = new MapiMessage("test@from.to", "test@to.to", "template subject", "Template body")) {
    mapi.saveAsTemplate(dataDir + "mapiToOft.msg");
}

Outlook MSG ファイルのカラーカテゴリ設定

カラーカテゴリは、メールメッセージに重要度やカテゴリを付与するものです。Microsoft Outlook では、ユーザーがメールを区別するためにカラーカテゴリを割り当てることができます。カラーカテゴリを操作するには、次を使用します: FollowUpManager. それは次のような関数を含みます addCategory, removeCategory, clearCategories および getCategories.

  • addCategory 受け取ります MapiMessage 引数としてカラーカテゴリ文字列(例:"Purple Category" または "Red Category")を受け取ります。
  • removeCategory 受け取ります MapiMessage メッセージから削除するカラーカテゴリ文字列です。
  • clearCategories メッセージからすべてのカラーカテゴリを削除するために使用されます。
  • getCategories 特定のメッセージからすべてのカラーカテゴリを取得するために使用されます。

以下の例は、次のタスクを実行します:

  1. カラーカテゴリを追加します。
  2. 別のカラーカテゴリを追加します。
  3. すべてのカテゴリの一覧を取得します。
  4. すべてのカテゴリを削除します。
// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");

// Add Two category
FollowUpManager.addCategory(msg, "Purple Category");
FollowUpManager.addCategory(msg, "Red Category");

// Retrieve the list of available categories
IList categories = FollowUpManager.getCategories(msg);

// Remove the specified category and then Clear all categories
FollowUpManager.removeCategory(msg, "Red Category");
FollowUpManager.clearCategories(msg);

MSG ファイルからフォローアップ情報にアクセス

Aspose.Email API は、送信または受信したメッセージからフォローアップ情報にアクセスする機能を提供します。メッセージファイルから読了確認、配信読了確認、および投票結果情報を取得できます。

読了確認および配信確認情報の取得

以下のコードスニペットは、読了確認および配信確認情報の取得方法を示しています。

// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");
for (MapiRecipient recipient : msg.getRecipients()) {
    System.out.println("Recipient: " + recipient.getDisplayName());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY property
    System.out.println("Delivery time: " + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY).getDateTime());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_READ property
    System.out.println("Read time" + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_READ).getDateTime());
}

転送メッセージと返信メッセージの作成

Aspose.Email API は、転送メッセージと返信メッセージの作成および書式設定機能を提供します。The ReplyMessageBuilder および ForwardMessageBuilder API のクラスは、それぞれ返信メッセージと転送メッセージの作成に使用されます。Reply または Forward メッセージは、以下のモードのいずれかを使用して作成できるよう指定できます。 OriginalMessageAdditionMode 列挙型です。この列挙型には次の値があります:

  • OriginalMessageAdditionMode.None - 元のメッセージは応答メッセージに含まれません。
  • OriginalMessageAdditionMode.Attachment - 元のメッセージが応答メッセージに添付ファイルとして含まれます
  • OriginalMessageAdditionMode.Textpart - 元のメッセージが応答メッセージの本文にテキストとして含まれます

返信メッセージの作成

以下のコードスニペットは、返信メッセージの作成方法を示します。

// 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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ReplyMessageBuilder builder = new ReplyMessageBuilder();

// Set ReplyMessageBuilder Properties
builder.setReplyAll(true);
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
builder.setResponseText(
        "<p><b>Dear Friend,</b></p> I want to do is introduce my co-author and co-teacher. <p><a href=\"www.google.com\">This is a first link</a></p><p><a href=\"www.google.com\">This is a second link</a></p>");

MapiMessage replyMsg = builder.buildResponse(originalMsg);
replyMsg.save(dataDir + "reply_out.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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ForwardMessageBuilder builder = new ForwardMessageBuilder();
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
MapiMessage forwardMsg = builder.buildResponse(originalMsg);
forwardMsg.save(dataDir + "forward_out.msg");

メッセージ変換時に空の日付を保持

MapiConversionOptions.setPreserveEmptyDates(boolean) メッセージ変換時に空の日付を保持する必要があるかどうかを示すプロパティです。この API は Aspose.Email 21.5 で利用可能です。以下のコードスニペットは、空の日付を保持する方法を示します。

MailMessage mailMessage = MailMessage.load("message.eml");
System.out.println(mailMessage.getDate()); // zero date
MapiConversionOptions mco = MapiConversionOptions.getUnicodeFormat();
// keep empty dates when converting a message
mco.setPreserveEmptyDates(true);
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage, mco);
System.out.println(mapiMessage.getClientSubmitTime()); // zero date
// check zero date
if (mapiMessage.getClientSubmitTime().equals(JavaHelper.ZERO_DATE))
    System.out.println("ZERO DATE");