การจัดการข้อความ Outlook
บันทึกอีเมลเป็น HTML
Aspose.Email ทำให้สามารถบันทึกรีซอร์สของอีเมลด้วยเส้นทาง relative เมื่อส่งออกข้อความเป็นรูปแบบ HTML คุณลักษณะนี้ให้ความยืดหยุ่นมากขึ้นในวิธีการเชื่อมโยงรีซอร์สในไฟล์ HTML ที่ส่งออก ทำให้แชร์และแสดงอีเมลที่บันทึกไว้บนระบบต่าง ๆ ง่ายขึ้น เพื่อบันทึกรีซอร์สด้วยเส้นทาง relative ใช้ HtmlSaveOptions.UseRelativePathToResources property. ค่าเริ่มต้นของ property คือ false (รีซอร์สจะบันทึกด้วยเส้นทาง absolute). เมื่อตั้งค่าเป็น true, รีซอร์สจะบันทึกด้วยเส้นทาง relative.
ไฟล์ HTML ที่มีเส้นทาง relative จะพกพาได้ง่ายกว่าและสามารถดูได้อย่างถูกต้องไม่ว่ารูปแบบไฟล์ของสภาพแวดล้อมโฮสติ้งจะเป็นอย่างไร คุณสามารถเลือกใช้เส้นทาง absolute หรือ relative ตามความต้องการ คุณสามารถกำหนดเส้นทาง custom สำหรับรีซอร์สโดยใช้ ResourceHtmlRendering เหตุการณ์.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธี บันทึกอีเมลด้วยเส้นทาง relative เริ่มต้นไปยังรีซอร์ส:
var msg = MapiMessage.Load(sourceFileName);
var htmlSaveOptions = new HtmlSaveOptions
{
ResourceRenderingMode = ResourceRenderingMode.SaveToFile,
UseRelativePathToResources = true
};
msg.Save(Path.Combine("target_files"), htmlSaveOptions);
ในกรณีนี้, รีซอร์สจะถูกบันทึกในโฟลเดอร์ [html file name]_files, ในเส้นทางเดียวกับไฟล์ .html และ HTML จะอ้างอิงรีซอร์สผ่านเส้นทาง relative.
ตัวอย่างโค้ดด้านล่างนี้แสดงวิธี บันทึกด้วยเส้นทาง absolute ไปยังรีซอร์ส:
var msg = MapiMessage.Load(sourceFileName);
var htmlSaveOptions = new HtmlSaveOptions
{
ResourceRenderingMode = ResourceRenderingMode.SaveToFile,
UseRelativePathToResources = false
};
msg.Save(Path.Combine("target_files"), htmlSaveOptions);
เช่นเดียวกับกรณีแรก, รีซอร์สจะถูกบันทึกในโฟลเดอร์ [html file name]_files ตามค่าเริ่มต้น แต่ HTML จะอ้างอิงรีซอร์สโดยใช้เส้นทางแบบ absolute.
โดยใช้ ResourceHtmlRendering เหตุการณ์, คุณสามารถตั้งค่าเส้นทางสัมพันธ์หรือสัมบูรณ์แบบกำหนดเองสำหรับทรัพยากร. เมื่อปรับแต่งเส้นทางด้วย ResourceHtmlRendering ตัวจัดการเหตุการณ์, และเนื่องจาก UseRelativePathToResources เมื่อกำหนดเป็น true, คุณควรกำหนดเส้นทางสัมพันธ์ให้กับ PathToResourceFile คุณสมบัติสำหรับรับรองการอ้างอิงที่ถูกต้อง.
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธี กำหนดเส้นทางสัมพัทธ์แบบกำหนดเองโดยใช้เหตุการณ์ ResourceHtmlRendering
var msg = MapiMessage.Load(sourceFileName);
var htmlSaveOptions = new HtmlSaveOptions
{
ResourceRenderingMode = ResourceRenderingMode.SaveToFile,
UseRelativePathToResources = true
};
htmlSaveOptions.ResourceHtmlRendering += (o, args) =>
{
if (o is AttachmentBase attachment)
{
// Since UseRelativePathToResources = true, you should assign a relative path to the PathToResourceFile property.
args.PathToResourceFile = $@"images\{attachment.ContentType.Name}";
}
};
msg.Save(Path.Combine(targetPath, "A Day in the Park.html"), htmlSaveOptions);
แปลง MSG เป็น MIME Messages
Aspose.Email API มีความสามารถในการแปลงไฟล์ MSG เป็นข้อความ MIME โดยใช้ ToMailMessage เมธอด.
MapiMessage msg = new MapiMessage("sender@test.com","recipient1@test.com; recipient2@test.com","Test Subject","This is a body of message.");
var options = new MailConversionOptions();
options.ConvertAsTnef = true;
MailMessage mail = msg.ToMailMessage(options);
ตั้งค่า Timeout สำหรับการแปลงและโหลดข้อความ
คุณสมบัติดังต่อไปนี้จะช่วยให้คุณตั้งค่า timeout เป็นมิลลิวินาทีสำหรับกระบวนการแปลงและโหลด:
-
MailConversionOptions.Timeout property- จำกัดเวลาเป็นมิลลิวินาทีขณะแปลงข้อความ.
-
MailConversionOptions.TimeoutReached - เกิดขึ้นหากหมดเวลาในขณะแปลงเป็น MailMessage.
-
MsgLoadOptions.Timeout - จำกัดเวลาเป็นมิลลิโวลต์ขณะแปลงข้อความ.
-
MsgLoadOptions.TimeoutReached - เกิดขึ้นหากหมดเวลาในขณะแปลงเป็น MailMessage.
ตัวอย่างโค้ดด้านล่างนี้จะแสดงวิธีตั้งค่า timeout ขณะแปลงข้อความ:
var options = new MailConversionOptions();
// Set the timeout to 5 seconds
options.Timeout = 5000;
options.TimeoutReached += (object sender, EventArgs args) =>
{
string subj = (sender as MailMessage).Subject;
// Set a flag indicating the timeout was reached
isTimedOut = true;
};
var mailMessage = mapiMessage.ToMailMessage(options);
การแปลง MSG เป็น EML คงเนื้อหา RTF
การแปลงไฟล์ MSG เป็น EML โดยคงเนื้อหา RTF สามารถทำได้สองวิธี:
-
โดยใช้ MsgLoadOptions.PreserveRtfContent คุณสมบัติของ MsgLoadOptions class;
-
โดยใช้ MailConversionOptions.PreserveRtfContent คุณสมบัติของ MailConversionOptions class;
ทั้งสอง property จะได้รับหรือกำหนดค่าที่บ่งบอกว่าจะคงรักษาเนื้อหา rtf ใน MailMessage หรือไม่.
โค้ด snippet ต่อไปนี้แสดงวิธีแปลงไฟล์ MSG เป็น EML และคงรักษาเนื้อหา RTF:
var loadOptions = new MsgLoadOptions
{
PreserveRtfContent = true
};
var eml = MailMessage.Load("my.msg", loadOptions);
var conversionOptions = new MailConversionOptions
{
PreserveRtfContent = true
};
var msg = MapiMessage.Load("my.msg");
var eml = msg.ToMailMessage(conversionOptions);
จัดการไฟล์เทมเพลต Outlook (.OFT)
เทมเพลต Outlook มีประโยชน์มากเมื่อคุณต้องการส่งข้อความอีเมลที่คล้ายกันซ้ำ ๆ แทนการเตรียมข้อความจากศูนย์ทุกครั้ง, ขั้นแรกให้เตรียมข้อความใน Outlook แล้วบันทึกเป็น Outlook Template (OFT). หลังจากนั้น เมื่อใดก็ตามที่ต้องส่งข้อความ คุณสามารถสร้างจากเทมเพลตได้ ช่วยประหยัดเวลาในการเขียนข้อความเดิมในส่วนเนื้อหา หรือหัวเรื่อง, ตั้งค่าการจัดรูปแบบ ฯลฯ Aspose.Email’s MailMessage คลาสสามารถใช้เพื่อโหลดและอ่านไฟล์แม่แบบ Outlook (OFT) เมื่อโหลดแม่แบบ Outlook แล้วในอินสแตนซ์ของ MailMessage คลาส คุณสามารถอัปเดตผู้ส่ง, ผู้รับ, เนื้อหา, หัวข้อและคุณสมบัติอื่นๆ หลังจากอัปเดตคุณสมบัติแล้ว:
- ส่งอีเมลโดยใช้ SmtpClient คลาสหรือ
- บันทึกข้อความเป็น MSG และทำการอัปเดต/ตรวจสอบเพิ่มเติมโดยใช้ Microsoft Outlook.
ในตัวอย่างโค้ดด้านล่าง เรา:
- โหลดแม่แบบโดยใช้ MailMessage คลาส.
- อัปเดตคุณสมบัติบางอย่าง.
- บันทึกข้อความในรูปแบบ MSG.
โค้ด snippet ต่อไปนี้แสดงวิธีโหลดไฟล์ OFT, ปรับปรุงข้อความและบันทึกเป็นรูปแบบ MSG.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Load the Outlook template (OFT) file in MailMessage's instance
MailMessage message = MailMessage.Load(dataDir + "sample.oft", new MsgLoadOptions());
// Set the sender and recipients information
string senderDisplayName = "John";
string senderEmailAddress = "john@abc.com";
string recipientDisplayName = "William";
string recipientEmailAddress = "william@xzy.com";
message.Sender = new MailAddress(senderEmailAddress, senderDisplayName);
message.To.Add(new MailAddress(recipientEmailAddress, recipientDisplayName));
message.HtmlBody = message.HtmlBody.Replace("DisplayName", "<b>" + recipientDisplayName + "</b>");
// Set the name, location and time in email body
string meetingLocation = "<u>" + "Hall 1, Convention Center, New York, USA" + "</u>";
string meetingTime = "<u>" + "Monday, June 28, 2010" + "</u>";
message.HtmlBody = message.HtmlBody.Replace("MeetingPlace", meetingLocation);
message.HtmlBody = message.HtmlBody.Replace("MeetingTime", meetingTime);
// Save the message in MSG format and open in Office Outlook
MapiMessage msg = MapiMessage.FromMailMessage(message);
msg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
msg.Save(dataDir + "ReadAndWritingOutlookTemplateFile_out.msg");
บันทึกไฟล์ MSG เป็นเทมเพลต
ส่วนโค้ดต่อไปนี้จะแสดงวิธีบันทึกไฟล์ Outlook MSG เป็นแม่แบบ
using (MapiMessage mapi = new MapiMessage("test@from.to", "test@to.to", "template subject", "Template body"))
{
string oftMapiFileName = "mapiToOft.oft";
mapi.SaveAsTemplate(oftMapiFileName);
}
กำหนดประเภทข้อความ MAPI (OFT หรือ MSG)
เมื่อต้องโหลดวัตถุ MapiMessage จากไฟล์ คุณอาจต้องตรวจสอบว่าเมสเซจที่โหลดเป็นไฟล์เทมเพลตหรือไฟล์อีเมลปกติ โดยการใช้ IsTemplate คุณสมบัติของ MapiMessage คลาสนี้ช่วยให้คุณตรวจจับได้อย่างแม่นยำว่าอีเมลเป็นเทมเพลตหรือไม่ ความสามารถนี้มีประโยชน์เมื่อต้องจัดการและประมวลผลไฟล์อีเมลประเภทต่าง ๆ ในแอปพลิเคชันและระบบ.
ตัวอย่างโค้ดด้านล่างแสดงวิธีตรวจสอบว่า MapiMessage เป็น OFT หรือ MSG:
var msg = MapiMessage.Load("message.msg");
var isOft = msg.IsTemplate; // returns false
var oft = MapiMessage.Load("message.oft");
var isOftTemplate = oft.IsTemplate; // returns true
บันทึก MapiMessage หรือ MailMessage ในรูปแบบ OFT
นี้ SaveOptions คลาสนี้อนุญาตให้คุณระบุตัวเลือกเพิ่มเติมเมื่อบันทึก MailMessage หรือ MapiMessage ลงในรูปแบบเฉพาะ.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีบันทึกข้อความเป็นรูปแบบ OFT:
// Save the MailMessage to OFT format
using (var eml = MailMessage.Load("message.eml"))
{
eml.Save("message.oft", SaveOptions.DefaultOft);
// or alternative way #2
var saveOptions = new MsgSaveOptions(MailMessageSaveType.OutlookTemplateFormat);
eml.Save("message.oft", saveOptions);
// or alternative way #3
saveOptions = SaveOptions.CreateSaveOptions(MailMessageSaveType.OutlookTemplateFormat);
eml.Save("message.oft", saveOptions);
}
// Save the MapiMessage to OFT format
using (var msg = MapiMessage.Load("message.msg"))
{
msg.Save("message.oft", SaveOptions.DefaultOft);
// or alternative way #2
var saveOptions = new MsgSaveOptions(MailMessageSaveType.OutlookTemplateFormat);
msg.Save("message.oft", saveOptions);
// or alternative way #3
saveOptions = SaveOptions.CreateSaveOptions(MailMessageSaveType.OutlookTemplateFormat);
msg.Save("message.oft", saveOptions);
}
จัดการข้อความที่มีลายเซ็นดิจิทัล
Aspose.Email ใช้การทำงานของอัลกอริทึมอ็อบเจกต์อีเมล S/MIME อย่างครบถ้วน ทำให้ API มีความสามารถเต็มที่ในการคงรักษาลายเซ็นดิจิทัลขณะแปลงข้อความระหว่างรูปแบบต่าง ๆ.
คงรักษาลายเซ็นขณะแปลงจาก EML เป็น MSG
Aspose.Email คงลายเซ็นดิจิทัลขณะแปลงจาก EML เป็น MSG ส่วนโค้ดต่อไปนี้แสดงวิธีแปลงจาก EML ไปยัง MSG.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Load mail message
MailMessage message = MailMessage.Load(dataDir + "Message.eml", new EmlLoadOptions());
// Save as MSG
message.Save(dataDir + "ConvertEMLToMSG_out.msg", SaveOptions.DefaultMsgUnicode);
แปลงข้อความ S/MIME จาก MSG เป็น EML
Aspose.Email คงลายเซ็นดิจิทัลขณะแปลงจาก MSG เป็น EML ตามที่แสดงในส่วนโค้ดต่อไปนี้.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Load the MSG file
MailMessage msg = MailMessage.Load(dataDir + "message.msg", new MsgLoadOptions());
// Save as EML
msg.Save(dataDir + "ConvertMIMEMessagesFromMSGToEML_out.eml", SaveOptions.DefaultEml);
ตรวจสอบลายเซ็นของอีเมลที่ปลอดภัย
คุณลักษณะต่อไปนี้พร้อมใช้งานเพื่อตรวจสอบลายเซ็นของวัตถุ MapiMessage.
- SecureEmailManager คลาสสำหรับตรวจสอบลายเซ็นของอีเมลที่ปลอดภัย.
- SmimeResult คลาสสำหรับเก็บผลลัพธ์ของการตรวจสอบ.
- SecureEmailManager.CheckSignature(MapiMessage msg) เมธอด.
- SecureEmailManager.CheckSignature(MapiMessage msg, X509Certificate2 certificateForDecrypt) เมธอด.
- SecureEmailManager.CheckSignature(MapiMessage msg, X509Certificate2 certificateForDecrypt, X509Store store) เมธอด.
ตัวอย่างโค้ดด้านล่างแสดงวิธีนำคุณลักษณะเหล่านี้ไปใช้ในโครงการของคุณ:
ตรวจสอบลายเซ็นของข้อความโดยไม่ต้องให้ใบรับรอง:
var msg = MapiMessage.Load(fileName);
var result = new SecureEmailManager().CheckSignature(msg);
ตรวจสอบลายเซ็นของข้อความเข้ารหัสโดยให้ใบรับรองสำหรับการถอดรหัสและ store ที่เก็บใบรับรองที่ใช้ตรวจสอบ:
var certFileName = "cert.pfx";
var cert = new X509Certificate2(certFileName, "pass");
var msg = MapiMessage.Load(fileName);
var store = new X509Store();
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
var result = new SecureEmailManager().CheckSignature(msg, cert, store);
ลบลายเซ็นจาก MapiMessages
เพื่อความเข้ากันได้ที่ดียิ่งขึ้น, MapiMessage.RemoveSignature เมธอดและ MapiMessage.IsSigned คุณสมบัติเหล่านี้ใช้เพื่อลบลายเซ็นดิจิทัลออกจากข้อความ.
โค้ดต่อไปนี้แสดงวิธีนำคุณลักษณะเหล่านี้ไปใช้ในโครงการของคุณ:
var msg = MapiMessage.Load(fileName);
if (msg.IsSigned)
{
var unsignedMsg = msg.RemoveSignature();
}
ถอดรหัส MapiMessages ด้วยใบรับรอง
หากคุณมีข้อความ MAPI ที่เข้ารหัสและต้องการถอดรหัสด้วยคีย์ส่วนตัวที่เก็บในใบรับรอง คุณลักษณะของ Aspose.Email ต่อไปนี้อาจเป็นประโยชน์:
- MapiMessage.IsEncrypted - รับค่าที่แสดงว่าข้อความถูกเข้ารหัสหรือไม่.
- MapiMessage.Decrypt() - ถอดรหัสข้อความนี้ (วิธีการค้นหาระยะผู้ใช้และคอมพิวเตอร์ใน My stores เพื่อหาใบรับรองและคีย์ส่วนตัวที่เหมาะสม).
- MapiMessage.Decrypt(X509Certificate2 certificate) - ถอดรหัสข้อความนี้ด้วยใบรับรอง.
ส่วนโค้ดต่อไปนี้แสดงวิธีทำงานกับข้อความ MAPI ที่เข้ารหัส:
var privateCert = new X509Certificate2(privateCertFile, "password");
var msg = MapiMessage.Load("encrypted.msg");
if (msg.IsEncrypted)
{
var decryptedMsg = msg.Decrypt(privateCert);
}
ตั้งค่าหมวดหมู่สีสำหรับไฟล์ MSG
ประเภทสีทำเครื่องหมายข้อความอีเมลเพื่อบ่งบอกความสำคัญหรือประเภทบางอย่าง Microsoft Outlook ให้ผู้ใช้กำหนดประเภทสีเพื่อแยกแยะอีเมล เพื่อจัดการประเภทสี ให้ใช้ FollowUpManager. มีฟังก์ชันเช่น AddCategory, RemoveCategory, ClearCategories และ GetCategories.
- AddCategory รับค่า MapiMessage และสตริงประเภทสี เช่น "Purple Category" หรือ "Red Category" เป็นอาร์กิวเมนต์.
- RemoveCategory รับค่า MapiMessage และสตริงประเภทสีที่ต้องการลบออกจากข้อความ
- ClearCategories ใช้เพื่อลบประเภทสีทั้งหมดจากข้อความ.
- GetCategories ใช้เพื่อดึงประเภทสีทั้งหมดจากข้อความที่ระบุ.
ตัวอย่างต่อไปนี้ทำงานตามขั้นตอนที่ระบุด้านล่าง:
- เพิ่มประเภทสี.
- เพิ่มประเภทสีอื่น.
- ดึงรายการของทุกประเภท.
- ลบทุกประเภทออกทั้งหมด.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
MapiMessage msg = MapiMessage.FromFile(dataDir + "message1.msg");
// Add Two category
FollowUpManager.AddCategory(msg, "Purple Category");
FollowUpManager.AddCategory(msg, "Red Category");
// Retrieve the list of available categories
IList<string> categories = FollowUpManager.GetCategories(msg);
// Remove the specified category and then Clear all categories
FollowUpManager.RemoveCategory(msg, "Red Category");
FollowUpManager.ClearCategories(msg);
เข้าถึงข้อมูลติดตามในไฟล์ MSG
Aspose.Email API ให้ความสามารถในการเข้าถึงข้อมูลติดตามจากข้อความที่ส่งหรือรับ สามารถดึงข้อมูลการอ่าน การรับการจัดส่งและผลการโหวตจากไฟล์ข้อความได้
ดึงข้อมูลการอ่านและใบรับรองการส่ง
ส่วนโค้ดต่อไปนี้จะแสดงให้คุณเห็นวิธีการเรียกคืนข้อมูลการรับและการยืนยันการจัดส่ง
MapiMessage msg = MapiMessage.FromFile(dataDir + @"TestMessage.msg");
foreach (MapiRecipient recipient in msg.Recipients)
{
Console.WriteLine(string.Format("Recipient: {0}", recipient.DisplayName));
// Get the PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY property
Console.WriteLine(string.Format("Delivery time: {0}", recipient.Properties[MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY].GetDateTime()));
// Get the PR_RECIPIENT_TRACKSTATUS_TIME_READ property
Console.WriteLine(string.Format("Read time: {0}", recipient.Properties[MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_READ].GetDateTime()));
Console.WriteLine();
}
สร้างข้อความส่งต่อและตอบกลับ
Aspose.Email API มีความสามารถในการสร้างและจัดรูปแบบข้อความส่งต่อและตอบกลับ. ReplyMessageBuilder และ ForwardMessageBuilder คลาสของ API ใช้สร้างข้อความ Reply และ Forward ตามลำดับ ข้อความ Reply หรือ Forward สามารถกำหนดให้สร้างโดยใช้โหมดใดก็ได้ของ OriginalMessageAdditionMode enum. enum นี้มีค่าต่อไปนี้:
- OriginalMessageAdditionMode.None - ข้อความต้นฉบับจะไม่ถูกรวมในข้อความตอบกลับ.
- OriginalMessageAdditionMode.Attachment - ข้อความต้นฉบับจะรวมเป็นไฟล์แนบในข้อความตอบกลับ
- OriginalMessageAdditionMode.Textpart - ข้อความต้นฉบับจะถูกรวมเป็นข้อความในเนื้อหาของข้อความตอบกลับ
สร้างข้อความตอบกลับ
โค้ดสแนปต่อไปนี้จะแสดงวิธีสร้างข้อความตอบกลับ.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
MapiMessage originalMsg = MapiMessage.FromFile(dataDir + "message1.msg");
ReplyMessageBuilder builder = new ReplyMessageBuilder();
// Set ReplyMessageBuilder Properties
builder.ReplyAll = true;
builder.AdditionMode = OriginalMessageAdditionMode.Textpart;
builder.ResponseText = "<p><b>Dear Friend,</b></p> I want to do is introduce my co-author and co-teacher. <p><a href=\"www.google.com\">This is a first link</a></p><p><a href=\"www.google.com\">This is a second link</a></p>";
MapiMessage replyMsg = builder.BuildResponse(originalMsg);
replyMsg.Save(dataDir+ "reply_out.msg");
สร้างข้อความส่งต่อ
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างข้อความต่อ (forward).
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
MapiMessage originalMsg = MapiMessage.FromFile(dataDir +"message1.msg");
ForwardMessageBuilder builder = new ForwardMessageBuilder();
builder.AdditionMode = OriginalMessageAdditionMode.Textpart;
MapiMessage forwardMsg = builder.BuildResponse(originalMsg);
forwardMsg.Save(dataDir + "forward_out.msg");