Tạo và Lưu các Tệp MSG

Aspose.Email hỗ trợ tạo các tệp Outlook message (MSG). Bài viết này giải thích cách:

  • Tạo tin nhắn MSG.
  • Tạo tin nhắn MSG có đính kèm.
  • Tạo tin nhắn MSG với phần thân RTF.
  • Lưu tin nhắn dưới dạng bản nháp.
  • Làm việc với nén phần thân.

Tạo và Lưu Tin Nhắn Outlook

Cái MailMessage lớp có lưu phương thức có thể lưu các tệp Outlook MSG vào đĩa hoặc stream. Các đoạn mã dưới đây tạo một thể hiện của MailMessage lớp, thiết lập các thuộc tính như from, to, subject và body. Phương thức lưu phương thức nhận tên tệp làm đối số. Ngoài ra, các Tin nhắn Outlook có thể được tạo với một phần thân RTF nén sử dụng MapiConversionOptions.

  1. Tạo một thể hiện mới của MailMessage lớp và thiết lập các thuộc tính From, To, Subject và Body.
  2. Gọi MapiMessage lớp fromMailMessage phương thức nhận đối tượng của MailMessage kiểu. Phương thức fromMailMessage phương thức chuyển đổi MailMessage vào một MapiMessage (MSG).
  3. Gọi MapiMessage.save phương thức để lưu tệp MSG.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("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);

Tạo Tệp MSG với Đính Kèm

Trong ví dụ trên, chúng tôi đã tạo một tệp MSG đơn giản. Aspose.Email cũng hỗ trợ lưu các tệp tin nhắn kèm đính kèm. Tất cả những gì bạn cần làm là thêm các đính kèm vào MailMessage đối tượng. Thêm tệp đính kèm bằng cách gọi phương thức addItem trên MailMessage.Attachments bộ sưu tập.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

String[] files = new String[2];
files[0] = "attachment.doc";
files[1] = "attachment.png";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");

// Add the attachments
for (String strFileName : files)
{
    mailMsg.getAttachments().addItem(new Attachment(strFileName));
}

// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
String strMsgFile = "CreateMessagesWithAttachments.msg";
outlookMsg.save(dataDir + strMsgFile);

Tạo Tệp MSG với Phần Thân RTF

Bạn cũng có thể tạo các tệp Outlook Message (MSG) với phần thân văn bản định dạng (RTF) bằng Aspose.Email. Phần thân RTF hỗ trợ định dạng văn bản. Tạo một bằng cách thiết lập MailMessage.HtmlBody thuộc tính. Khi bạn chuyển đổi một MailMessage đối tượng thành một MapiMessage trong trường hợp này, phần thân HTML được chuyển thành RTF. Cách này giúp định dạng của phần thân email được bảo tồn.

Ví dụ sau tạo một tệp MSG với phần thân RTF. Có một tiêu đề, định dạng in đậm và gạch dưới được áp dụng trong phần thân HTML. Định dạng này được giữ lại khi HTML được chuyển sang RTF.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setHtmlBody("<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");

Lưu Tin Nhắn ở Trạng Thái Bản Nháp

Email được lưu dưới dạng bản nháp khi người dùng đã bắt đầu chỉnh sửa nhưng muốn quay lại để hoàn thành sau. Aspose.Email hỗ trợ lưu tin nhắn email ở trạng thái bản nháp bằng cách thiết lập cờ tin nhắn. Dưới đây là đoạn mã mẫu để lưu một tin nhắn email Outlook (MSG) dưới dạng bản nháp.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// 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.setSubject(msg.getSubject() + " NEW SUBJECT (updated by Aspose.Email)");
msg.setHtmlBody(msg.getHtmlBody() + " NEW BODY (udpated 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");

Hệ quả của Nén Phần Thân

Phương pháp nén phần thân RTF có thể được sử dụng để tạo MSG có kích thước nhỏ hơn. Tuy nhiên, điều này làm chậm tốc độ tạo. Để tạo tin nhắn nhanh hơn, đặt cờ thành false. Cờ này, ngược lại, ảnh hưởng đến các PST được tạo: các tệp MSG nhỏ hơn dẫn đến PST nhỏ hơn, và các tệp MSG lớn dẫn đến quá trình tạo PST chậm hơn.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";

MailMessage message = MailMessage.load(fileName);
MapiConversionOptions options = new MapiConversionOptions();
options.setUseBodyCompression(true);
MapiMessage ae_mapi = MapiMessage.fromMailMessage(message, options);