メールのロード、保存、変換
メールファイル形式の検出
Aspose.Email API は、提供されたメッセージファイルのファイル形式を検出する機能を提供します。 DetectFileFormat メソッド FileFormatUtil この目的にはクラスを使用できます。以下のクラスとメソッドを使用して、ロードされたファイル形式を検出できます。
- 列挙型 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 他の形式でメッセージを保存する際の追加設定に使用できます。この記事では、これらのクラスを使用してサンプルメールを次のように保存する方法を示します:
- EML 形式。
- Outlook MSG。
- MHTML 形式。
- HTML 形式。
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 ファイルをロードし、元の境界を保持したまま同じ形式で保存する方法を示しています。
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)
MIME 境界文字列のカスタマイズ
Aspose.Email for .NET は、MIME メッセージで使用される境界テンプレートを構成できるようにします。これは以下を通じて実現します: boundaries_template プロパティ( EmlSaveOptions クラス。
カスタムテンプレートを設定することで、MIME 境界が生成される方法を定義できます。テンプレート内の {#} ワイルドカードは増分の境界番号に置き換えられ、動的で読みやすい境界文字列が生成されます。
もし boundaries_template が設定されていない場合(デフォルトは None)、ライブラリは内部の境界形式を使用します。
次のコードサンプルは、メールメッセージを .eml 形式で保存する際に MIME 境界文字列をカスタマイズする方法を示しています:
import aspose.email as ae
# Create a sample MailMessage
message = ae.MailMessage()
message.subject = "Custom MIME Boundary"
message.body = "This message uses a custom MIME boundary template."
message.to.append(ae.MailAddress("recipient@example.com"))
# Configure save options with a custom boundary template
save_options = ae.EmlSaveOptions(ae.MailMessageSaveType.EML_FORMAT)
save_options.boundaries_template = "boundary--{#}"
# Save the message to .eml with custom boundaries
message.save("Custom_Boundary_Message.eml", save_options)
以下は、上記のコードで保存されたカスタム境界線を使用した メッセージ構造の例 です:
From: sender@example.com
To: recipient@example.com
Subject: Custom_Boundary_Message
Date: Fri, 27 Dec 2024 12:00:00 +0000
Content-Type: multipart/mixed;
boundary="boundary--1"
This is a multi-part message in MIME format.
--boundary--1
Content-Type: multipart/alternative; boundary="boundary--2"
--boundary--2
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"
This is the plain text content of the email.
--boundary--2
Content-Type: text/html; charset="windows-1251"
Content-Transfer-Encoding: quoted-printable
<html>
<body>
<p>This is the <b>HTML</b> content of the email.</p>
</body>
</html>
--boundary--2--
--boundary--1
Content-Type: application/octet-stream; name="report-2023-08.xlsx"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="report-2023-08.xlsx"
UEsDBBQABgAIAAAAIQBi7...
--boundary--1--
EML ファイルで TNEF 添付ファイルを保持
次のコードスニペットは、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)
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)として保存できます。次のコードスニペットは、EML ファイルを 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)
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 class は以下を提供します TimeZoneOffset MHT にエクスポートする際にカスタマイズされたタイムゾーンを設定するプロパティです。次のコードスニペットは、カスタムタイムゾーンでメールを 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)
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)