E-postaları Yükleme, Kaydetme ve Dönüştürme
E-posta Dosya Formatlarını Tespit Etme
Aspose.Email API, sağlanan ileti dosyasının dosya formatını tespit etme yeteneği sağlar. DetectFileFormat metodu FileFormatUtil Bu sınıf bu amaçla kullanılabilir. Aşağıdaki sınıflar ve yöntemler yüklü dosya formatını tespit etmek için kullanılabilir:
- Enum FileFormatType
- Sınıf FileFormatInfo
- Sınıf FileFormatUtil
- detect_file_format(stream) yöntemi
- detect_file_format(file_path) yöntemi
Aşağıdaki kod parçacığı, dosya formatlarını nasıl tespit edeceğinizi gösterir:
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))
Yükleme Seçenekleriyle Mesaj Yükleme
Aşağıdaki kod parçacığı, bir mesajı yükleme seçenekleriyle nasıl yükleyeceğinizi gösterir.
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)
Yükleme Sırasında Gömülü Mesaj Formatını Korumak
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))
E-postaları Kaydetme ve Dönüştürme
Aspose.Email, herhangi bir mesaj tipini başka bir formata dönüştürmeyi kolaylaştırır. Bu özelliği göstermek için, bu makaledeki kod diskten üç tip mesaj yükleyip bunları diğer formatlarda geri kaydeder. Temel sınıf SaveOptions ve sınıflar EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions diğer formatlara mesaj kaydederken ek ayarlar için kullanılabilir. Makale, bu sınıfları bir örnek e-postayı şu şekilde kaydetmek için nasıl kullanılacağını gösterir:
- EML formatı.
- Outlook MSG.
- MHTML formatı.
- HTML formatı.
EML Olarak Kaydetme
Aşağıdaki kod parçacığı, bir EML mesajını yükleyip aynı formatta diske nasıl kaydedeceğinizi gösterir.
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 Dosyalarında Sınırları Koruma
Aşağıdaki kod parçacığı, bir EML dosyasını yükleyip aynı formatta, özgün sınırları koruyarak nasıl kaydedeceğinizi gösterir.
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 Sınır Dizelerini Özelleştirme
Aspose.Email for .NET, MIME mesajlarında kullanılan sınır şablonunu yapılandırmanıza izin verir. Bu, şunun aracılığıyla yapılır: boundaries_template özellik EmlSaveOptions sınıf.
Özel bir şablon ayarlayarak, MIME sınırlarının nasıl oluşturulacağını tanımlayabilirsiniz. Şablondaki {#} joker karakteri, artan bir sınır numarasıyla değiştirilecek ve dinamik, okunabilir sınır dizeleri sağlanacaktır.
Eğer boundaries_template ayarlanmamışsa (varsayılan None’dur), kütüphane iç sınır biçimini kullanır.
Aşağıdaki kod örneği, bir e-posta mesajını .eml formatında kaydederken MIME sınır dizelerini nasıl özelleştireceğinizi gösterir:
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)
Yukarıdaki kodla kaydedilen özel sınırlarla mesaj yapısının bir örneği aşağıdadır:
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 Dosyalarında TNEF Eklerini Koruma
Aşağıdaki kod parçacığı, bir EML dosyasını TNEF eklerini koruyarak nasıl kaydedeceğinizi gösterir.
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 Olarak Kaydetme
Aşağıdaki kod parçacığı, bir EML mesajını yükleyip uygun seçeneği kullanarak MSG’ye nasıl dönüştüreceğinizi gösterir: 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 Dosyalarında Tarihleri Koruma
Bu MsgSaveOptions class, kaynak mesajı tarihleri koruyarak bir Outlook Message dosyası (MSG) olarak kaydetmenizi sağlar. Aşağıdaki kod parçacığı, bir EML dosyasını MSG olarak nasıl kaydedip tarihleri koruyacağınızı gösterir.
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 Olarak Kaydetme
İstenilen sonuçları elde etmek için farklı MHTML seçenekleri kullanılabilir. Aşağıdaki kod parçacığı, bir EML mesajını şuna yüklemeyi gösterir MailMessage ve bunu MHTML’ye dönüştürün.
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)
Özel MHTML Dönüşüm Ayarları
Bu MhtSaveOptions sınıf, e-posta mesajlarını MHTML formatında kaydetmek için ek seçenekler sağlar. Enumerator MhtFormatOptions çıktı MHTML’ye ek e-posta bilgileri yazılmasını mümkün kılar. Aşağıdaki ek alanlar yazılabilir:
- NONE - Belirli bir ayar belirtilmemiştir.
- WRITE_HEADER - Başlık bilgilerinin yazılacağını gösterir.
- WRITE_OUTLINE_ATTACHMENTS - Outline eklerinin yazılacağını gösterir.
- WRITE_COMPLETE_EMAIL_ADDRESS - Tam e-posta adresinin tüm e-posta başlıklarına yazılacağını gösterir.
- NO_ENCODE_CHARACTERS - Karakterlerin transfer kodlamasının kullanılmayacağını gösterir.
- HIDE_EXTRA_PRINT_HEADER - PageHeader’ın görünmez olacağını gösterir.
- WRITE_COMPLETE_TO_EMAIL_ADDRESS - Tam e-posta adresinin ‘To’ başlığına yazılacağını gösterir.
- WRITE_COMPLETE_FROM_EMAIL_ADDRESS - Tam e-posta adresinin ‘From’ başlığına yazılacağını gösterir.
- WRITE_COMPLETE_CC_EMAIL_ADDRESS - Tam e-posta adresinin ‘Cc’ başlığına yazılacağını gösterir.
- WRITE_COMPLETE_BCC_EMAIL_ADDRESS - Tam e-posta adresinin ‘Bcc’ başlığına yazılacağını gösterir.
- RENDER_CALENDAR_EVENT - Takvim etkinliğinden metnin çıktıda mhtml olarak yazılacağını gösterir.
- SKIP_BYTE_ORDER_MARK_IN_BODY - Byte Order Mark (BOM) baytlarının gövdeye yazılmayacağını gösterir.
- RENDER_V_CARD_INFO - VCard AlternativeView’den metnin çıktıda mhtml olarak yazılacağını gösterir.
- DISPLAY_AS_OUTLOOK - From başlığının Outlook’ta olduğu gibi görüntüleneceğini gösterir.
- RENDER_TASK_FIELDS - Belirli Görev alanlarının çıktıda mhtml olarak yazılacağını gösterir.
Aşağıdaki kod parçacığı, isteğe bağlı ayarlarla bir EML dosyasını MHTML’ye nasıl dönüştüreceğinizi gösterir.
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 Dönüşümüne Takvim Etkinliklerini Dahil Etme
Bu MhtFormatOptions.RenderCalendarEvent Takvim etkinliklerini çıktı MHTML’sine render eder. Aşağıdaki kod parçacığı, MHTML’ye dönüştürürken takvim etkinliklerini nasıl render edeceğinizi gösterir:
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)
Satır İçi Görseller Olmadan E-postayı MHT’ye Dışa Aktarma
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)
Özel Zaman Dilimiyle E-postayı MHT’ye Dışa Aktarma
MailMessage class, şunları sağlar TimeZoneOffset property, MHT’ye dışa aktarırken özel zaman dilimi ayarlamak için kullanılır. Aşağıdaki kod parçacığı, özelleştirilmiş zaman dilimiyle bir e-postayı MHT’ye nasıl dışa aktaracağınızı gösterir:
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 Olarak Kaydetme
Bu HtmlSaveOptions class, mesaj gövdesini gömülü kaynakları kaydetme seçeneği ile HTML’ye dışa aktarmanızı sağlar. Aşağıdaki kod parçacığı, embed_resources varsayılan değerinin true olduğu bir mesajı HTML olarak nasıl kaydedeceğinizi gösterir.
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 Şablonu (.OFT) Olarak Kaydetme
Aşağıdaki kod parçacığı, bir mesajı Outlook şablonu (.oft) dosyası olarak nasıl kaydedeceğinizi gösterir.
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)