การทำงานกับไฟล์แนบและวัตถุฝัง

ทำงานกับไฟล์แนบ

ไฟล์แนบอีเมล คือไฟล์ที่ส่งมาพร้อมกับข้อความอีเมล ไฟล์เหล่านี้อาจเป็นเอกสาร, รูปภาพ, ไฟล์อาจบีบอัด, และอื่น ๆ.

ใน Aspose.Email สำหรับ C++ ไฟล์แนบจะถูกจัดการผ่าน Attachment คลาส ที่ทำงานร่วมกับ MailMessage คลาส คุณสามารถเพิ่ม, ลบ, แยก หรือแสดงไฟล์แนบได้โดยโปรแกรม.

หมายเหตุ: ขนาดไฟล์แนบสูงสุดขึ้นอยู่กับเซิร์ฟเวอร์เมล (เช่น Gmail จำกัดไฟล์แนบที่ 10 MB).

ตัวอย่างในบทความนี้ใช้ส่วนหัวและเนมสเปซต่อไปนี้:

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <ReferenceAttachment.h>
#include <AttachmentProviderType.h>
#include <AttachmentPermissionType.h>
#include <AlternateView.h>
#include <AlternateViewCollection.h>
#include <LinkedResource.h>
#include <LinkedResourceCollection.h>
#include <EmlLoadOptions.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <HtmlSaveOptions.h>
#include <ResourceHtmlRenderingEventArgs.h>
#include <Mime/MediaTypeNames.h>
#include <Mime/ContentType.h>
#include <Mime/HeaderCollection.h>
#include <system/io/file.h>
#include <system/io/directory.h>
#include <system/io/path.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mime;

การเพิ่มไฟล์แนบไปยังอีเมล

เพื่อแนบไฟล์หนึ่งหรือหลายไฟล์ไปยังข้อความอีเมล ให้ทำตามขั้นตอนต่อไปนี้:

  1. สร้างอินสแตนซ์ของ MailMessage คลาส.
  2. สร้าง Attachment ออบเจกต์สำหรับแต่ละไฟล์.
  3. เพิ่มแนบไฟล์ลงในข้อความ - ผ่าน ไฟล์แนบ คอลเลกชันหรือด้วย AddAttachment.
  4. บันทึกข้อความหากต้องการ.
SharedPtr<MailMessage> message = MakeObject<MailMessage>();
message->set_From(u"sender@sender.com");
message->get_To()->Add(u"receiver@gmail.com");
message->set_Subject(u"Files for the review");

// an Attachment is created from a file; its name and content type come from the file
message->get_Attachments()->Add(MakeObject<Attachment>(u"description.html"));
message->AddAttachment(MakeObject<Attachment>(u"1.jpg"));
message->AddAttachment(MakeObject<Attachment>(u"portrait.jpg"));

message->Save(u"WithAttachments.msg", SaveOptions::get_DefaultMsgUnicode());
Console::WriteLine(u"Attachments: " + Convert::ToString(message->get_Attachments()->get_Count()));

การลบไฟล์แนบจากอีเมล

คุณสามารถลบไฟล์แนบจากข้อความได้อย่างง่ายดายโดยใช้ Remove() เมธอดของ ไฟล์แนบ คอลเลกชัน.

โค้ดตัวอย่างต่อไปนี้แสดงวิธีลบไฟล์แนบ:

  1. โหลดหรือสร้าง MailMessage อินสแตนซ์
  2. หาแนบไฟล์ที่จะลบ - เก็บอ็อบเจกต์ที่คุณเพิ่ม, หรือค้นหาในคอลเลกชัน.
  3. ส่งไปยัง Remove(); RemoveAt() ลบแนบไฟล์ที่ตำแหน่งหนึ่งโดยแทนที่.
  4. แสดงแนบไฟล์ที่เหลือเป็นทางเลือก.
SharedPtr<MailMessage> message = MakeObject<MailMessage>();
message->set_From(u"sender@sender.com");
message->get_To()->Add(u"receiver@gmail.com");

SharedPtr<Attachment> description = MakeObject<Attachment>(u"description.html");
message->get_Attachments()->Add(description);
message->AddAttachment(MakeObject<Attachment>(u"1.jpg"));
message->AddAttachment(MakeObject<Attachment>(u"portrait.jpg"));

// remove one attachment and save the message
message->get_Attachments()->Remove(description);
message->Save(u"AttachmentRemoved.msg", SaveOptions::get_DefaultMsgUnicode());

for (SharedPtr<Attachment> attachment : message->get_Attachments())
{
    Console::WriteLine(u"Left: " + attachment->get_Name());
}

แสดงชื่อไฟล์แนบ

นี้ Name ของแนบไฟล์คือชื่อไฟล์ที่ข้อความกำหนดให้. วนลูปผ่านคอลเลกชันจะแสดงทั้งหมด:

SharedPtr<MailMessage> message = MailMessage::Load(u"WithAttachments.msg");

for (SharedPtr<Attachment> attachment : message->get_Attachments())
{
    Console::WriteLine(attachment->get_Name());
}

การแยกไฟล์แนบจากอีเมล

เพื่อแยกและบันทึกไฟล์แนบจากไฟล์อีเมลที่มีอยู่ ทำตามขั้นตอนต่อไปนี้:

  1. โหลดข้อความโดยใช้ MailMessage คลาส.
  2. วนลูปผ่าน ไฟล์แนบ คอลเลกชัน.
  3. บันทึกแนบไฟล์แต่ละไฟล์ลงดิสก์ด้วย Attachment::Save และแสดงชื่อของมันตามต้องการ.

โค้ดส่วนนำไปใช้ต่อไปนี้บันทึกแนบไฟล์ทุกไฟล์ของข้อความลงโฟลเดอร์, แต่ละไฟล์ภายใต้ชื่อของมันเอง:

SharedPtr<MailMessage> message = MailMessage::Load(u"WithAttachments.msg");

IO::Directory::CreateDirectory_(u"ExtractedAttachments");
for (SharedPtr<Attachment> attachment : message->get_Attachments())
{
    attachment->Save(IO::Path::Combine(u"ExtractedAttachments", attachment->get_Name()));
    Console::WriteLine(u"Saved " + attachment->get_Name());
}

|ไฟล์แนบที่ดึงจากอีเมล| | :- | |todo:image_alt_text|

การอ่านคำอธิบายเนื้อหาไฟล์แนบ

แนบไฟล์มีหัว MIME ของตนเอง หัว Content-Description ที่ไม่บังคับเก็บคำอธิบายสั้นของไฟล์; มันอ่านจาก Headers คอลเลกชันของแนบไฟล์. ตัวอย่างเขียนหัวและอ่านกลับจากข้อความที่บันทึก.

SharedPtr<MailMessage> source = MakeObject<MailMessage>(u"sender@sender.com", u"receiver@gmail.com", u"Report", u"See the attachment.");
SharedPtr<Attachment> report = MakeObject<Attachment>(u"description.html");
report->get_Headers()->Add(u"Content-Description", u"Quarterly report");
source->get_Attachments()->Add(report);
source->Save(u"WithDescription.eml", SaveOptions::get_DefaultEml());

SharedPtr<MailMessage> message = MailMessage::Load(u"WithDescription.eml");
String description = message->get_Attachments()->idx_get(0)->get_Headers()->Get(u"Content-Description");
Console::WriteLine(u"Content-Description: " + description);

การทำงานกับวัตถุที่ฝังอยู่

ออบเจกต์ฝัง คือไฟล์ที่สร้างในแอปพลิเคชันหนึ่งและถูกรวมอยู่ในเอกสารหรืออีเมลอื่น ตัวอย่างเช่น แผ่นงาน Microsoft Excel สามารถฝังในเอกสาร Word หรือภาพสามารถฝังโดยตรงในเนื้อหาอีเมล.

เมื่อออบเจกต์ถูกฝัง (ไม่ใช่แค่แนบหรือเชื่อมโยง) มันจะคงรูปแบบเดิมและสามารถเปิดหรือแก้ไขได้ในแอปพลิเคชันต้นฉบับ.

การฝังวัตถุลงในอีเมล

เพื่อฝังรูปภาพหรือออบเจกต์อื่นในข้อความอีเมล ใช้ LinkedResource คลาสพร้อมกับ MailMessage และ AlternateView คลาส ซึ่งทำให้คุณสามารถใส่รูปภาพหรือสื่อแบบฝังในข้อความที่ปรากฏโดยตรงในเนื้อหาข้อความ.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างข้อความอีเมลที่มีทั้งเนื้อหา plain text และ HTML พร้อมภาพฝัง:

  1. สร้างอินสแตนซ์ของ MailMessage คลาส.
  2. กำหนดฟิลด์ผู้ส่ง, ผู้รับ และหัวเรื่อง.
  3. สร้างส่วนเนื้อหา plain-text โดยใช้ AlternateView.
  4. สร้างส่วนเนื้อหา HTML ที่มีการอ้างอิงถึงออบเจกต์ฝังโดยใช้ cid (Content-ID). เขียนอ้างอิงในเครื่องหมายอัญประกาศคู่, src="cid:barcode": ในรูปแบบนี้ โปรแกรมที่โหลดข้อความต่อมาจะจดจำรูปภาพเป็นทรัพยากรอินไลน์ ไม่ใช่แนบไฟล์.
  5. สร้างอินสแตนซ์ของ LinkedResource คลาสและโหลดออบเจกต์.
  6. กำหนด Content-ID ให้กับทรัพยากรที่เชื่อมโยง.
  7. เพิ่มทรัพยากรที่เชื่อมโยงและมุมมองสำรองลงในข้อความ.
  8. บันทึกหรือส่งข้อความอีเมล.
SharedPtr<MailMessage> mail = MakeObject<MailMessage>();
mail->set_From(MakeObject<MailAddress>(u"test001@gmail.com"));
mail->get_To()->Add(u"test001@gmail.com");
mail->set_Subject(u"This is an email");

// the plain text part, for clients that do not display HTML
SharedPtr<AlternateView> plainView = AlternateView::CreateAlternateViewFromString(u"This is my plain text content", nullptr, u"text/plain");

// the HTML part refers to the image by its Content-ID: src="cid:barcode" maps to the resource with the ID "barcode"
SharedPtr<AlternateView> htmlView = AlternateView::CreateAlternateViewFromString(u"Here is an embedded image.<img src=\"cid:barcode\">", nullptr, u"text/html");

SharedPtr<LinkedResource> barcode = MakeObject<LinkedResource>(u"1.jpg", Mime::MediaTypeNames::Image::Jpeg);
barcode->set_ContentId(u"barcode");

mail->get_LinkedResources()->Add(barcode);
mail->get_AlternateViews()->Add(plainView);
mail->get_AlternateViews()->Add(htmlView);

mail->Save(u"EmbeddedImage.eml", SaveOptions::get_DefaultEml());

หมายเหตุ: > คุณสามารถฝังออบเจกต์หลายรายการได้ แต่ขนาดข้อความสูงสุดถูกจำกัดโดยเซิร์ฟเวอร์เมลของคุณ ตัวอย่างเช่น Gmail จำกัดขนาดข้อความที่ 10 MB.

การแยกออบเจกต์ฝังจากอีเมล

คุณสามารถสกัดวัตถุที่ฝัง เช่น รูปภาพ จากข้อความอีเมลที่มีอยู่ พวกมันไม่ใช่แนบ: ข้อความที่โหลดจะแสดงใน LinkedResources คอลเลกชัน. ขั้นตอนโดยรวมคล้ายกับการสกัดแนบไฟล์:

  1. โหลดไฟล์อีเมลเข้าเป็น MailMessage.
  2. วนผ่านทรัพยากรที่ลิงก์.
  3. บันทึกวัตถุที่ฝังแต่ละอันลงดิสก์; Content-ID ของมันบอกว่าเป็นอ้างอิงใดในเนื้อหา HTML.
  4. ตัวเลือก: แสดงชื่อไฟล์บนคอนโซล.
SharedPtr<MailMessage> message = MailMessage::Load(u"EmbeddedImage.eml");

IO::Directory::CreateDirectory_(u"EmbeddedObjects");
int32_t index = 0;
for (SharedPtr<LinkedResource> resource : message->get_LinkedResources())
{
    String fileName = IO::Path::Combine(u"EmbeddedObjects", u"object_" + Convert::ToString(index++) + u".jpg");
    resource->Save(fileName);
    Console::WriteLine(u"cid:" + resource->get_ContentId() + u" -> " + fileName);
}

|วัตถุฝังที่ดึงออกจากอีเมล| | :- | |todo:image_alt_text|

วิธีเพิ่มเติมในการทำงานกับแนบไฟล์

เพิ่มไฟล์แนบอ้างอิง

แนบอ้างอิงไม่ได้เดินทางพร้อมข้อความ: มันมีลิงก์ไปไฟล์ในตำแหน่งแชร์, เพื่อให้ผู้รับเปิดเวอร์ชันล่าสุดและข้อความเองยังคงขนาดเล็ก. ReferenceAttachment รับ URI, ขณะที่ ProviderType ตั้งชื่อบริการเก็บและ PermissionType การเข้าถึงที่ผู้รับได้รับ.

SharedPtr<MailMessage> eml = MailMessage::Load(u"sample.eml");

// the message carries a link to the file, not the file itself
SharedPtr<ReferenceAttachment> refAttach = MakeObject<ReferenceAttachment>(u"https://example.com/shared/Document.docx");
refAttach->set_Name(u"Document.docx");
refAttach->set_ProviderType(AttachmentProviderType::OneDrivePro);
refAttach->set_PermissionType(AttachmentPermissionType::AnyoneCanEdit);

eml->get_Attachments()->Add(refAttach);
eml->Save(u"WithReferenceAttachment.eml", SaveOptions::get_DefaultEml());

// a reference attachment is recognized when the message is read back
SharedPtr<MailMessage> reloaded = MailMessage::Load(u"WithReferenceAttachment.eml");
for (SharedPtr<Attachment> attachment : reloaded->get_Attachments())
{
    Console::WriteLine(attachment->get_Name() + u" - IsUri: " + Convert::ToString(attachment->get_IsUri()));
}

ระบุข้อความที่ฝังและเนื้อหาลายลักษณ์

IsEmbeddedMessage บอกว่าแนบเป็นข้อความหรือไม่; แนบเช่นนั้นสามารถแยกวิเคราะห์ด้วย MailMessage::Load โดยตรงจากสตรีมเนื้อหา. ข้อความที่ลงลายเซ็นเป็นกรณีอื่น: ประกอบด้วยหนึ่งเดียว smime.p7m ส่วน, และ IsSigned ร่วมกับ IsEncrypted รายงานเช่นนั้น.

// an attachment that is itself a message
SharedPtr<MailMessage> eml = MailMessage::Load(u"WithEmbeddedMsg.msg");
for (SharedPtr<Attachment> attachment : eml->get_Attachments())
{
    Console::WriteLine(attachment->get_Name() + u": " +
        (attachment->get_IsEmbeddedMessage() ? u"an embedded message" : u"a regular attachment"));

    if (attachment->get_IsEmbeddedMessage())
    {
        // the attachment content is a complete message and can be parsed as one
        SharedPtr<MailMessage> inner = MailMessage::Load(attachment->get_ContentStream());
        Console::WriteLine(u"   subject of the embedded message: " + inner->get_Subject());
    }
}

// a signed message carries a single smime.p7m part
SharedPtr<MailMessage> signedEml = MailMessage::Load(u"signed.eml");
Console::WriteLine(u"IsSigned: " + Convert::ToString(signedEml->get_IsSigned()) +
    u", IsEncrypted: " + Convert::ToString(signedEml->get_IsEncrypted()));

for (SharedPtr<Attachment> attachment : signedEml->get_Attachments())
{
    Console::WriteLine(u"Signed email attachment: " + attachment->get_Name());
}

รูปภาพในบรรทัด

ภาพอินไลน์ไม่ได้แสดงเป็นแนบ; มันเป็น LinkedResource ซึ่ง ContentId เนื้อหา HTML อ้างอิงผ่าน cid: URL.

เพิ่มภาพอินไลน์ลงในเนื้อหา

ภาพถูกวางในเนื้อหาโดยเพิ่มลงใน LinkedResources ด้วย Content-ID และอ้างอิง ID นั้นจาก HTML - ตัวอย่างเต็มอยู่ใน ฝังวัตถุลงในอีเมล. จำนวนวัตถุที่ฝังได้จำกัดเพียงขนาดของข้อความที่เซิร์ฟเวอร์เมลยอมรับ.

ลบภาพอินไลน์จากเนื้อหา

RemoveAt และ ล้าง ทั้งสองรับแฟล็กที่ยังลบ <img src=cid:...> อ้างอิงจาก HTML, ดังนั้นไม่มีรูปภาพที่เสียหายเหลืออยู่. จำไว้วิวทางเลือกจะเก็บสำเนาเองของทรัพยากร.

SharedPtr<MailMessage> eml = MailMessage::Load(u"EmailWithAttachEmbedded.eml");

Console::WriteLine(u"Linked resources before: " + Convert::ToString(eml->get_LinkedResources()->get_Count()));

// true also removes the <img src=cid:...> reference from the HTML body
eml->get_LinkedResources()->RemoveAt(0, true);

// or drop every inline resource at once
eml->get_LinkedResources()->Clear(true);

// the alternate views hold their own copies of the resources
for (SharedPtr<AlternateView> view : eml->get_AlternateViews())
{
    view->get_LinkedResources()->Clear(true);
}

Console::WriteLine(u"Linked resources after: " + Convert::ToString(eml->get_LinkedResources()->get_Count()));
eml->Save(u"WithoutInlineImages.eml", SaveOptions::get_DefaultEml());

สกัดรูปภาพที่ลิงก์ในเนื้อหา HTML

บางจดหมายข่าวไม่ฝังรูปภาพเลย - เนื้อหา HTML ชี้ไปยังพวกมันด้วย http ลิงก์, และข้อความดูเสียหายเมื่อเชื่อมโยงเหล่านั้นหยุดทำงาน. การตั้งค่า ExtractHTMLBodyResourcesAsAttachments ทำให้ไลบรารีดึงทรัพยากรเหล่านั้นขณะส่งออกข้อความและฝังลงในผลลัพธ์. ตัว ResourceHtmlRendering เหตุการณ์รายงานทรัพยากรทุกอย่างที่ถูกประมวลผล.

SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"LinksSample.eml");

SharedPtr<HtmlSaveOptions> options = MakeObject<HtmlSaveOptions>();

// download the images the HTML body links to and store them as attachments
options->set_ExtractHTMLBodyResourcesAsAttachments(true);

// the event reports every resource that is written
options->ResourceHtmlRendering.connect(std::function<void(SharedPtr<Object>, SharedPtr<ResourceHtmlRenderingEventArgs>)>(
    [](SharedPtr<Object> sender, SharedPtr<ResourceHtmlRenderingEventArgs> e)
    {
        Console::WriteLine(u"resource: " + e->get_PathToResourceFile());
    }));

mailMessage->Save(u"WithExtractedResources.html", options);