Làm việc với TNEF Attachments
Transport Neutral Encapsulation Format (TNEF) là một định dạng tệp đính kèm email độc quyền được Microsoft Outlook và Exchange Server sử dụng. Các tệp đính kèm này thường chứa dữ liệu tin nhắn phong phú như văn bản định dạng, ảnh nhúng, hoặc yêu cầu họp.
Aspose.Email cho C++ cung cấp hỗ trợ đầy đủ để đọc và bảo tồn các tệp đính kèm TNEF khi tải tin nhắn email. Bạn có thể truy cập và chỉnh sửa nội dung của các tệp đính kèm TNEF, sau đó lưu tin nhắn ở định dạng gốc hoặc định dạng khác trong khi giữ lại toàn bộ dữ liệu nhúng.
Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:
#include <MailMessage.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <LinkedResource.h>
#include <LinkedResourceCollection.h>
#include <EmlLoadOptions.h>
#include <MsgLoadOptions.h>
#include <EmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <SaveOptions.h>
#include <MailMessageSaveType.h>
#include <FileCompatibilityMode.h>
#include <Mime/ContentType.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <Mapi/MailConversionOptions.h>
#include <system/io/file.h>
#include <system/io/path.h>
#include <system/io/memory_stream.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
Đọc Emails Trong Khi Bảo Tồn TNEF Attachments
Khi một tin nhắn Outlook được tải dưới dạng một MailMessage, MsgLoadOptions quyết định cách nội dung đặc thù Outlook được biểu diễn. Với PreserveTnefAttachments đặt thành true, nội dung đó được đóng gói vào một tệp đính kèm TNEF duy nhất, winmail.dat, chính xác như Exchange sẽ gửi tin nhắn; với mặc định false, các tệp đính kèm của tệp MSG trở thành các tệp đính kèm MIME thông thường.
for (bool preserve : { true, false })
{
SharedPtr<MsgLoadOptions> options = MakeObject<MsgLoadOptions>();
options->set_PreserveTnefAttachments(preserve);
SharedPtr<MailMessage> eml = MailMessage::Load(u"MsgWithAtt.msg", options);
Console::WriteLine(u"PreserveTnefAttachments = " + Convert::ToString(preserve) + u":");
for (SharedPtr<Attachment> attachment : eml->get_Attachments())
{
Console::WriteLine(u" " + attachment->get_Name() + (attachment->get_IsTnef() ? u" (TNEF)" : u""));
}
}
Giữ hoặc Giải mã Container TNEF
Một tin nhắn nhận được dưới dạng TNEF mang toàn bộ dữ liệu đặc thù của Outlook trong một winmail.dat phần. Đối với một tin nhắn như vậy, PreserveTnefAttachments thuộc tính của EmlLoadOptions quyết định cách tin nhắn trông như thế nào sau khi phân tích:
true- container được để nguyên và xuất hiện như mộtwinmail.datđính kèm. Sử dụng điều này khi tin nhắn phải được ghi lại dưới cùng một dạng;false(mặc định) - container được giải mã và mọi thứ nó chứa trở thành tệp đính kèm bình thường.
// keep the TNEF container as a single winmail.dat attachment
SharedPtr<EmlLoadOptions> preserve = MakeObject<EmlLoadOptions>();
preserve->set_PreserveTnefAttachments(true);
SharedPtr<MailMessage> kept = MailMessage::Load(u"tnefEml1.eml", preserve);
Console::WriteLine(u"With PreserveTnefAttachments = true");
for (SharedPtr<Attachment> attachment : kept->get_Attachments())
{
Console::WriteLine(u" " + attachment->get_Name());
}
// the default: decode the container and expose its content as regular attachments
SharedPtr<EmlLoadOptions> decode = MakeObject<EmlLoadOptions>();
decode->set_PreserveTnefAttachments(false);
SharedPtr<MailMessage> decoded = MailMessage::Load(u"tnefEml1.eml", decode);
Console::WriteLine(u"With PreserveTnefAttachments = false");
for (SharedPtr<Attachment> attachment : decoded->get_Attachments())
{
Console::WriteLine(u" " + attachment->get_Name());
}
Thuộc tính tương tự có sẵn trên MsgLoadOptions và MhtmlLoadOptions.
Xác định Tin Nhắn và Tệp Đính Kèm TNEF
OriginalIsTnef báo cáo liệu tin nhắn đã tải có phải là TNEF hay không, và Attachment::IsTnef cho biết một tệp đính kèm cụ thể có phải là chính container hay không.
SharedPtr<MailMessage> mail = MailMessage::Load(u"tnefEml1.eml");
// whether the source message was a TNEF one
Console::WriteLine(u"Is the message originally TNEF? " + Convert::ToString(mail->get_OriginalIsTnef()));
// whether a particular attachment is a TNEF container
SharedPtr<EmlLoadOptions> options = MakeObject<EmlLoadOptions>();
options->set_PreserveTnefAttachments(true);
SharedPtr<MailMessage> preserved = MailMessage::Load(u"tnefEml1.eml", options);
for (SharedPtr<Attachment> attachment : preserved->get_Attachments())
{
Console::WriteLine(attachment->get_Name() + u" - IsTnef: " + Convert::ToString(attachment->get_IsTnef()));
}
Đọc và Ghi Tệp Đính Kèm TNEF Trực Tiếp
Một tệp đính kèm MAPI có thể được ghi ra dưới dạng TNEF và đọc lại với SaveToTnef và phương thức tĩnh LoadFromTnef. Cả hai đều hoạt động với tên tệp và với luồng, cho phép tạo ra một winmail.dat tệp hoặc để di chuyển định dạng đặc trưng của Outlook giữa các tin nhắn.
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"MsgWithAtt.msg");
// write an attachment out in TNEF form and read it back
SharedPtr<IO::MemoryStream> stream = MakeObject<IO::MemoryStream>();
msg->get_Attachments()->idx_get(0)->SaveToTnef(stream);
stream->set_Position(0);
SharedPtr<MapiAttachment> fromStream = MapiAttachment::LoadFromTnef(stream);
msg->get_Attachments()->Add(fromStream->get_DisplayName(), fromStream->get_BinaryData());
// the same through a file
msg->get_Attachments()->idx_get(0)->SaveToTnef(u"attachment.dat");
SharedPtr<MapiAttachment> fromFile = MapiAttachment::LoadFromTnef(u"attachment.dat");
Console::WriteLine(u"Restored attachment: " + fromFile->get_DisplayName() +
u", " + Convert::ToString(fromFile->get_BinaryData()->get_Length()) + u" bytes");
Thêm Tệp Đính Kèm vào Tin Nhắn TNEF
Các tệp đính kèm mới được thêm vào một tin nhắn TNEF giống như bất kỳ tin nhắn nào khác. Điều quan trọng là cách nó được lưu: FileCompatibilityMode::PreserveTnefAttachments cho biết EmlSaveOptions để giữ phần TNEF gốc thay vì ghi lại tin nhắn dưới dạng MIME thuần túy.
SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"tnefEml1.eml");
mailMessage->get_Attachments()->Add(MakeObject<Attachment>(IO::File::OpenRead(u"1.jpg"), u"1.jpg", u"image/jpeg"));
// FileCompatibilityMode keeps the TNEF part of the source message intact
SharedPtr<EmlSaveOptions> emlSaveOptions = MakeObject<EmlSaveOptions>(MailMessageSaveType::get_EmlFormat());
emlSaveOptions->set_FileCompatibilityMode(FileCompatibilityMode::PreserveTnefAttachments);
mailMessage->Save(u"TnefWithNewAttachment.eml", emlSaveOptions);
SharedPtr<MailMessage> reloaded = MailMessage::Load(u"TnefWithNewAttachment.eml");
Console::WriteLine(u"Attachments after saving: " + Convert::ToString(reloaded->get_Attachments()->get_Count()));
Thay thế Tài nguyên của Tin Nhắn TNEF
Các hình ảnh của một tin nhắn TNEF - bao gồm cả những hình ảnh của các tin nhắn được nhúng trong nó - có thể được thay thế bằng những hình ảnh khác. Trợ giúp bên dưới duyệt các tệp đính kèm một cách đệ quy: hình ảnh được thay thế trực tiếp, các tin nhắn nhúng được phân tích, xử lý và ghi lại vào tệp đính kèm của chúng.
// Replaces the images of a message, descending into embedded messages
void UpdateResources(SharedPtr<MailMessage> msg, String imgFileName)
{
for (SharedPtr<Attachment> attachment : msg->get_Attachments())
{
String mediaType = attachment->get_ContentType()->get_MediaType();
String extension = IO::Path::GetExtension(attachment->get_Name()).ToLower();
if (mediaType == u"image/png" || extension == u".jpg" || extension == u".png")
{
attachment->set_ContentStream(MakeObject<IO::MemoryStream>(IO::File::ReadAllBytes(imgFileName)));
}
else if (mediaType == u"message/rfc822" || extension == u".msg" || extension == u".eml")
{
SharedPtr<IO::MemoryStream> content = MakeObject<IO::MemoryStream>();
attachment->Save(content);
content->set_Position(0);
SharedPtr<MailMessage> embedded = MailMessage::Load(content);
UpdateResources(embedded, imgFileName);
SharedPtr<IO::MemoryStream> updated = MakeObject<IO::MemoryStream>();
embedded->Save(updated, SaveOptions::get_DefaultMsgUnicode());
updated->set_Position(0);
attachment->set_ContentStream(updated);
}
}
for (SharedPtr<LinkedResource> resource : msg->get_LinkedResources())
{
if (resource->get_ContentType()->get_MediaType() == u"image/png")
{
resource->set_ContentStream(MakeObject<IO::MemoryStream>(IO::File::ReadAllBytes(imgFileName)));
}
}
}
SharedPtr<MailMessage> originalMailMessage = MailMessage::Load(u"tnefEml1.eml");
UpdateResources(originalMailMessage, u"1.jpg");
SharedPtr<EmlSaveOptions> emlSaveOptions = MakeObject<EmlSaveOptions>(MailMessageSaveType::get_EmlFormat());
emlSaveOptions->set_FileCompatibilityMode(FileCompatibilityMode::PreserveTnefAttachments);
originalMailMessage->Save(u"UpdatedTnefResources.eml", emlSaveOptions);
Tạo một TNEF EML từ tệp MSG
Một tin nhắn Outlook có thể chứa bảng, phông chữ và các định dạng RTF khác mà một chuyển đổi MIME thuần túy không thể biểu diễn. Chuyển đổi nó thành một TNEF EML giữ lại định dạng đó: các máy khách thư hiểu TNEF sẽ hiển thị tin nhắn như Outlook. Đặt ConvertAsTnef trên MailConversionOptions, hoặc tải MSG với PreserveTnefAttachments được bật và lưu kết quả dưới dạng EML.
SharedPtr<MapiMessage> mapiMsg = MapiMessage::Load(u"MSG_file_with_RTF_Formatting.msg");
// the RTF formatting of the MSG survives the conversion inside a TNEF part
SharedPtr<MailConversionOptions> conversionOptions = MakeObject<MailConversionOptions>();
conversionOptions->set_ConvertAsTnef(true);
SharedPtr<MailMessage> message = mapiMsg->ToMailMessage(conversionOptions);
message->Save(u"TnefFromMsg.eml", SaveOptions::get_DefaultEml());
// the same result can be reached through the load options
SharedPtr<MsgLoadOptions> loadOptions = MakeObject<MsgLoadOptions>();
loadOptions->set_PreserveTnefAttachments(true);
SharedPtr<MailMessage> eml = MailMessage::Load(u"MSG_file_with_RTF_Formatting.msg", loadOptions);
eml->Save(u"TnefFromMsg2.eml", SaveOptions::get_DefaultEml());
Console::WriteLine(u"Originally TNEF: " + Convert::ToString(MailMessage::Load(u"TnefFromMsg.eml")->get_OriginalIsTnef()));