การจัดการไฟล์ข้อความด้วย Aspose.Email.Outlook

การแปลง MSG เป็นข้อความ MIME

Aspose.Email API มีความสามารถในการแปลงไฟล์ MSG เป็นข้อความ MIME โดยใช้ toMailMessage เมธอด.

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

การแปลง MSG เป็น EML โดยคงเนื้อหา RTF

API มีเมธอดต่อไปนี้เพื่อคงเนื้อหา RTF ขณะแปลง MSG เป็น EML:

  • MsgLoadOptions.PreserveRtfContent - รับหรือกำหนดค่าที่บ่งบอกว่าจะคงเนื้อหา rtf ใน MailMessage หรือไม่.
  • MailConversionOptions.PreserveRtfContent - รับหรือกำหนดค่าที่บ่งบอกว่าจะคงเนื้อหา rtf ใน MailMessage หรือไม่.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีคงเนื้อหา rtf ใน 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);

การแปลง MSG เป็น MHTML โดยคงส่วนหัวประเภท

Aspose.Email API มีความสามารถในการเพิ่มส่วนหัวประเภทขณะแปลงข้อความเป็น MHTML ฟีเจอร์นี้กำหนดโดย MhtSaveOptions คลาสเป็นตัวเลือกเพิ่มเติมเมื่อบันทึก MailMessage เป็นรูปแบบ Mhtml.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างไฟล์ MHT (MHTML) จากวัตถุ MapiMessage ปรับแต่งรูปแบบและส่วนหัวของไฟล์ MHT ด้วย MhtSaveOptions ตั้งค่าประเภทสำหรับข้อความอีเมล แล้วแก้ไขแม่แบบรูปแบบและส่วนหัวการเรนเดอร์สำหรับไฟล์ MHT ก่อนบันทึก

 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);

การอ่านและเขียนไฟล์แม่แบบ Outlook (.OFT)

แม่แบบ Outlook มีประโยชน์มากเมื่อคุณต้องการส่งข้อความอีเมลที่คล้ายกันซ้ำๆ แทนการเตรียมข้อความตั้งแต่เริ่มต้นทุกครั้ง ให้เตรียมข้อความใน Outlook ก่อนแล้วบันทึกเป็นแม่แบบ Outlook (OFT) จากนั้นเมื่อใดก็ตามที่คุณต้องการส่งข้อความ คุณสามารถสร้างจากแม่แบบนั้นได้ ช่วยประหยัดเวลาในการพิมพ์ข้อความเดียวกันในเนื้อหาหรือหัวเรื่อง ตั้งค่าการจัดรูปแบบ เป็นต้น Aspose.Email MailMessage คลาสสามารถใช้เพื่อโหลดและอ่านไฟล์แม่แบบ Outlook (OFT) เมื่อโหลดแม่แบบ Outlook แล้วในอินสแตนซ์ของ MailMessage คลาส คุณสามารถอัปเดตผู้ส่ง, ผู้รับ, เนื้อหา, หัวข้อและคุณสมบัติอื่นๆ หลังจากอัปเดตคุณสมบัติแล้ว:

  • ส่งอีเมลโดยใช้ SmtpClient คลาสหรือ
  • บันทึกข้อความเป็น MSG และทำการอัปเดต/ตรวจสอบเพิ่มเติมโดยใช้ Microsoft Outlook.

ในตัวอย่างโค้ดด้านล่าง เรา:

  1. โหลดแม่แบบโดยใช้ MailMessage คลาส.
  2. อัปเดตคุณสมบัติบางอย่าง.
  3. บันทึกข้อความในรูปแบบ MSG.

ส่วนโค้ดต่อไปนี้จะแสดงวิธีโหลดไฟล์ OFT, อัปเดตข้อความและบันทึกเป็นรูปแบบ 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");

การบันทึกไฟล์ Outlook MSG เป็นแม่แบบ

ส่วนโค้ดต่อไปนี้จะแสดงวิธีบันทึกไฟล์ Outlook 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/";

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

การตั้งค่าประเภทสีสำหรับไฟล์ Outlook MSG

ประเภทสีทำเครื่องหมายข้อความอีเมลเพื่อบ่งบอกความสำคัญหรือประเภทบางอย่าง Microsoft Outlook ให้ผู้ใช้กำหนดประเภทสีเพื่อแยกแยะอีเมล เพื่อจัดการประเภทสี ให้ใช้ FollowUpManager. มีฟังก์ชันเช่น addCategory, removeCategory, clearCategories และ getCategories.

  • addCategory รับค่า MapiMessage และสตริงประเภทสี เช่น "Purple Category" หรือ "Red Category" เป็นอาร์กิวเมนต์.
  • removeCategory รับค่า MapiMessage และสตริงประเภทสีที่ต้องการลบออกจากข้อความ
  • clearCategories ใช้เพื่อลบประเภทสีทั้งหมดจากข้อความ.
  • getCategories ใช้เพื่อดึงประเภทสีทั้งหมดจากข้อความที่ระบุ.

ตัวอย่างต่อไปนี้ทำงานตามขั้นตอนที่ระบุด้านล่าง:

  1. เพิ่มประเภทสี.
  2. เพิ่มประเภทสีอื่น.
  3. ดึงรายการของทุกประเภท.
  4. ลบทุกประเภทออกทั้งหมด.
// 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);

การเข้าถึงข้อมูลติดตามจากไฟล์ MSG

Aspose.Email API ให้ความสามารถในการเข้าถึงข้อมูลติดตามจากข้อความที่ส่งหรือรับ สามารถดึงข้อมูลการอ่าน การรับการจัดส่งและผลการโหวตจากไฟล์ข้อความได้

การดึงข้อมูลการรับและการยืนยันการจัดส่ง

ส่วนโค้ดต่อไปนี้จะแสดงให้คุณเห็นวิธีการเรียกคืนข้อมูลการรับและการยืนยันการจัดส่ง

// 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());
}

การสร้างข้อความต่อและตอบกลับ

Aspose.Email API ให้ความสามารถในการสร้างและจัดรูปแบบข้อความต่อและตอบกลับ The ReplyMessageBuilder และ ForwardMessageBuilder คลาสของ API ใช้สร้างข้อความ Reply และ Forward ตามลำดับ ข้อความ Reply หรือ Forward สามารถกำหนดให้สร้างโดยใช้โหมดใดก็ได้ของ OriginalMessageAdditionMode enum. enum นี้มีค่าต่อไปนี้:

  • OriginalMessageAdditionMode.None - ข้อความต้นฉบับจะไม่ถูกรวมในข้อความตอบกลับ.
  • OriginalMessageAdditionMode.Attachment - ข้อความต้นฉบับจะรวมเป็นไฟล์แนบในข้อความตอบกลับ
  • OriginalMessageAdditionMode.Textpart - ข้อความต้นฉบับจะรวมเป็นข้อความในส่วนเนื้อหาของข้อความตอบกลับ

การสร้างข้อความตอบกลับ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างข้อความตอบกลับ (reply).

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

การสร้างข้อความต่อ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างข้อความต่อ (forward).

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

รักษาวันที่ว่างเมื่อแปลงข้อความ

MapiConversionOptions.setPreserveEmptyDates(boolean) คุณสมบัติบ่งบอกว่าจำเป็นต้องรักษาวันที่ว่างเปล่าขณะแปลงข้อความหรือไม่ API นี้ปรากฏใน Aspose.Email 21.5 ตัวอย่างโค้ดต่อไปนี้แสดงวิธีรักษาวันที่ว่าง.

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