Đọc tệp Outlook Message (MSG)
Mẹo di chuyển của chúng tôi cho thấy cách các sản phẩm Aspose có thể được sử dụng để cải thiện ứng dụng của bạn và giải phóng bạn khỏi phụ thuộc vào tự động hoá truyền thống.
Mẹo di chuyển này cho thấy cách đọc tệp tin tin nhắn Microsoft Outlook và hiển thị nội dung trên màn hình bằng cả Tự động Hoá Microsoft Office và Aspose.Email mã. Mã mẫu dưới đây chỉ hiển thị nội dung trên console để bạn có ý tưởng về cách hoạt động. Sử dụng các đoạn mã trong ứng dụng Windows, web hoặc các ứng dụng khác của bạn.
Tự động Hoá Office
Để sử dụng các đối tượng Tự động Hoá Office cho Microsoft Outlook, bạn cần thêm tham chiếu tới Microsoft Office và thư viện Microsoft Office Interop cho Outlook vào dự án của mình.
Mẫu lập trình
C#
// Add the namespaces
using Microsoft.Office;
using Microsoft.Office.Interop.Outlook;
// Create a new Application Class
Application app = new Application();
// Create a MailItem object
MailItem item = (MailItem)outlook.CreateItemFromTemplate(@"d:\temp\test.msg", Type.Missing);
// Access different fields of the message
System.Console.WriteLine(string.Format("Subject:{0}", item.Subject));
System.Console.WriteLine(string.Format("Sender Email Address:{0}", item.SenderEmailAddress));
System.Console.WriteLine(string.Format("SenderName:{0}", item.SenderName));
System.Console.WriteLine(string.Format("TO:{0}", item.To));
System.Console.WriteLine(string.Format("CC:{0}", item.CC));
System.Console.WriteLine(string.Format("BCC:{0}", item.BCC));
System.Console.WriteLine(string.Format("Html Body:{0}", item.HTMLBody));
System.Console.WriteLine(string.Format("Text Body:{0}", item.Body));
Aspose.Email cho Java
Đoạn mã sau thực hiện cùng việc như mã ở trên bằng cách sử dụng Aspose.Email cho Java.
Để truy cập Aspose.Email Outlook các đối tượng, bạn cần thêm tham chiếu đến Aspose.Email trong dự án của mình.
Mẫu lập trình
// Create a new object of type MapiMessage
MapiMessage msg = MapiMessage.fromFile("d:\\temp\\test.msg");
// Access the fields of the message
System.out.println("Subject: " + msg.getSubject());
System.out.println("Sender Email Address: " + msg.getSenderEmailAddress());
System.out.println("SenderName:{0}" + msg.getSenderName());
System.out.println("TO:{0}" + msg.getDisplayTo());
System.out.println("CC:{0}" + msg.getDisplayCc());
System.out.println("BCC:{0}" + msg.getDisplayBcc());
System.out.println("Html Body:{0}" + msg.getBodyHtml());
System.out.println("Text Body:{0}" + msg.getBody());
System.out.println("Rtf Body:{0}" + msg.getBodyRtf());