メッセージの読み込みと保存
ファイル形式の検出
Aspose.Email API は、提供されたメッセージファイルのファイル形式を検出する機能を提供します。FileFormatUtil クラスの DetectFileFormat メソッドを使用してこれを実現できます。次のクラスとメソッドを使用して、ロードされたファイル形式を検出できます。
- 列挙型 FileFormatType
- クラス FileFormatInfo
- クラス FileFormatUtil
- メソッド detect_file_format(stream)
- メソッド detect_file_format(file_path)
次のコードスニペットは、ファイル形式を検出する方法を示しています。
from aspose.email.tools import FileFormatUtil
# Detect file format and get the detected load format
info = FileFormatUtil.detect_file_format(data_dir + "message.msg")
print("The message format is: " + str(info.file_format_type))
ロードオプションでメッセージをロード
次のコードスニペットは、ロードオプションを使用してメッセージをロードする方法を示しています。
from aspose.email import MailMessage, EmlLoadOptions, HtmlLoadOptions, MhtmlLoadOptions, MsgLoadOptions
# Load Eml, html, mhtml, msg, and dat files
mail_message = MailMessage.load(data_dir + "message.eml", EmlLoadOptions())
MailMessage.load(data_dir + "description.html", HtmlLoadOptions())
MailMessage.load(data_dir + "message.mhtml", MhtmlLoadOptions())
MailMessage.load(data_dir + "message.msg", MsgLoadOptions())
# Loading with custom options
eml_load_options = EmlLoadOptions()
eml_load_options.preferred_text_encoding = "utf-8"
eml_load_options.preserve_tnef_attachments = True
MailMessage.load(data_dir + "description.html", eml_load_options)
html_load_options = HtmlLoadOptions()
html_load_options.preferred_text_encoding = "utf-8"
html_load_options.should_add_plain_text_view = True
html_load_options.path_to_resources = data_dir
MailMessage.load(data_dir + "description.html", html_load_options)
ロード時に埋め込みメッセージ形式を保持
from aspose.email import MailMessage, EmlLoadOptions, HtmlLoadOptions, MhtmlLoadOptions, MsgLoadOptions
from aspose.email.tools import FileFormatUtil
eml_load_options = EmlLoadOptions()
eml_load_options.preserve_embedded_message_format = True
mail = MailMessage.load(data_dir + "message.eml", eml_load_options)
file_format = FileFormatUtil.detect_file_format(mail.attachments[0].content_stream).file_format_type
print("Embedded message file format: " + str(file_format))
メッセージの保存と変換
Aspose.Email は任意のメッセージタイプを別の形式に変換することを容易にします。この機能を示すために、この記事のコードはディスクから 3 種類のメッセージを読み込み、別の形式で保存します。基底クラスは SaveOptions およびクラス EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions 保存時の追加設定用 MailMessage 他の形式でメッセージを保存するために使用できます。本記事では、これらのクラスを使用してサンプルメールを以下のように保存する方法を示します。
- EML 形式。
- Outlook MSG。
- MHTML 形式。
- HTML 形式。
EML を読み込み、EML として保存
次のコードスニペットは、EML メッセージをロードし、同じ形式でディスクに保存する方法を示しています。
from aspose.email import MailMessage, SaveOptions
# Initialize and Load an existing EML file by specifying the MessageFormat
mail_message = MailMessage.load(data_dir + "message.eml")
mail_message.save(data_dir + "LoadAndSaveFileAsEML_out.eml", SaveOptions.default_eml)
EML を読み込み、元の境界を保持したまま EML として保存
次のコードスニペットは、EML を読み込み、元の境界を保持したまま EML として保存する方法を示しています。
from aspose.email import MailMessage, EmlSaveOptions, MailMessageSaveType
mail_message = MailMessage.load(data_dir + "message.eml")
# Save as eml with preserved original boundaries
eml_save_options = EmlSaveOptions(MailMessageSaveType.eml_format)
mail_message.save(data_dir + "PreserveOriginalBoundaries_out.eml", eml_save_options)
TNEF 添付を保持して EML として保存
次のコードスニペットは、TNEF 添付ファイルを保持したまま EML として保存する方法を示しています。
from aspose.email import MailMessage, EmlSaveOptions, MailMessageSaveType, FileCompatibilityMode
mail_message = MailMessage.load(data_dir + "message.eml")
# Save as eml with preserved attachment
eml_save_options = EmlSaveOptions(MailMessageSaveType.eml_format)
eml_save_options.file_compatibility_mode = FileCompatibilityMode.PRESERVE_TNEF_ATTACHMENTS
mail_message.save(data_dir + "PreserveTNEFAttachment_out.eml", eml_save_options)
EML を読み込み、MSG に保存
以下のコードスニペットは、EML メッセージを読み込み、適切なオプションを使用して MSG に変換する方法を示しています。 SaveOptions.
from aspose.email import MailMessage, SaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
# Save the Email message to disk in ASCII format and Unicode format
eml.save(data_dir + "AnEmail_out.msg", SaveOptions.default_msg_unicode)
日付を保持したまま MSG として保存
この MsgSaveOptions このクラスを使用すると、元のメッセージを日付を保持した Outlook メッセージ ファイル (MSG) として保存できます。以下のコードスニペットは、日付を保持して MSG として保存する方法を示しています。
from aspose.email import MailMessage, MsgSaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
# Save as msg with preserved dates
msg_save_options = MsgSaveOptions(MailMessageSaveType.outlook_message_format_unicode)
msg_save_options.preserve_original_dates = True
eml.save(data_dir + "outTest_out.msg", msg_save_options)
MailMessage を MHTML として保存
MHTML にはさまざまなオプションがあり、目的の結果を得ることができます。次のコードスニペットは、EML メッセージをロードする方法を示していますMailMessage そしてそれを MHTML に変換します。
from aspose.email import MailMessage, SaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
eml.save(data_dir + "AnEmail_out.mhtml", SaveOptions.default_mhtml)
オプション設定で MHTML に変換
この MhtSaveOptions このクラスは、メールメッセージを MHTML 形式で保存するための追加オプションを提供します。列挙体は MhtFormatOptions これにより、追加のメール情報を出力 MHTML に書き込むことが可能になります。以下の追加フィールドが書き込めます:
- NONE - 特別な設定は指定されていません。
- WRITE_HEADER - ヘッダー情報を書き込むことを示します。
- WRITE_OUTLINE_ATTACHMENTS - アウトライン添付ファイルを書き込むことを示します。
- WRITE_COMPLETE_EMAIL_ADDRESS - 完全なメールアドレスをすべてのメールヘッダーに書き込むことを示します。
- NO_ENCODE_CHARACTERS - 文字の転送エンコーディングを使用しないことを示します。
- HIDE_EXTRA_PRINT_HEADER - PageHeader が非表示になることを示します。
- WRITE_COMPLETE_TO_EMAIL_ADDRESS - 完全なメールアドレスを ‘To’ ヘッダーに書き込むことを示します。
- WRITE_COMPLETE_FROM_EMAIL_ADDRESS - 完全なメールアドレスを ‘From’ ヘッダーに書き込むことを示します。
- WRITE_COMPLETE_CC_EMAIL_ADDRESS - 完全なメールアドレスを ‘Cc’ ヘッダーに書き込むことを示します。
- WRITE_COMPLETE_BCC_EMAIL_ADDRESS - 完全なメールアドレスを ‘Bcc’ ヘッダーに書き込むことを示します。
- RENDER_CALENDAR_EVENT - カレンダーイベントのテキストを出力 MHTML に書き込むことを示します。
- SKIP_BYTE_ORDER_MARK_IN_BODY - バイト順マーク(BOM)バイトを本文に書き込まないことを示します。
- RENDER_V_CARD_INFO - VCard AlternativeView のテキストを出力 MHTML に書き込むことを示します。
- DISPLAY_AS_OUTLOOK - From ヘッダーが Outlook と同様に表示されることを示します。
- RENDER_TASK_FIELDS - 特定のタスクフィールドを出力 MHTML に書き込むことを示します。
次のコードスニペットは、オプション設定を使用して eml ファイルを MHTML に変換する方法を示しています。
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MailMessageSaveType, FileCompatibilityMode
# Load an existing EML file
eml = MailMessage.load(data_dir + "message.eml")
# Save as mht with header
mht_save_options = MhtSaveOptions()
# Specify formatting options required
# Here we are specifying to write header information to output without writing extra print header
# and the output headers should be displayed as the original headers in the message
mht_save_options.mht_format_options = MhtFormatOptions.WRITE_HEADER | MhtFormatOptions.HIDE_EXTRA_PRINT_HEADER | MhtFormatOptions.DISPLAY_AS_OUTLOOK
# Check the body encoding for validity.
mht_save_options.check_body_content_encoding = True
eml.save(data_dir + "outMessage_out.mht", mht_save_options)
MHTML に変換しながらカレンダーイベントをレンダリング
この MhtFormatOptions.RenderCalendarEvent カレンダーイベントを出力 MHTML にレンダリングします。次のコードスニペットは、MHTML に変換しながらカレンダーイベントをレンダリングする方法を示しています。
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
file_name = "message.msg"
# Load the MSG file
msg = MailMessage.load(data_dir + file_name)
# Create MHT save options
options = MhtSaveOptions()
options.mht_format_options = MhtFormatOptions.WRITE_HEADER | MhtFormatOptions.RENDER_CALENDAR_EVENT
# Save the message as MHTML
msg.save(data_dir + "Meeting with Recurring Occurrences.mhtml", options)
インライン画像なしで MHT にメールをエクスポート
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
# Load the EML file
eml = MailMessage.load(data_dir + "message.eml")
# Create MHT save options
mht_save_options = MhtSaveOptions()
mht_save_options.skip_inline_images = True
# Save the message as MHTML without inline images
eml.save(data_dir + "EmlToMhtmlWithoutInlineImages_out.mht", mht_save_options)
カスタマイズされたタイムゾーンでメールを MHT にエクスポート
MailMessage このクラスは、MHT にエクスポートする際にカスタマイズされたタイムゾーンを設定できる TimeZoneOffset プロパティを提供します。次のコードスニペットは、カスタマイズされたタイムゾーンでメールを MHT にエクスポートする方法を示しています。
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
from datetime import timedelta
# Load the EML file
eml = MailMessage.load(data_dir + "message.eml")
# Set the local time for message date
eml.time_zone_offset = timedelta(hours=-8)
# The dates will be rendered by the local system time zone
mht_save_options = MhtSaveOptions()
mht_save_options.mht_format_options = MhtFormatOptions.WRITE_HEADER
eml.save(data_dir + "ExportEmailToMHTWithCustomTimezone_out.mhtml", mht_save_options)
メールをEMLにエクスポート
次のコードスニペットは、メールを eml にエクスポートする方法を示しています。
from aspose.email import MailMessage, SaveOptions
# Load the EML file
msg = MailMessage.load(data_dir + "message.eml")
# Save the Email message as EML
msg.save(data_dir + "ExporttoEml_out.eml", SaveOptions.default_eml)
メッセージを HTML として保存
この HtmlSaveOptions このクラスは、埋め込みリソースを保存するオプション付きでメッセージ本文を HTML にエクスポートできます。次のコードスニペットは、embed_resources のデフォルト値が true の状態でメッセージを HTML として保存する方法を示しています。
from aspose.email import MailMessage, SaveOptions, HtmlSaveOptions, HtmlFormatOptions
# Load the EML file
message = MailMessage.load(data_dir + "message.eml")
# Save the Email message as HTML
message.save(data_dir + "SaveAsHTML_out.html", SaveOptions.default_html)
# OR
eml = MailMessage.load(data_dir + "message.eml")
options = HtmlSaveOptions()
options.embed_resources = False
options.html_format_options = (
HtmlFormatOptions.WRITE_HEADER
| HtmlFormatOptions.WRITE_COMPLETE_EMAIL_ADDRESS
) # save the message headers to output HTML using the formatting options
eml.save(data_dir + "SaveAsHTML1_out.html", options)
メッセージを Outlook テンプレート(.oft)ファイルとして保存
次のコードスニペットは、メッセージを Outlook テンプレート(.oft)ファイルとして保存する方法を示しています。
from aspose.email import MailMessage, SaveOptions
eml = MailMessage("test@from.to", "test@to.to", "template subject", "Template body")
oft_eml_file_name = "EmlAsOft_out.oft"
options = SaveOptions.default_msg_unicode
options.save_as_template = True
eml.save(data_dir + oft_eml_file_name, options)