การสร้างและบันทึกไฟล์ Outlook
Aspose.Email รองรับการสร้างไฟล์ Outlook message (MSG) บทความนี้อธิบายวิธีทำ:
- สร้างและบันทึกข้อความ Outlook
- สร้างไฟล์ MSG พร้อมไฟล์แนบ
- สร้างไฟล์ MSG พร้อมเนื้อหา RTF
- บันทึกข้อความในสถานะฉบับร่าง
สร้างและบันทึกข้อความ Outlook
นี้ MailMessage คลาสมี Save() เมธอดที่สามารถบันทึกไฟล์ Outlook MSG ลงดิสก์หรือสตรีม โค้ดสแนปเปิลด้านล่างสร้างอินสแตนซ์ของ MailMessage คลาส, ตั้งค่าคุณสมบัติเช่น from, to, subject และ body. The Save() เมธอดรับชื่อไฟล์เป็นอากิวเมนต์ นอกจากนี้ Outlook Messages สามารถสร้างด้วย เนื้อหา RTF ที่บีบอัด โดยใช้ MapiConversionOptions.
- สร้างอินสแตนซ์ใหม่ของ MailMessage คลาสและตั้งค่าคุณสมบัติ From, To, Subject และ Body.
- เรียกใช้ MapiMessage คลาส FromMailMessage เมธอดที่รับอ็อบเจกต์ของ MailMessage ประเภท. The FromMailMessage เมธอดแปลง MailMessage เป็น a MapiMessage (MSG).
- เรียกใช้ MapiMessage.Save() เมธอดเพื่อบันทึกไฟล์ MSG.
เขียนโค้ดต่อไปนี้ในเหตุการณ์คลิกของปุ่มควบคุมในแอปพลิเคชัน Windows.
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.From = "sender@domain.com";
mailMsg.To = "receiver@domain.com";
mailMsg.Subject = "This is test message";
mailMsg.Body = "This is test body";
// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
// Save the message (MSG) file
string strMsgFile = @"CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.Save(dataDir + strMsgFile);
สร้างไฟล์ MSG พร้อมไฟล์แนบ
ในตัวอย่างข้างต้น, เราได้สร้างไฟล์ MSG อย่างง่าย Aspose.Email ยังรองรับการบันทึกไฟล์ข้อความที่มีไฟล์แนบ สิ่งที่คุณต้องทำคือเพิ่มไฟล์แนบลงใน MailMessage อินสแตนซ์. เพิ่มไฟล์แนบโดยเรียกเมธอด Add() บน MailMessage.Attachments คอลเลกชัน เพิ่ม ListBox ลงในฟอร์มที่สร้างด้านบนและเพิ่มสองปุ่ม หนึ่งสำหรับเพิ่มและอีกหนึ่งสำหรับลบไฟล์แนบ แอปพลิเคชันที่เพิ่มไฟล์แนบทำงานดังนี้:
- เมื่อคลิกปุ่ม Add Attachment จะปรากฏ Open File Dialog เพื่อช่วยให้ผู้ใช้เรียกดูและเลือกไฟล์แนบ.
- เมื่อไฟล์ถูกเลือก เส้นทางเต็มจะถูกเพิ่มในรายการ.
- เมื่อสร้างไฟล์ MSG เส้นทางไฟล์แนบจะถูกดึงจากรายการและเพิ่มไปยัง MailMessage.Attachments คอลเลกชัน.
เขียนโค้ดต่อไปนี้ในเหตุการณ์คลิกปุ่ม Add Attachment.
// Open a file open dialog to select the attachment
OpenFileDialog fd = new OpenFileDialog();
// If user selected the file and clicked OK, add the file name and path to the list
if (fd.ShowDialog() == DialogResult.OK)
{
lstAttachments.Items.Add(fd.FileName);
}
เมื่อคลิกปุ่ม Remove Attachment ให้ลบรายการที่เลือกออกจาก ListBox. เขียนโค้ดต่อไปนี้ในเหตุการณ์คลิกปุ่ม Remove Attachment.
// Check if the user has selected any item in the attachments listbox
if (lstAttachments.SelectedItems.Count > 0)
{
// Remove the selected attachment from the listbox
lstAttachments.Items.RemoveAt(lstAttachments.SelectedIndex);
}
เพิ่มโค้ดสำหรับการเพิ่มไฟล์แนบไปยัง MailMessage อินสแตนซ์. โค้ดสุดท้ายสำหรับฟังก์ชัน Write Msg ถูกเขียนดังนี้.
// File name for output MSG file
string strMsgFile;
// Open a save file dialog for saving the file and Add filter for msg files
SaveFileDialog fd = new SaveFileDialog();
fd.Filter = "Outlook Message files (*.msg)|*.msg|All files (*.*)|*.*";
// If user pressed OK, save the file name + path
if (fd.ShowDialog() == DialogResult.OK)
{
strMsgFile = fd.FileName;
}
else
{
// If user did not selected the file, return
return;
}
// Create an instance of MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.From = txtFrom.Text;
mailMsg.To = txtTo.Text;
mailMsg.Subject = txtSubject.Text;
mailMsg.Body = txtBody.Text;
// Add the attachments
foreach (string strFileName in lstAttachments.Items)
{
mailMsg.Attachments.Add(new Attachment(strFileName));
}
// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.Save(strMsgFile);
สร้างไฟล์ MSG พร้อมส่วนเนื้อหา RTF
คุณยังสามารถสร้างไฟล์ Outlook Message (MSG) พร้อมเนื้อหา rich text (RTF) ด้วย Aspose.Email เนื้อหา RTF รองรับการจัดรูปแบบข้อความ สร้างโดยตั้งค่า MailMessage.HtmlBody คุณสมบัติ. เมื่อคุณแปลง a MailMessage อินสแตนซ์เป็น a MapiMessage ในกรณีนี้, ส่วน HTML จะถูกแปลงเป็น RTF วิธีนี้ทำให้การจัดรูปแบบของเนื้อหาอีเมลคงเดิม.
ตัวอย่างต่อไปนี้สร้างไฟล์ MSG พร้อมเนื้อหา RTF มีหัวข้อหนึ่งรายการพร้อมการจัดรูปแบบตัวหนาและขีดเส้นใต้ในส่วน HTML การจัดรูปแบบนี้จะคงไว้เมื่อ HTML ถูกแปลงเป็น RTF.
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.From = "from@domain.com";
mailMsg.To = "to@domain.com";
mailMsg.Subject = "subject";
mailMsg.HtmlBody = "<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>";
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.Save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");
การบีบอัด RTF สำหรับส่วนเนื้อหาข้อความ MAPI
NOTE: กระบวนการบีบอัดอาจทำให้ประสิทธิภาพช้าลงเมื่สร้างข้อความ หากเข้าใจข้อเท็จจริงนี้และกำหนดค่าสถานะการบีบอัดตามความต้องการเฉพาะและการประนีประนอมระหว่างขนาดไฟล์และประสิทธิภาพ นักพัฒนาสามารถจัดการการสร้างไฟล์ MSG และ PST ได้อย่างมีประสิทธิภาพเมื่อทำงานกับข้อความอีเมล
การบีบอัด RTF มีจุดประสงค์เพื่อลดขนาดของข้อความรวมถึงไฟล์ PST (Personal Storage Table) ที่ Microsoft Outlook ใช้ในการจัดเก็บข้อความอีเมลและข้อมูลอื่น ๆ โดยการใช้การบีบอัด RTF เมื่อตั้งค่าชิ้นส่วนของข้อความ นักพัฒนาสามารถลดปริมาณหน่วยความจำที่จำเป็นในการจัดเก็บข้อความอีเมลหรือเพิ่มประสิทธิภาพแบนด์วิดท์ของเครือข่ายเมื่อส่งข้อความได้
เพื่อวัตถุประสงค์นี้ ได้ออกแบบเมธอดที่มีการโอเวอร์โหลดสองเมธอดไว้แล้ว:
-
MapiMessageItemBase.SetBodyContent(string content, BodyContentType contentType, bool compression): เมธอดนี้ให้คุณตั้งค่าเนื้อหาส่วนข้อความโดยใช้สตริงที่ระบุและระบุประเภทเนื้อหา (เช่น plain text, HTML ฯลฯ). พารามิเตอร์ compression ที่เป็นตัวเลือกเป็นค่าที่ระบุว่าควรบีบอัดเนื้อหาโดยใช้การบีบอัด RTF หรือไม่. หากพารามิเตอร์ compression เป็น true เนื้อหาจะถูกบีบอัด ทำให้ขนาดข้อความเล็กลง.
-
MapiMessageItemBase.SetBodyRtf(string content, bool compression): เมธอดนี้ตั้งค่าเนื้อหาของส่วนข้อความในรูปแบบ RTF โดยพารามิเตอร์ content เป็นสตริงที่เป็นเนื้อหา RTF ที่จะตั้งเป็นส่วนข้อความของเมสเซจ. เช่นเดียวกับเมธอดก่อนหน้า พารามิเตอร์ compression กำหนดว่าจะใช้การบีบอัด RTF กับเนื้อหาหรือไม่. หาก compression เป็น true เนื้อหา RTF จะถูกบีบอัดเพื่อลดขนาด.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งค่า body เป็น html และรักษาการบีบอัดไว้:
var htmlBody = "<html><body><h1>This is the body content</h1></body></html>";
var msg = new MapiMessage("from@domain.com", "to@domain.com", "subject", "body");
// set the html body and keep it compressed
// this will reduce the message size
msg.SetBodyContent(htmlBody, BodyContentType.Html, true);
ยังมี MapiConversionOptions.UseBodyCompression คุณสมบัติ. เมื่อเปิดใช้งานคุณสมบัตินี้ การบีบอัดเนื้อหา RTF จะถูกใช้ระหว่างการแปลง MailMessage เป็น MapiMessage ทำให้ไฟล์ MSG มีขนาดเล็กลง. ตัวอย่างโค้ดด้านล่างแสดงให้เห็น:
var message = MailMessage.Load(fileName);
var options = new MapiConversionOptions();
options.UseBodyCompression = true;
var msg = MapiMessage.FromMailMessage(message, options);
บันทึกข้อความในสถานะฉบับร่าง
อีเมลจะถูกบันทึกเป็นแบบร่างเมื่อมีการเริ่มแก้ไขแต่ต้องการกลับมาทำต่อในภายหลัง Aspose.Email รองรับการบันทึกข้อความอีเมลในสถานะแบบร่างโดยการตั้งค่าสถานะแฟล็กของข้อความ ด้านล่างเป็นตัวอย่างโค้ดเพื่อบันทึกข้อความอีเมล Outlook (MSG) เป็นแบบร่าง.
// Change properties of an existing MSG file
string strExistingMsg = @"message.msg";
// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.Load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.Subject += "NEW SUBJECT (updated by Aspose.Email)";
msg.HtmlBody += "NEW BODY (updated by Aspose.Email)";
// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.FromMailMessage(msg);
mapiMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.Save(dataDir + "SavingMessageInDraftStatus_out.msg");