โหลดและบันทึกข้อความอีเมล
โหลดข้อความอีเมล
โหลดจาก EML
ส่วนนี้อธิบายวิธีโหลดไฟล์ EML เข้าอ็อบเจ็กต์ MailMessage โดยใช้ EmlLoadOptions class. คลาส EmlLoadOptions ให้ตัวเลือกต่าง ๆ เพื่อปรับแต่งวิธีการโหลดไฟล์ EML เช่น การรักษารูปแบบข้อความฝังหรือการควบคุมพฤติกรรมการโหลดไฟล์แนบ TNEF
- เริ่มต้นอินสแตนซ์ของ EmlLoadOptions
- กำหนดค่าตัวเลือกการโหลดตามต้องการ
- ใช้ MailMessage.Load() เมธอดเพื่อโหลดไฟล์ EML ด้วยตัวเลือกที่กำหนด
using Aspose.Email;
// Initialize EmlLoadOptions
var loadOptions = new EmlLoadOptions
{
// Configure load options
PreferredTextEncoding = Encoding.UTF8, // Sets preferred encoding for the message
PreserveEmbeddedMessageFormat = true, // Preserve format of embedded messages
PreserveTnefAttachments = true, // Control TNEF attachment loading
RemoveSignature = false // Do not remove the signature
};
// Load the EML file
var eml = MailMessage.Load("file.eml", loadOptions);
คุณสมบัติของ EmlLoadOptions
- PreferredTextEncoding: ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อหาข้อความ ค่าเริ่มต้นคือ
null. - PreserveEmbeddedMessageFormat: บ่งบอกว่าจะรักษารูปแบบของข้อความฝังขณะโหลดหรือไม่ ค่าเริ่มต้นคือ
false. - PreserveTnefAttachments: ควบคุมการโหลดไฟล์แนบ TNEF ค่าเริ่มต้นคือ
false. - RemoveSignature: ระบุกำหนดว่าลายเซ็นจะถูกลบขณะโหลดหรือไม่ ค่าเริ่มต้นคือ
false.
รักษารูปแบบข้อความที่ฝังอยู่
string dataDir = RunExamples.GetDataDir_Email();
MailMessage mail = MailMessage.Load(dataDir + "tnefWithMsgInside.eml", new EmlLoadOptions() { PreserveEmbeddedMessageFormat = true });
FileFormatType fileFormat = FileFormatUtil.DetectFileFormat(mail.Attachments[0].ContentStream).FileFormatType;
Console.WriteLine("Embedded message file format: " + fileFormat);
โหลดข้อความพร้อมลายเซ็น/ไม่มีลายเซ็น
การเก็บลายเซ็นเป็นค่าเริ่มต้นเมื่อโหลดไฟล์ EML หากต้องการลบลายเซ็นสามารถตั้งค่า LoadOptions.RemoveSignature ตั้งคุณสมบัติเป็น true.
ตัวอย่างโค้ดด้านล่างแสดงวิธีลบลายเซ็นขณะโหลดข้อความ:
var msg = MailMessage.Load(fileName, new EmlLoadOptions() { RemoveSignature = true});
โหลดจาก EMLX
ส่วนต่อไปนี้อธิบายการโหลดไฟล์ EMLX ด้วย EmlxLoadOptions คลาส. คลาสนี้ให้ตัวเลือกที่คล้ายกับ EmlLoadOptions เพื่อควบคุมการเข้ารหัส, การลบลายเซ็น, และอื่น ๆ.
- สร้างอินสแตนซ์ของ EmlxLoadOptions.
- กำหนดค่าคุณสมบัติตามจำเป็น.
- ใช้ MailMessage.Load() วิธีการโหลดไฟล์ EMLX.
using Aspose.Email;
// Instantiate EmlxLoadOptions
var loadOptions = new EmlxLoadOptions
{
// Configure load options
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
PreserveEmbeddedMessageFormat = true, // Preserve embedded message formats
RemoveSignature = true // Remove signatures during loading
};
// Load the EMLX file
var emlx = MailMessage.Load("file.emlx", loadOptions);
คุณสมบัติของ EmlxLoadOptions
- PreferredTextEncoding: ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อหาข้อความ ค่าเริ่มต้นคือ
null. - PreserveEmbeddedMessageFormat: บ่งบอกว่าจะรักษารูปแบบของข้อความฝังหรือไม่ ค่าเริ่มต้นคือ
false. - RemoveSignature: ระบุว่าจะลบลายเซ็นขณะโหลดหรือไม่ ค่าเริ่มต้นคือ
false.
โหลดจาก HTML
เรียนรู้วิธีโหลดไฟล์ HTML ลงใน MailMessage โดยใช้ HtmlLoadOptions คลาส. คลาสนี้ออกแบบมาเฉพาะเพื่อจัดการเนื้อหา HTML พร้อมตัวเลือกในการจัดการทรัพยากรและเพิ่มมุมมองข้อความธรรมดา.
- เริ่มต้นอินสแตนซ์ของ HtmlLoadOptions.
- กำหนดค่าคุณสมบัติที่จำเป็น.
- ใช้ MailMessage.Load() วิธีการโหลดไฟล์ HTML ด้วยตัวเลือกที่ระบุ
using Aspose.Email;
// Initialize HtmlLoadOptions
var loadOptions = new HtmlLoadOptions
{
// Configure load options
PathToResources = "resources/", // Path to directory containing resource files
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
ShouldAddPlainTextView = true // Add plain text view of the body
};
// Load the HTML file
var html = MailMessage.Load("file.html", loadOptions);
คุณสมบัติของ HtmlLoadOptions
- PathToResources: กำหนดพาธไปยังไดเรกทอรีที่มีไฟล์ทรัพยากร (เช่น รูปภาพ).
- PreferredTextEncoding: ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อหาข้อความค่าเริ่มต้นคือ
null. - PreserveEmbeddedMessageFormat: กำหนดว่าจะเก็บรูปแบบของข้อความที่ฝังอยู่หรือไม่ค่าเริ่มต้นคือ
false. - ShouldAddPlainTextView: ระบุว่าจะเพิ่มมุมมองข้อความธรรมดาของเนื้อหาข้อความหรือไม่ค่าเริ่มต้นคือ
false.
โหลดจาก MHTML
ส่วนนี้อธิบายวิธีโหลดไฟล์ MHTML ด้วย MhtmlLoadOptions คลาส. คลาส MhtmlLoadOptions มีตัวเลือกในการจัดการการเข้ารหัส, เก็บรูปแบบของข้อความที่ฝังอยู่, และจัดการไฟล์แนบ TNEF.
- สร้างอินสแตนซ์ของ MhtmlLoadOptions.
- กำหนดค่าคุณสมบัติที่ต้องการ.
- โหลดไฟล์ MHTML ด้วย MailMessage.Load() เมธอด.
using Aspose.Email;
// Create MhtmlLoadOptions
var loadOptions = new MhtmlLoadOptions
{
// Set load options
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
PreserveEmbeddedMessageFormat = true, // Preserve format of embedded messages
PreserveTnefAttachments = true, // Handle TNEF attachments
RemoveSignature = false // Keep the signature
};
// Load the MHTML file
var mhtml = MailMessage.Load("file.mht", loadOptions);
คุณสมบัติของ MhtmlLoadOptions
- MessageFormat: แสดงรูปแบบของข้อความอีเมล ที่อาจเป็น EML, MSG หรือ MHTML ค่าเริ่มต้นคือ EML.
- PreferredTextEncoding: ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อหาข้อความ ค่าเริ่มต้นคือ
null. - PreserveEmbeddedMessageFormat: กำหนดว่าจะเก็บรูปแบบของข้อความที่ฝังอยู่หรือไม่ค่าเริ่มต้นคือ
false. - PreserveTnefAttachments: ควบคุมการโหลดไฟล์แนบ TNEF ค่าเริ่มต้นคือ
false. - RemoveSignature: ระบุว่าจะลบลายเซ็นขณะโหลดหรือไม่ ค่าเริ่มต้นคือ
false.
โหลดจาก MSG
ส่วนนี้อธิบายวิธีโหลดไฟล์ MSG ลงในอ็อบเจกต์ MailMessage โดยใช้ MsgLoadOptions คลาส. คลาส MsgLoadOptions มีคุณสมบัติต่าง ๆ เพื่อจัดการวิธีการโหลดไฟล์ MSG รวมถึงตัวเลือกในการเก็บเนื้อหา RTF หรือจัดการไฟล์แนบ TNEF.
- สร้างอินสแตนซ์ของ MsgLoadOptions.
- ตั้งค่าคุณสมบัติที่ต้องการเพื่อปรับแต่งการโหลด.
- โหลดไฟล์ MSG โดยใช้ MailMessage.Load() เมธอด.
using Aspose.Email;
// Create MsgLoadOptions
var loadOptions = new MsgLoadOptions
{
// Set load options
KeepOriginalEmailAddresses = true, // Preserve original email addresses
PreferredTextEncoding = Encoding.UTF8, // Set preferred encoding
PreserveRtfContent = true, // Keep RTF content in the message
PreserveTnefAttachments = true, // Handle TNEF attachments
RemoveSignature = false, // Keep the signature
Timeout = 5000 // Set timeout to 5 seconds
};
// Load the MSG file
var msg = MailMessage.Load("file.msg", loadOptions);
คุณสมบัติของ MsgLoadOptions
- KeepOriginalEmailAddresses: บ่งชี้ว่าจะเก็บที่อยู่อีเมลต้นฉบับหรือไม่ค่าเริ่มต้นคือ
false. - PreferredTextEncoding: กำหนดการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อหาข้อความค่าเริ่มต้นคือ
null. - PreserveEmbeddedMessageFormat: กำหนดว่ารูปแบบของข้อความที่ฝังอยู่จะต้องถูกเก็บไว้หรือไม่ค่าเริ่มต้นคือ
false. - PreserveRtfContent: ระบุว่าจะเก็บเนื้อหา RTF body ใน
MailMessage. ค่าเริ่มต้นคือfalse. - PreserveTnefAttachments: จัดการพฤติกรรมการโหลดไฟล์แนบ TNEFค่าเริ่มต้นคือ
false. - RemoveSignature: กำหนดว่าจะลบลายเซ็นระหว่างการโหลดหรือไม่ค่าเริ่มต้นคือ
false. - เวลาจำกัด: จำกัดเวลาการจัดรูปแบบเป็นมิลลิวินาทีระหว่างการแปลงค่าเริ่มต้นคือ 3000 ms.
โหลดจาก TNEF
เรียนรู้วิธีโหลดไฟล์ TNEF eml ด้วย TnefLoadOptions class. คลาสนี้ให้ตัวเลือกเพื่อจัดการการเข้ารหัสและลบลายเซ็นขณะโหลด
- สร้างอินสแตนซ์ของ TnefLoadOptions
- กำหนดค่าคุณสมบัติตามต้องการ
- ใช้ MailMessage.Load() เมธอดเพื่อโหลดไฟล์ TNEF
using Aspose.Email;
// Instantiate TnefLoadOptions
var loadOptions = new TnefLoadOptions
{
// Configure load options
PreferredTextEncoding = Encoding.UTF8, // Set encoding preference
PreserveEmbeddedMessageFormat = true, // Preserve embedded message formats
RemoveSignature = true // Remove signatures during loading
};
// Load the TNEF file
var tnef = MailMessage.Load("file.eml", loadOptions);
คุณสมบัติของ TnefLoadOptions
- MessageFormat: แสดงรูปแบบข้อความเมล เช่น EML, MSG, หรือ MHTML ค่าเริ่มต้นคือ EML.
- PreferredTextEncoding: ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อหาข้อความ ค่าเริ่มต้นคือ
null. - PreserveEmbeddedMessageFormat: บ่งบอกว่าจะรักษารูปแบบของข้อความฝังหรือไม่ ค่าเริ่มต้นคือ
false. - RemoveSignature: ระบุว่าจะลบลายเซ็นขณะโหลดหรือไม่ ค่าเริ่มต้นคือ
false.
โหลดข้อความจากไลบรารีเอกสาร Microsoft SharePoint
คุณสามารถอ่านข้อความอีเมลที่เก็บอยู่ในไลบรารีเอกสาร Microsoft SharePoint แล้วโหลดเข้าสู่ MailMessage อ็อบเจกต์ เพื่อเข้าถึงไฟล์ในไลบรารีเอกสาร SharePoint จำเป็นต้องติดตั้ง SDK ของ SharePoint บนระบบ SDK ให้ API ที่จำเป็นสำหรับล็อกอินและเข้าถึงไฟล์จากไลบรารีเอกสาร
ตัวอย่างด้านล่างสมมติว่าเป็นไฟล์ข้อความ Microsoft Outlook (.msg) ถูกจัดเก็บไว้แล้วในโฟลเดอร์ Shared Documents ของไลบรารีเอกสาร SharePoint SDK ของ SharePoint ใช้เพื่ออ่านไฟล์ข้อความเข้าสู่สตรีม แล้วส่งต่อให้ MailMessage.Load เมธอด เมื่อลูกค้าโหลดแล้ว คุณสมบัติเช่น (subject, text body, HTML body ฯลฯ) สามารถเข้าถึงได้ สำหรับข้อมูลเกี่ยวกับวิธีการติดตั้งและกำหนดค่า Microsoft SharePoint Server และ SDK โปรดดูส่วนที่เกี่ยวข้องในไลบรารี MSDN
// Private members
private SPSite site = null;
private SPWeb web = null;
private void EstablishSharepoint()
{
site = new SPSite("http://localhost/Site1");
web = site.OpenWeb();
}
private void button2_Click(object sender, EventArgs e)
{
SPSecurity.CodeToRunElevated elevatedGetSite = new SPSecurity.CodeToRunElevated(EstablishSharepoint);
SPSecurity.RunWithElevatedPrivileges(elevatedGetSite);
// Path to the MSG file stored in the Shared Documents folder
SPFile msgFile = web.GetFile("Shared Documents/Test.msg");
// Read the file into a memory stream
MemoryStream fileStream = new MemoryStream();
byte[] currentFileContent = msgFile.OpenBinary();
fileStream.Write(currentFileContent, 0, currentFileContent.Length);
fileStream.Position = 0;
// Create an instance of the MailMessage class and pass the memory stream of the MSG file to it
MailMessage msg = MailMessage.Load(fileStream, new MsgLoadOptions());
fileStream.Close();
fileStream.Dispose();
// Access the MailMessage class' public properties
Console.WriteLine("Subject: " + msg.Subject);
Console.WriteLine("From: " + msg.From.ToString());
Console.WriteLine("Text Body: " + msg.TextBody);
}
การตรวจจับรูปแบบไฟล์
Aspose.Email API ให้ความสามารถในการตรวจจับรูปแบบไฟล์ของไฟล์ข้อความที่ให้ไว้. The DetectFileFormat เมธอดของ FileFormatUtil คลาส สามารถใช้เพื่อบรรลุเป้าหมายนี้ ประเภทและเมธอดต่อไปนี้สามารถใช้ตรวจจับรูปแบบไฟล์ที่โหลดได้
- FileFormatType Enumeration
- FileFormatInfo คลาส
- FileFormatUtil คลาส
- FileFormatUtil.DetectFileFormat(Stream) Method
- FileFormatUtil.DetectFileFormat(String) Method
โค้ดตัวอย่างต่อไปนี้แสดงวิธีตรวจจับรูปแบบไฟล์.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
// Detect file format and Gets the detected load format
FileFormatInfo info = FileFormatUtil.DetectFileFormat(dataDir + "message.msg");
Console.WriteLine("The message format is: " + info.FileFormatType);
บันทึกและแปลงข้อความ
Aspose.Email ทำให้การแปลงประเภทข้อความใดๆ ไปยังรูปแบบอื่นเป็นเรื่องง่าย. เพื่อแสดงคุณลักษณะนี้, โค้ดในบทความนี้โหลดข้อความสามประเภทจากดิสก์และบันทึกกลับเป็นรูปแบบอื่น. คลาสฐาน SaveOptions และคลาส EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions สำหรับการตั้งค่าเพิ่มเติมเมื่อบันทึก MailMessage สามารถใช้เพื่อบันทึกข้อความเป็นรูปแบบอื่น. บทความนี้แสดงวิธีใช้คลาสเหล่านี้เพื่อบันทึกอีเมลตัวอย่างเป็น:
- รูปแบบ EML.
- Outlook MSG.
- รูปแบบ MHTML.
- รูปแบบ HTML.
บันทึกเป็น EML
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML และบันทึกลงดิสก์ในรูปแบบเดียวกัน.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
// Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage mailMessage = MailMessage.Load(dataDir + "Attachments.eml");
mailMessage.Save(dataDir + "LoadAndSaveFileAsEML_out.eml", SaveOptions.DefaultEml);
ขอบเขตของข้อความต้นฉบับ
เมื่อโหลดข้อความ EML แล้วบันทึกกลับเป็น EML ขอบเขตของข้อความต้นฉบับจะถูกเก็บไว้ตามค่าเริ่มต้น ดังนั้นไม่ต้องตั้งค่าใด ๆ เพื่อรักษาขอบเขต เวอร์ชันก่อนของไลบรารีมี EmlSaveOptions.PreserveOriginalBoundaries คุณสมบัตินี้สำหรับวัตถุประสงค์ดังกล่าว คุณสมบัตินี้ได้ถูกลบออกแล้ว และการรักษาขอบเขตเดิมเป็นพฤติกรรมเริ่มต้นแล้ว
ปรับแต่งขอบเขต MIME
หากคุณต้องการให้ขอบเขตตามรูปแบบที่กำหนดเฉพาะ ให้ใช้ BoundariesTemplate คุณสมบัติของ EmlSaveOptions คลาส กำหนดเทมแพลตที่ใช้สร้างขอบเขต MIME สำหรับข้อความที่บันทึก ค่าตั้งต้นคือ null, ซึ่งหมายความว่าขอบเขตถูกสร้างโดยไลบรารี รองรับไวลด์การ์ดสองตัวในเทมแพลต:
{#}– หมายเลขขอบเขต{guid}– GUID ที่สร้างขึ้น
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลด EML และบันทึกเป็น EML พร้อมขอบเขต MIME ที่กำหนดเอง
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
MailMessage mailMessage = MailMessage.Load(dataDir + "Attachments.eml");
// Save as eml with a custom boundary template
EmlSaveOptions emlSaveOptions = new EmlSaveOptions(MailMessageSaveType.EmlFormat)
{
BoundariesTemplate = "boundary_{#}_{guid}"
};
mailMessage.Save(dataDir + "CustomBoundaries_out.eml", emlSaveOptions);
คงไฟล์แนบ TNEF
โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกเป็น EML โดยคงไฟล์แนบ TNEF ไว้.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
MailMessage mailMessage = MailMessage.Load(dataDir + "tnefEML.eml");
// Save as eml with preserved attachment
EmlSaveOptions emlSaveOptions = new EmlSaveOptions(MailMessageSaveType.EmlFormat)
{
FileCompatibilityMode = FileCompatibilityMode.PreserveTnefAttachments
};
mailMessage.Save(dataDir + "PreserveTNEFAttachment_out.eml", emlSaveOptions);
บันทึก EML เป็น MSG
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML และแปลงเป็น MSG โดยใช้ตัวเลือกที่เหมาะสมจาก SaveOptions.
// Data directory for reading and writing files
string dataDir = RunExamples.GetDataDir_Email();
// Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage eml = MailMessage.Load(dataDir + "Message.eml");
// Save the Email message to disk in ASCII format and Unicode format
eml.Save(dataDir + "AnEmail_out.msg", SaveOptions.DefaultMsgUnicode);
คงวันที่
นี้ MsgSaveOptions คลาสนี้ช่วยให้คุณบันทึกข้อความต้นฉบับเป็นไฟล์ Outlook Message (MSG) โดยคงวันที่ไว้ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกเป็น MSG พร้อมคงวันที่.
// Data directory for reading and writing files
string dataDir = RunExamples.GetDataDir_Email();
// Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage eml = MailMessage.Load(dataDir + "Message.eml");
// Save as msg with preserved dates
MsgSaveOptions msgSaveOptions = new MsgSaveOptions(MailMessageSaveType.OutlookMessageFormatUnicode)
{
PreserveOriginalDates = true
};
eml.Save(Path.Combine(dataDir, "outTest_out.msg"), msgSaveOptions);
บันทึก EML เป็น MHTML
ตัวเลือกต่างๆ ของ MHTML สามารถใช้เพื่อให้ได้ผลลัพธ์ที่ต้องการ. โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML เข้าไปใน MailMessage และแปลงเป็น MHTML พร้อมวันที่ข้อความในระบบ UTC.
// Set options for MHTML output
MhtSaveOptions saveOptions = SaveOptions.DefaultMhtml;
saveOptions.PreserveOriginalDate = false; // save a message date as UTC date
// Initialize and load an existing EML file
using (MailMessage mailMessage = MailMessage.Load(folderPath + "Message.eml"))
{
mailMessage.Save(folderPath + "Message_out.mhtml", saveOptions);
}
ตัวเลือกการแปลง
นี้ MhtSaveOptions คลาสนี้ให้ตัวเลือกเพิ่มเติมสำหรับการบันทึกอีเมลเป็นรูปแบบ MHTML. ตัวนับค่า MhtFormatOptions ทำให้สามารถเขียนข้อมูลอีเมลเพิ่มเติมลงไฟล์ MHTML ได้. ฟิลด์เพิ่มเติมต่อไปนี้สามารถเขียนได้:
WriteHeader– เขียนส่วนหัวอีเมลไปยังไฟล์ผลลัพธ์.WriteOutlineAttachments– เขียนไฟล์แนบแบบโครงร่างไปยังไฟล์ผลลัพธ์.WriteCompleteEmailAddress– เขียนที่อยู่อีเมลเต็มไปยังไฟล์ผลลัพธ์.NoEncodeCharacters– ไม่ควรใช้การเข้ารหัสการส่งของอักขระ.HideExtraPrintHeader– ซ่อนส่วนหัวพิมพ์เพิ่มเติมจากด้านบนของไฟล์ผลลัพธ์.WriteCompleteToEmailAddress– เขียนที่อยู่อีเมลของผู้รับเต็มไปยังไฟล์ผลลัพธ์.WriteCompleteFromEmailAddress– เขียนที่อยู่อีเมลของผู้ส่งเต็มไปยังไฟล์ผลลัพธ์.WriteCompleteCcEmailAddress– เขียนที่อยู่อีเมลเต็มของผู้รับที่ถูก CC ไปยังไฟล์ผลลัพธ์.WriteCompleteBccEmailAddress– เขียนที่อยู่อีเมลเต็มของผู้รับที่ถูก BCC ไปยังไฟล์ผลลัพธ์.RenderCalendarEvent– เขียนข้อความจากเหตุการณ์ปฏิทินไปยังไฟล์ผลลัพธ์.SkipByteOrderMarkInBody– ข้ามไบต์ BOM (Byte Order Mark) ในส่วนเนื้อหาของไฟล์ผลลัพธ์RenderVCardInfo– เขียนข้อความจาก VCard AlternativeView ไปยังไฟล์ผลลัพธ์.DisplayAsOutlook– แสดงหัวข้อ From.RenderTaskFields– เขียนฟิลด์ Task เฉพาะลงในไฟล์ผลลัพธ์.None– ไม่ได้ระบุการตั้งค่า.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีแปลงไฟล์ EML เป็น MHTML พร้อมการตั้งค่าเพิ่มเติม.
MailMessage eml = MailMessage.Load(Path.Combine(dataDir, "Message.eml"));
// Save as mht with header
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions
{
//Specify formatting options required
//Here we are specifying to write header information to the output without writing extra print header
//and the output headers should display as the original headers in message
MhtFormatOptions = MhtFormatOptions.WriteHeader | MhtFormatOptions.HideExtraPrintHeader | MhtFormatOptions.DisplayAsOutlook,
// Check the body encoding for validity.
CheckBodyContentEncoding = true
};
eml.Save(Path.Combine(dataDir, "outMessage_out.mht"), mhtSaveOptions);
เรนเดอร์เหตุการณ์ปฏิทิน
นี้ MhtFormatOptions.RenderCalendarEvent แสดงเหตุการณ์ปฏิทินเป็นผลลัพธ์ MHTML โค้ดตัวอย่างต่อไปนี้แสดงวิธีเรนเดอร์เหตุการณ์ปฏิทินขณะแปลงเป็น MHTML.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
string fileName = "Meeting with Recurring Occurrences.msg";
MailMessage msg = MailMessage.Load(dataDir + fileName);
MhtSaveOptions options = new MhtSaveOptions();
options.MhtFormatOptions = MhtFormatOptions.WriteHeader | MhtFormatOptions.RenderCalendarEvent;
//Format the output details if required - optional
//Set the display for Start Property
if (options.FormatTemplates.ContainsKey(MhtTemplateName.Start))
options.FormatTemplates[MhtTemplateName.Start] = @"<span class='headerLineTitle'>Start:</span><span class='headerLineText'>{0}</span><br/>";
else
options.FormatTemplates.Add(MhtTemplateName.Start, @"<span class='headerLineTitle'>Start:</span><span class='headerLineText'>{0}</span><br/>");
//Set the display for End Property
if (options.FormatTemplates.ContainsKey(MhtTemplateName.End))
options.FormatTemplates[MhtTemplateName.End] = @"<span class='headerLineTitle'>End:</span><span class='headerLineText'>{0}</span><br/>";
else
options.FormatTemplates.Add(MhtTemplateName.End, @"<span class='headerLineTitle'>End:</span><span class='headerLineText'>{0}</span><br/>");
//Set the display for Recurrence Property
if (options.FormatTemplates.ContainsKey(MhtTemplateName.Recurrence))
options.FormatTemplates[MhtTemplateName.Recurrence] = @"<span class='headerLineTitle'>Recurrence:</span><span class='headerLineText'>{0}</span><br/>";
else
options.FormatTemplates.Add(MhtTemplateName.Recurrence, @"<span class='headerLineTitle'>Recurrence:</span><span class='headerLineText'>{0}</span><br/>");
//Set the display for RecurrencePattern Property
if (options.FormatTemplates.ContainsKey(MhtTemplateName.RecurrencePattern))
options.FormatTemplates[MhtTemplateName.RecurrencePattern] = @"<span class='headerLineTitle'>RecurrencePattern:</span><span class='headerLineText'>{0}</span><br/>";
else
options.FormatTemplates.Add(MhtTemplateName.RecurrencePattern, @"<span class='headerLineTitle'>RecurrencePattern:</span><span class='headerLineText'>{0}</span><br/>");
//Set the display for Organizer Property
if (options.FormatTemplates.ContainsKey(MhtTemplateName.Organizer))
options.FormatTemplates[MhtTemplateName.Organizer] = @"<span class='headerLineTitle'>Organizer:</span><span class='headerLineText'>{0}</span><br/>";
else
options.FormatTemplates.Add(MhtTemplateName.Organizer, @"<span class='headerLineTitle'>Organizer:</span><span class='headerLineText'>{0}</span><br/>");
//Set the display for RequiredAttendees Property
if (options.FormatTemplates.ContainsKey(MhtTemplateName.RequiredAttendees))
options.FormatTemplates[MhtTemplateName.RequiredAttendees] = @"<span class='headerLineTitle'>RequiredAttendees:</span><span class='headerLineText'>{0}</span><br/>";
else
options.FormatTemplates.Add(MhtTemplateName.RequiredAttendees, @"<span class='headerLineTitle'>RequiredAttendees:</span><span class='headerLineText'>{0}</span><br/>");
msg.Save(dataDir + "Meeting with Recurring Occurrences.mhtml", options);
Exporting Email to MHT without Inline Images
นี้ SkipInlineImages คุณสมบัติของ MhtSaveOptions คลาสกำหนดว่าจะข้ามการอ้างอิงรูปภาพเมื่อบันทึกเป็น MHTML หรือไม่ ค่าตั้งต้นคือ false.
MailMessage eml = MailMessage.Load(Path.Combine(dataDir, "Message.eml"));
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions
{
SkipInlineImages = true
};
eml.Save(Path.Combine(dataDir, "EmlToMhtmlWithoutInlineImages_out.mht"), mhtSaveOptions);
การส่งออกอีเมลเป็น MHT พร้อมโซนเวลาที่กำหนดเอง
MailMessage class ให้ TimeZoneOffset คุณสมบัติสำหรับตั้งค่าเขตเวลาแบบกำหนดเองเมื่อส่งออกเป็น MHT. โค้ดตัวอย่างต่อไปนี้แสดงวิธีการส่งออกอีเมลเป็น MHT พร้อมตั้งค่าเขตเวลาแบบกำหนดเอง.
MailMessage eml = MailMessage.Load(dataDir + "Message.eml");
// Set the local time for message date.
eml.TimeZoneOffset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
// or set custom time zone offset for message date (-0800)
// eml.TimeZoneOffset = new TimeSpan(-8,0,0);
// The dates will be rendered by local system time zone.
MhtSaveOptions so = new MhtSaveOptions();
so.MhtFormatOptions = MhtFormatOptions.WriteHeader;
eml.Save(dataDir + "ExportEmailToMHTWithCustomTimezone_out.mhtml", so);
ปรับฟอนท์
string dataDir = RunExamples.GetDataDir_Email();
MailMessage eml = MailMessage.Load(dataDir + "Message.eml");
MhtSaveOptions options = SaveOptions.DefaultMhtml;
int cssInsertPos = options.CssStyles.LastIndexOf("</style>");
options.CssStyles = options.CssStyles.Insert(cssInsertPos,
".PlainText{" +
" font-family: sans-serif;" +
"}"
);
eml.Save("message.mhtml", options);
บันทึก EML เป็น HTML
นี้ HtmlSaveOptions คลาสที่ช่วยให้คุณส่งออกเนื้อความของข้อความเป็น HTML. โค้ดตัวอย่างต่อไปนี้แสดงวิธีการบันทึกข้อความเป็น HTML.
MailMessage message = MailMessage.Load(dataDir + "Message.eml");
message.Save(dataDir + "SaveAsHTML_out.html", SaveOptions.DefaultHtml);
// OR
MailMessage eml = MailMessage.Load(dataDir + "Message.eml");
HtmlSaveOptions options = SaveOptions.DefaultHtml;
options.HtmlFormatOptions = HtmlFormatOptions.WriteHeader | HtmlFormatOptions.WriteCompleteEmailAddress; //save the message headers to output HTML using the formatting options
eml.Save(dataDir + "SaveAsHTML1_out.html", options);
ควบคุมวิธีการแสดงผลทรัพยากร
นี้ ResourceRenderingMode คุณสมบัติของ HtmlSaveOptions คลาสกำหนดวิธีการเขียนทรัพยากรข้อความ (เช่น รูปภาพในบรรทัด) ไปยัง HTML ผลลัพธ์ โดยรับค่าจาก ResourceRenderingMode enumeration:
EmbedIntoHtml– ฝังทรัพยากรลงใน HTML เป็นสตริง base64 นี่คือค่าตั้งต้นSaveToFile– บันทึกทรัพยากรเป็นไฟล์และแทรกเส้นทางไปยังแต่ละไฟล์ลงในsrcแท็ก.SubstituteFromFile– ใช้ไฟล์กำหนดเองเป็นไอคอนและแทรกเส้นทางไปยังไฟล์นั้นลงในsrcแท็ก.None– ไม่แสดงทรัพยากรใน HTML
เมื่อทรัพยากรไม่ได้ฝังอยู่ SaveResourceHandler delegate จะถูกเรียกสำหรับทุกไฟล์แนบข้อความ ซึ่งทำให้คุณกำหนดได้ว่าแต่ละทรัพยากรจะถูกจัดเก็บที่ไหนและเส้นทางใดจะถูกเขียนลงใน HTML
var fileName = "EmailWithAttandEmbedded.eml";
var filePath = Path.Combine(dataDir, fileName);
MailMessage msg = MailMessage.Load(filePath);
var outFileName = Path.Combine(dataDir, fileName + ".html");
var options = new HtmlSaveOptions()
{
ResourceRenderingMode = ResourceRenderingMode.SaveToFile,
SaveResourceHandler =
(AttachmentBase attachment, out string resourcePath) =>
{
attachment.Save(Path.Combine(dataDir, attachment.ContentId));
resourcePath = Path.Combine(".", attachment.ContentId);
}
};
msg.Save(outFileName, options);
บันทึก EML เป็น OFT
โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกข้อความเป็นไฟล์เทมเพลต Outlook (.oft).
using (MailMessage eml = new MailMessage("test@from.to", "test@to.to", "template subject", "Template body"))
{
string oftEmlFileName = "EmlAsOft_out.oft";
MsgSaveOptions options = SaveOptions.DefaultMsgUnicode;
options.SaveAsTemplate = true;
eml.Save(dataDir + oftEmlFileName, options);
}
แปลง EML เป็น MSG (MailMessage เป็น MapiMessage)
เพื่อแปลง MailMessage อ็อบเจกต์ที่โหลดจากไฟล์ EML ไปยัง MapiMessage อ็อบเจกต์ ใช้ MapiConversionOptions คลาส การแปลงนี้มักจำเป็นเนื่องจากอ็อบเจกต์สองตัวนี้ทำหน้าที่ต่างกันและตอบสนองความต้องการที่แตกต่างกันในการประมวลผลอีเมล MapiMessage ถูกออกแบบมาโดยเฉพาะเพื่อความเข้ากันได้กับ Microsoft Outlook และ Exchange Server เนื่องจากเป็นไปตามรูปแบบ Messaging Application Programming Interface (MAPI) ซึ่งทำให้มั่นใจได้ว่าอีเมลที่แปลงเป็นไฟล์ MSG จะเข้ากันได้อย่างเต็มที่กับผลิตภัณฑ์ของ Microsoft ทำให้สามารถเปิด แก้ไข และจัดการได้โดยตรงใน Outlook
การแปลงนี้ยังจำเป็นเมื่อต้องการเก็บอีเมลเป็นถังพัก ดัชนี หรือประมวลผลโดยที่จัดเก็บที่ต้องใช้รูปแบบ MAPI เช่น PST/OST, OLM ตัวอย่างเช่น MapiConversionOptions คลาสนี้ช่วยให้คุณสามารถระบุตัวเลือกเพิ่มเติม เช่น การรักษารูปแบบข้อความต้นฉบับ การเก็บวันที่ต้นฉบับ และการบีบอัดเนื้อหา RTF ระหว่างการแปลง ตัวอย่างโค้ดต่อไปนี้แสดงวิธีโหลดอีเมลจากไฟล์ในรูปแบบ .eml และแปลงเป็นรูปแบบ Outlook .msg ซึ่งอนุญาตให้ปรับแต่งได้แต่ยังคงคุณลักษณะบางอย่างของอีเมลต้นฉบับไว้ระหว่างการแปลง
- สร้างอินสแตนซ์ของ
MapiConversionOptionsและกำหนดคุณสมบัติของมัน - แปลง
MailMessageอ็อบเจกต์เป็นMapiMessageอ็อบเจกต์ที่ใช้ตัวเลือกการแปลงที่กำหนดค่าไว้
MailMessage eml = MailMessage.Load("email.eml", new EmlLoadOptions());
// Create an instance of MapiConversionOptions
var conversionOptions = new MapiConversionOptions
{
// Configure MapiConversionOptions
Format = OutlookMessageFormat.Unicode, // Use Unicode format for MSG
PreserveEmbeddedMessageFormat = true, // Preserve the format of embedded messages
PreserveOriginalAddresses = true, // Keep original email addresses
PreserveOriginalDates = true, // Preserve original dates
PreserveEmptyDates = false, // Generate new dates if original are empty
RemoveSignature = false, // Do not remove the signature
UseBodyCompression = true, // Enable RTF body compression
ForcedRtfBodyForAppointment = false // Disable forced RTF body for appointments
};
// Convert MailMessage to MapiMessage
MapiMessage msg = MapiMessage.FromMailMessage(eml, conversionOptions);
คุณสมบัติที่พร้อมใช้งานของ MapiConversionOptions
-
ASCIIFormat: คืนค่า
MapiConversionOptionsกำหนดค่าโดยใช้OutlookMessageFormatเป็น ASCII พร้อมPreserveSignatureตั้งเป็น false และUseBodyCompressionตั้งเป็น false ตัวเลือกนี้มีประโยชน์เมื่อคุณต้องการการแปลงเป็นรูปแบบ ASCII ธรรมดา. -
UnicodeFormat: คืนค่า
MapiConversionOptionsกับOutlookMessageFormatตั้งเป็น Unicode และทั้งสองPreserveSignatureและUseBodyCompressionตั้งเป็น false ใช้ตัวเลือกนี้เพื่อสนับสนุนอักขระที่ครบถ้วนมากขึ้นในไฟล์ MSG. -
ForcedRtfBodyForAppointment: เมื่อตั้งค่าเป็น true จะบังคับให้ใช้ส่วนเนื้อหา RTF สำหรับการนัดปฏิทิน ซึ่งเป็นประโยชน์หากต้องการให้เข้ากันได้กับไคลเอนต์เมลบางประเภทค่าเริ่มต้นคือ true.
-
Format: ระบุรูปแบบเอาต์พุตของไฟล์ MSG ซึ่งสามารถตั้งเป็น
OutlookMessageFormat.ASCIIหรือOutlookMessageFormat.Unicode, ควบคุมว่าข้อความ MSG ที่แปลงแล้วควรใช้การเข้ารหัส ASCII หรือ Unicode. -
PreserveEmbeddedMessageFormat: กำหนดว่าจะคงรูปแบบ EML ดั้งเดิมของข้อความที่ฝังอยู่ระหว่างการแปลงเป็น
MapiMessage. การตั้งค่าเป็น true จะทำให้ข้อความอีเมลที่ฝังอยู่คงโครงสร้างต้นฉบับค่าเริ่มต้นคือ false. -
PreserveEmptyDates: เมื่อกำหนดเป็น true จะคงวันที่บันทึกและแก้ไขต้นฉบับจาก EML หากไม่เช่นนั้น วันที่ใหม่จะถูกสร้างหากวันที่ต้นฉบับว่าง.
-
PreserveOriginalAddresses: หากตั้งค่าเป็น true จะรักษาที่อยู่อีเมลต้นฉบับโดยไม่ทำการตรวจสอบ ซึ่งเป็นประโยชน์เมื่อทำงานกับที่อยู่อีเมลที่อาจไม่ตรงตามมาตรฐานเข้มงวดแต่ต้องการเก็บไว้ตามเดิมค่าเริ่มต้นคือ false.
-
PreserveOriginalDates: รับประกันว่ารูปแบบวันที่ส่งและรับต้นฉบับจะถูกเก็บไว้ระหว่างกระบวนการแปลงการตั้งค่าเป็น true จะรักษาวันที่เหล่านี้เพื่อความแม่นยำทางประวัติศาสตร์ที่ดียิ่งขึ้น.
-
RemoveSignature: ควบคุมว่าจะลบลายเซ็นดิจิทัล (หากมี) ออกจากข้อความระหว่างการแปลงหรือไม่ค่าเริ่มต้นคือ false หมายความว่าลายเซ็นจะถูกเก็บไว้จนกว่าจะลบอย่างชัดเจน.
-
UseBodyCompression: เปิดการบีบอัดส่วนเนื้อหา RTF หากตั้งค่าเป็น true ซึ่งช่วยลดขนาดไฟล์ MSG โดยเฉพาะเมื่อจัดการกับข้อความขนาดใหญ่ที่มีการจัดรูปแบบซับซ้อน.