Quản Lý Các Tệp Tin Nhắn với Aspose.Email.Outlook

Chuyển Đổi MSG sang Tin MIME

Aspose.Email API cung cấp khả năng chuyển đổi tệp MSG sang tin MIME bằng cách sử dụng toMailMessage phương thức.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = new MapiMessage(
                            "sender@test.com",
                            "recipient1@test.com; recipient2@test.com",
                            "Test Subject",
                            "This is a body of message.");
MailConversionOptions options = new MailConversionOptions();
options.setConvertAsTnef(true);
MailMessage mail = msg.toMailMessage(options);

Chuyển Đổi MSG sang EML Giữ Phần Thân RTF

API cung cấp các phương thức sau để giữ phần thân RTF khi chuyển đổi MSG sang EML:

Các mẫu mã dưới đây minh họa cách giữ phần thân rtf trong MailMessage:

MsgLoadOptions options = new MsgLoadOptions();
options.setPreserveRtfContent(true);
MailMessage message = MailMessage.load("fileName", options);
MapiMessage mapi = MapiMessage.load("fileName");
MailConversionOptions options = new MailConversionOptions();
options.setPreserveRtfContent(true);
MailMessage message = mapi.toMailMessage(options);

Chuyển Đổi MSG sang MHTML Giữ Tiêu Đề Danh Mục

Aspose.Email API cung cấp khả năng thêm tiêu đề danh mục khi chuyển đổi tin nhắn sang MHTML. Tính năng này được xác định bởi MhtSaveOptions lớp như một tùy chọn bổ sung khi lưu MailMessage sang định dạng Mhtml.

Mẫu mã dưới đây minh họa cách tạo tệp MHT (MHTML) từ đối tượng MapiMessage, tùy chỉnh định dạng và tiêu đề của tệp MHT bằng MhtSaveOptions, đặt danh mục cho tin email và sau đó sửa đổi mẫu định dạng và tiêu đề hiển thị cho tệp MHT trước khi lưu.

 MapiMessage msg = new MapiMessage("from@aaa.com", "to@aaa.com", "subj", "body");

msg.setCategories(new String[] { "Urgently", "Important" });

MhtSaveOptions saveOptions = new MhtSaveOptions();

saveOptions.getFormatTemplates().set_Item(MhtTemplateName.CATEGORIES,

    saveOptions.getFormatTemplates().get_Item(MhtTemplateName.CATEGORIES).replace("Categories", "Les catégories"));

saveOptions.getRenderingHeaders().add(MhtTemplateName.CATEGORIES);

msg.save("fileName.mhtml", saveOptions);

Đọc và Ghi Tệp Mẫu Outlook (.OFT)

Mẫu Outlook rất hữu ích khi bạn muốn gửi một email tương tự lần lượt. Thay vì chuẩn bị tin nhắn từ đầu mỗi lần, đầu tiên, chuẩn bị tin nhắn trong Outlook và lưu dưới dạng Mẫu Outlook (OFT). Sau đó, khi cần gửi tin, bạn có thể tạo nó từ mẫu, tiết kiệm thời gian viết lại cùng nội dung trong phần thân hoặc tiêu đề, thiết lập định dạng, v.v. Aspose.Email MailMessage lớp có thể được sử dụng để tải và đọc tệp mẫu Outlook (OFT). Khi mẫu Outlook đã được tải trong một thể hiện của MailMessage lớp, bạn có thể cập nhật người gửi, người nhận, nội dung, tiêu đề và các thuộc tính khác. Sau khi cập nhật các thuộc tính:

  • Gửi email bằng cách sử dụng SmtpClient lớp hoặc
  • Lưu tin nhắn dưới dạng MSG và thực hiện các cập nhật/kiểm tra tiếp theo bằng Microsoft Outlook.

Trong các mẫu mã dưới đây, chúng tôi:

  1. Tải mẫu bằng cách sử dụng MailMessage lớp.
  2. Cập nhật một số thuộc tính.
  3. Lưu tin nhắn ở định dạng MSG.

Đoạn mã dưới đây cho bạn thấy cách tải tệp OFT, cập nhật tin nhắn và lưu nó ở định dạng 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/";

// 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.setSender(new MailAddress(senderEmailAddress, senderDisplayName));
message.getTo().addMailAddress(new MailAddress(recipientEmailAddress, recipientDisplayName));
message.setHtmlBody(message.getHtmlBody().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.setHtmlBody(message.getHtmlBody().replace("MeetingPlace", meetingLocation));
message.setHtmlBody(message.getHtmlBody().replace("MeetingTime", meetingTime));

// Save the message in MSG format and open in Office Outlook
MapiMessage mapimessage = MapiMessage.fromMailMessage(message);
mapimessage.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapimessage.save(dataDir + "ReadAndWritingOutlookTemplateFile_out.msg");

Lưu Tệp MSG Outlook dưới dạng Mẫu

Đoạn mã dưới đây cho bạn thấy cách lưu tệp MSG outlook dưới dạng mẫu.

// 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/";

try (MapiMessage mapi = new MapiMessage("test@from.to", "test@to.to", "template subject", "Template body")) {
    mapi.saveAsTemplate(dataDir + "mapiToOft.msg");
}

Đặt Danh Mục Màu cho Tệp MSG Outlook

Một danh mục màu đánh dấu một tin email với một mức độ quan trọng hoặc loại nào đó. Microsoft Outlook cho phép người dùng gán các danh mục màu để phân biệt email. Để xử lý danh mục màu, sử dụng FollowUpManager. Nó chứa các hàm như addCategory, removeCategory, clearCategories và getCategories.

  • addCategory nhận MapiMessage và chuỗi danh mục màu, ví dụ "Purple Category" hoặc "Red Category" làm đối số.
  • removeCategory nhận MapiMessage và chuỗi danh mục màu cần xóa khỏi tin nhắn.
  • clearCategories được sử dụng để xóa tất cả các danh mục màu khỏi tin nhắn.
  • getCategories được sử dụng để lấy tất cả các danh mục màu từ một tin nhắn cụ thể.

Ví dụ dưới đây thực hiện các công việc như sau:

  1. Thêm một danh mục màu.
  2. Thêm một danh mục màu khác.
  3. Lấy danh sách tất cả các danh mục.
  4. Xóa tất cả các danh mục.
// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");

// Add Two category
FollowUpManager.addCategory(msg, "Purple Category");
FollowUpManager.addCategory(msg, "Red Category");

// Retrieve the list of available categories
IList categories = FollowUpManager.getCategories(msg);

// Remove the specified category and then Clear all categories
FollowUpManager.removeCategory(msg, "Red Category");
FollowUpManager.clearCategories(msg);

Truy Cập Thông Tin Theo Dõi từ Tệp MSG

Aspose.Email API cung cấp khả năng truy cập thông tin theo dõi từ một tin nhắn đã gửi hoặc đã nhận. Nó có thể lấy thông tin Đã Đọc, Biên Nhận Đã Giao và kết quả bỏ phiếu từ tệp tin tin nhắn.

Lấy Thông Tin Biên Nhận Đã Đọc và Đã Giao

Đoạn mã dưới đây cho bạn thấy cách lấy thông tin biên nhận đã đọc và đã giao.

// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");
for (MapiRecipient recipient : msg.getRecipients()) {
    System.out.println("Recipient: " + recipient.getDisplayName());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY property
    System.out.println("Delivery time: " + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY).getDateTime());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_READ property
    System.out.println("Read time" + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_READ).getDateTime());
}

Tạo Tin Nhắn Forward và Reply

Aspose.Email API cung cấp khả năng tạo và định dạng các tin nhắn forward và reply. Các ReplyMessageBuilderForwardMessageBuilder các lớp của API được sử dụng để tạo tin nhắn Reply và Forward tương ứng. Một tin nhắn Reply hoặc Forward có thể được chỉ định tạo bằng bất kỳ chế độ nào của OriginalMessageAdditionMode enum. Enum này có các giá trị sau:

  • OriginalMessageAdditionMode.None - Tin nhắn gốc không được đưa vào trong tin nhắn trả lời.
  • OriginalMessageAdditionMode.Attachment - Tin nhắn gốc được đưa vào dưới dạng đính kèm trong tin nhắn trả lời
  • OriginalMessageAdditionMode.Textpart - Tin nhắn gốc được đưa vào dưới dạng văn bản trong phần thân tin nhắn trả lời

Tạo Tin Nhắn Trả Lời

Đoạn mã dưới đây cho bạn cách tạo tin nhắn trả lời.

// 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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ReplyMessageBuilder builder = new ReplyMessageBuilder();

// Set ReplyMessageBuilder Properties
builder.setReplyAll(true);
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
builder.setResponseText(
        "<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");

Tạo Tin Nhắn Chuyển Tiếp

Đoạn mã dưới đây cho bạn cách tạo tin nhắn chuyển tiế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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ForwardMessageBuilder builder = new ForwardMessageBuilder();
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
MapiMessage forwardMsg = builder.buildResponse(originalMsg);
forwardMsg.save(dataDir + "forward_out.msg");

Bảo Tồn Ngày Trống khi Chuyển Đổi Tin Nhắn

MapiConversionOptions.setPreserveEmptyDates(boolean) thuộc tính chỉ ra liệu cần giữ lại các ngày trống khi chuyển đổi tin nhắn hay không. API này xuất hiện trong Aspose.Email 21.5. Đoạn mã dưới đây cho bạn cách bảo tồn các ngày trống.

MailMessage mailMessage = MailMessage.load("message.eml");
System.out.println(mailMessage.getDate()); // zero date
MapiConversionOptions mco = MapiConversionOptions.getUnicodeFormat();
// keep empty dates when converting a message
mco.setPreserveEmptyDates(true);
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage, mco);
System.out.println(mapiMessage.getClientSubmitTime()); // zero date
// check zero date
if (mapiMessage.getClientSubmitTime().equals(JavaHelper.ZERO_DATE))
    System.out.println("ZERO DATE");