การโหลดและแยกวิเคราะห์ไฟล์ MSG

โดยใช้ Aspose.Email for .NET นักพัฒนาสามารถโหลดและแยกวิเคราะห์เนื้อหาจากไฟล์ข้อความ Outlook ได้

  • เพื่อโหลดไฟล์ MSG จากดิสก์ ใช้เมธอดสแตติก MapiMessage.Load เมธอดของ MapiMessage คลาสนี้ให้ฟังก์ชันการโหลดแบบสแตติกหลายตัวสำหรับสถานการณ์ต่าง ๆ
  • เพื่อแยกเนื้อหาไฟล์ MSG, MapiMessage เปิดเผยเมธอดและคุณสมบัติจำนวนหลายรายการ

ในบทความนี้คุณจะได้เรียนรู้วิธีโหลดและแยกวิเคราะห์ไฟล์ MSG เพื่อแสดงเนื้อหา ตัวอย่างโค้ดพร้อมขั้นตอนจะทำให้คุณเข้าใจชัดเจนว่าจะแสดงฟังก์ชันการโหลดและการแยกวิเคราะห์ไฟล์ Outlook MSG ในโปรเจคของคุณอย่างไร

ขั้นแรก ให้เรียนรู้การโหลดไฟล์ MSG จากไฟล์หรือจากสตรีม

โหลดไฟล์ MSG

โค้ดสแนปต่อไปนี้แสดงวิธีโหลดไฟล์ MSG.

// Create an instance of MapiMessage from file
var msg = MapiMessage.Load(@"message.msg");

// Get subject
Console.WriteLine("Subject:" + msg.Subject);

// Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress);

// Get body
Console.WriteLine("Body" + msg.Body);

// Get recipients information
Console.WriteLine("Recipient: " + msg.Recipients);

// Get attachments
foreach (var att in msg.Attachments)
{
    Console.Write("Attachment Name: " + att.FileName);
    Console.Write("Attachment Display Name: " + att.DisplayName);
}

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีใช้ MailMessage เพื่อโหลดข้อความในรูปแบบ MSG


var eml = MailMessage.Load("message.msg");

ควรทราบว่าข้อความที่ได้จะถูกแปลงเป็นรูปแบบ EML รวมถึงไฟล์แนบข้อความที่ฝังอยู่ ไม่ควรใช้วิธีการโหลดนี้หากต้องการคงคุณสมบัติดรูปแบบ msg เฉพาะของข้อความต้นฉบับ

เพื่อคงรูปแบบดั้งเดิมของไฟล์แนบข้อความที่ฝังอยู่ ให้ใช้ msgLoadOptions.PreserveEmbeddedMessageFormat คุณสมบัติ.


var msgLoadOptions = new MsgLoadOptions();
msgLoadOptions.PreserveEmbeddedMessageFormat = true;
var msg = MailMessage.Load(stream, msgLoadOptions);

โหลดจากสตรีม

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดไฟล์จากสตรีม.

// Create an instance of MapiMessage from file
byte[] bytes = File.ReadAllBytes(@"message.msg");

using (MemoryStream stream = new MemoryStream(bytes))
{
    stream.Seek(0, SeekOrigin.Begin);
    // Create an instance of MapiMessage from file
    MapiMessage msg = MapiMessage.Load(stream);

    // Get subject
    Console.WriteLine("Subject:" + msg.Subject);

    // Get from address
    Console.WriteLine("From:" + msg.SenderEmailAddress);

    // Get body
    Console.WriteLine("Body" + msg.Body);

}

แปลง EML เป็น MSG พร้อมคงรูปแบบ EML ที่ฝังไว้

ไฟล์ EML สามารถโหลดเข้าไปใน MapiMessage คลาสโดยการสร้าง MailMessage อ็อบเจ็กต์และส่งผ่านไปยัง MapiMessage.FromMailMessage เมธอด หากไฟล์ EML มีไฟล์ EML ฝังอยู่ ให้ใช้ MapiConversionOptions.PreserveEmbeddedMessageFormat เพื่อรักษารูปแบบของไฟล์ EML ที่ฝังอยู่ โค้ดสแนปด้านล่างแสดงวิธีโหลดไฟล์ EML เข้าไปใน MapiMessage พร้อมคงรูปแบบของไฟล์ EML ที่ฝังอยู่

// Load the EML file into a MailMessage object
var mailMessage = MailMessage.Load(emlFilePath);

// Set conversion options to preserve the format of embedded EML messages
var options = new MapiConversionOptions
    {
        PreserveEmbeddedMessageFormat = true
    };

// Convert MailMessage to MapiMessage, preserving embedded EML files
var mapiMessage = MapiMessage.FromMailMessage(mailMessage, options);

// Save the converted message in MSG format
mapiMessage.Save(msgFilePath);

แยกวิเคราะห์ไฟล์ข้อความ Outlook

Aspose.Email for .NET มีให้ MapiMessage คลาสที่ใช้เปิดและแยกไฟล์ MSG เนื่องจากอาจมีผู้รับหลายคนในไฟล์ MSG, MapiMessage คลาสเปิดเผย ผู้รับ คุณสมบัติที่คืนค่า MapiRecipientCollection ซึ่งเป็นคอลเลกชันของ MapiRecipient อ็อบเจกต์. MapiRecipient อ็อบเจกต์ยังเปิดเผยเมธอดสำหรับทำงานกับแอตทริบิวต์ของผู้รับ.

ลำดับขั้นตอนต่อไปนี้ใช้เพื่อบรรลุเป้าหมาย:

  1. สร้างอินสแตนซ์ของ MapiMessage คลาสโดยใช้ MapiMessage.Load เมธอดสเตติก.
  2. แสดงชื่อผู้ส่ง, หัวข้อ, และเนื้อหาจากไฟล์ MSG โดยใช้ ชื่อผู้ส่ง, Subject และ Body คุณสมบัติ.
  3. ใช้ ผู้รับ คุณสมบัติสำหรับรับอ้างอิงไปยังคอลเลกชันของ MapiRecipient อ็อบเจกต์ที่เชื่อมโยงกับไฟล์ MSG.
  4. วนลูปผ่าน MapiRecipientCollection คอลเลกชันเพื่อแสดงเนื้อหาสำหรับแต่ละ MapiRecipient อ็อบเจกต์ผ่านเมธอดสาธารณะของมัน.
//Instantiate an MSG file to load an MSG file from disk
var outlookMessageFile = MapiMessage.Load(dataDir + "message.msg");
//Display sender's name
Console.WriteLine("Sender Name : " + outlookMessageFile.SenderName);
//Display Subject
Console.WriteLine("Subject : " + outlookMessageFile.Subject);
//Display Body
Console.WriteLine("Body : " + outlookMessageFile.Body);
//Display Recipient's info
Console.WriteLine("Recipients : \n");

//Loop through the recipients collection associated with the MapiMessage object
foreach (var rcp in outlookMessageFile.Recipients)
{
	//Display recipient email address
	Console.WriteLine("Email : " + rcp.EmailAddress);
	//Display recipient name
	Console.WriteLine("Name : " + rcp.DisplayName);
	//Display recipient type
	Console.WriteLine("Recipient Type : " + rcp.RecipientType);
}