การโหลดและบันทึกข้อความ
การโหลดและบันทึกอีเมล
ตรวจจับรูปแบบไฟล์
Aspose.Email API ให้ความสามารถในการตรวจจับรูปแบบไฟล์ของไฟล์ข้อความที่ให้ไว้. The DetectFileFormat เมธอดของ FileFormatUtil คลาสนี้สามารถใช้เพื่อทำสิ่งนี้ได้. คลาสและเมธอดต่อไปนี้สามารถใช้เพื่อตรวจจับรูปแบบไฟล์ที่โหลด.
- FileFormatType คลาส
- FileFormatInfo คลาส
- FileFormatUtil คลาส
- FileFormatUtil.detectFileFormat(Stream) Method
- FileFormatUtil.detectFileFormat(String) Method
โค้ดตัวอย่างต่อไปนี้แสดงวิธีตรวจจับรูปแบบไฟล์.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the resource directory.
String dataDir = "data/";
// Detect the file format
FileFormatInfo info = FileFormatUtil.detectFileFormat(dataDir + "Message.msg");
// Get the detected load format
System.out.println("The message format is: " + info.getFileFormatType());
ตรวจจับไฟล์ข้อความ RFC
เซิร์ฟเวอร์เมลและเครื่องมือเก็บบันทึกมักจะเก็บข้อความในไฟล์ที่มีเนื้อหา MIME แต่ไม่ใช่ไฟล์ EML หรือ MHT มาตรฐาน เช่นไฟล์ที่มี .trn ส่วนขยาย ไฟล์เหล่านี้ถูกระบุตัวเป็นข้อความ RFC: detectFileFormat คืนค่า FileFormatType.RfcMessage ค่าเหล่านี้ทำให้คุณระบุไฟล์เหล่านี้และประมวลผลเป็นข้อความอีเมลแทนที่จะข้ามเป็นรูปแบบไม่รู้จัก
FileFormatInfo info = FileFormatUtil.detectFileFormat("message.trn");
if (info.getFileFormatType() == FileFormatType.RfcMessage) {
// The file contains MIME content, so it can be loaded as an email message
MailMessage message = MailMessage.load("message.trn");
System.out.println("Body: " + message.getBody());
}
โหลดอีเมล
เพื่อนำเข้าข้อความด้วยตัวเลือกการโหลดเฉพาะ, Aspose.Email มี LoadOptions คลาสที่สามารถใช้ได้ดังต่อไปนี้:
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Loading with default options
// Load from EML
MailMessage eml = MailMessage.load("test.eml", new EmlLoadOptions());
// Load from HTML
MailMessage html = MailMessage.load("test.html", new HtmlLoadOptions());
// Load from MHTML
MailMessage mhtml = MailMessage.load("test.mhtml", new MhtmlLoadOptions());
// Load from MSG
MailMessage msg = MailMessage.load("test.msg", new MsgLoadOptions());
// Load from the TNEF format
MailMessage tnef = MailMessage.load("winmail.dat", new TnefLoadOptions());
// Loading with custom options
EmlLoadOptions emlOptions = new EmlLoadOptions();
emlOptions.setPreserveTnefAttachments(true);
MailMessage emlMailMessage = MailMessage.load("test.eml", emlOptions);
HtmlLoadOptions htmlOptions = new HtmlLoadOptions();
htmlOptions.shouldAddPlainTextView(true);
MailMessage htmlMailMessage = MailMessage.load("test.html", htmlOptions);
โหลดจาก EML
ส่วนนี้อธิบายวิธีโหลดไฟล์ EML เข้าอ็อบเจ็กต์ MailMessage โดยใช้ EmlLoadOptions คลาส. คลาสนี้ให้ตัวเลือกเพื่อปรับแต่งวิธีการโหลดไฟล์ EML เช่น การรักษารูปแบบข้อความที่ฝังอยู่หรือควบคุมพฤติกรรมการโหลดการแนบ TNEF.
- เริ่มต้นอินสแตนซ์ของ EmlLoadOptions
- กำหนดค่าตัวเลือกการโหลดตามต้องการ
- ใช้
MailMessage.load()เมธอดเพื่อโหลดไฟล์ EML ด้วยตัวเลือกที่กำหนด
// Initialize EmlLoadOptions
EmlLoadOptions loadOptions = new EmlLoadOptions();
// Configure the load options
loadOptions.setPreferredTextEncoding(StandardCharsets.UTF_8); // preferred encoding for the message
loadOptions.setPreserveEmbeddedMessageFormat(true); // preserve the format of embedded messages
loadOptions.setPreserveTnefAttachments(true); // control TNEF attachment loading
loadOptions.setRemoveSignature(false); // do not remove the signature
// Load the EML file
MailMessage eml = MailMessage.load("file.eml", loadOptions);
คุณสมบัติของ EmlLoadOptions
PreferredTextEncoding- ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อความข้อความ ค่าเริ่มต้นคือnull.PreserveEmbeddedMessageFormat- ระบุว่าจะรักษารูปแบบของข้อความที่ฝังอยู่ขณะโหลดหรือไม่ ค่าเริ่มต้นคือfalse.PreserveTnefAttachments- ควบคุมพฤติกรรมการโหลดไฟล์แนบ TNEF ค่าเริ่มต้นคือfalse.RemoveSignature- กำหนดว่าควรลบลายเซ็นขณะโหลดหรือไม่ ค่าเริ่มต้นคือfalse.
โหลดข้อความพร้อมลายเซ็น/ไม่มีลายเซ็น
การรักษาลายเซ็นได้รับการสนับสนุนโดยค่าเริ่มต้นเมื่อโหลดไฟล์ EML. เพื่อลบลายเซ็นให้ตั้งค่า setRemoveSignature(true):
EmlLoadOptions loadOptions = new EmlLoadOptions();
loadOptions.setRemoveSignature(true);
MailMessage msg = MailMessage.load("signed.eml", loadOptions);
โหลดจาก EMLX
นี้ EmlxLoadOptions คลาสนี้ให้ตัวเลือกคล้ายกับ EmlLoadOptions โดยให้การควบคุมการเข้ารหัส การลบลายเซ็น และรูปแบบข้อความที่ฝังอยู่.
// Instantiate EmlxLoadOptions
EmlxLoadOptions loadOptions = new EmlxLoadOptions();
loadOptions.setPreferredTextEncoding(StandardCharsets.UTF_8);
loadOptions.setPreserveEmbeddedMessageFormat(true);
loadOptions.setRemoveSignature(true);
// Load the EMLX file
MailMessage emlx = MailMessage.load("file.emlx", loadOptions);
คุณสมบัติของ EmlxLoadOptions
PreferredTextEncoding- ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อความข้อความ ค่าเริ่มต้นคือnull.PreserveEmbeddedMessageFormat- แสดงว่าต้องคงรูปแบบของข้อความที่ฝังอยู่หรือไม่ ค่าเริ่มต้นคือfalse.RemoveSignature- ระบุว่าต้องลบลายเซ็นในระหว่างการโหลดหรือไม่ ค่าเริ่มต้นคือfalse.
โหลดจาก HTML
นี้ HtmlLoadOptions คลาสนี้ออกแบบมาโดยเฉพาะเพื่อจัดการเนื้อหา HTML พร้อมตัวเลือกการจัดการทรัพยากรและการเพิ่มมุมมองข้อความธรรมดา
// Initialize HtmlLoadOptions
HtmlLoadOptions loadOptions = new HtmlLoadOptions();
loadOptions.setPathToResources("resources/"); // directory containing the resource files
loadOptions.setPreferredTextEncoding(StandardCharsets.UTF_8);
loadOptions.shouldAddPlainTextView(true); // add a plain text view of the body
// Load the HTML file
MailMessage html = MailMessage.load("file.html", loadOptions);
คุณสมบัติของ HtmlLoadOptions
PathToResources- กำหนดพาธไปยังไดเรกทอรีที่มีไฟล์ทรัพยากร (เช่นรูปภาพ)PreferredTextEncoding- ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อความข้อความ ค่าเริ่มต้นคือnull.PreserveEmbeddedMessageFormat- กำหนดว่าจะคงรูปแบบของข้อความที่ฝังอยู่หรือไม่ ค่าเริ่มต้นคือfalse.shouldAddPlainTextView(boolean)- ระบุว่าจะเพิ่มมุมมองข้อความตัวอักษรธรรมดาหรือไม่ ค่าเริ่มต้นคือfalse. โปรดสังเกตชื่อ accessor: ไม่ใช่setShouldAddPlainTextView.
โหลดจาก MHTML
นี้ MhtmlLoadOptions คลาสนี้ให้ตัวเลือกเพื่อจัดการการเข้ารหัส, คงรูปแบบข้อความที่ฝังอยู่, และจัดการไฟล์แนบ TNEF
// Create MhtmlLoadOptions
MhtmlLoadOptions loadOptions = new MhtmlLoadOptions();
loadOptions.setPreferredTextEncoding(StandardCharsets.UTF_8);
loadOptions.setPreserveEmbeddedMessageFormat(true);
loadOptions.setPreserveTnefAttachments(true);
loadOptions.setRemoveSignature(false);
// Load the MHTML file
MailMessage mhtml = MailMessage.load("file.mht", loadOptions);
คุณสมบัติของ MhtmlLoadOptions
MessageFormat- แสดงรูปแบบของเมล ซึ่งอาจเป็น EML, MSG หรือ MHTML ค่าเริ่มต้นคือ EML คุณสมบัตินี้อ่านได้เท่านั้นใน JavaPreferredTextEncoding- ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อความข้อความ ค่าเริ่มต้นคือnull.PreserveEmbeddedMessageFormat- กำหนดว่าจะคงรูปแบบของข้อความที่ฝังอยู่หรือไม่ ค่าเริ่มต้นคือfalse.PreserveTnefAttachments- ควบคุมพฤติกรรมการโหลดไฟล์แนบ TNEF ค่าเริ่มต้นคือfalse.RemoveSignature- ระบุว่าต้องลบลายเซ็นในระหว่างการโหลดหรือไม่ ค่าเริ่มต้นคือfalse.
โหลดจาก MSG
นี้ MsgLoadOptions คลาสนี้ให้คุณสมบัติต่าง ๆ เพื่อจัดการวิธีการโหลดไฟล์ MSG รวมถึงตัวเลือกการคงเนื้อหา RTF หรือจัดการไฟล์แนบ TNEF
// Create MsgLoadOptions
MsgLoadOptions loadOptions = new MsgLoadOptions();
loadOptions.setKeepOriginalEmailAddresses(true); // preserve the original email addresses
loadOptions.setPreferredTextEncoding(StandardCharsets.UTF_8);
loadOptions.setPreserveRtfContent(true); // keep the RTF content in the message
loadOptions.setPreserveTnefAttachments(true);
loadOptions.setRemoveSignature(false);
loadOptions.setTimeout(5000); // limit the loading time to 5 seconds
// Load the MSG file
MailMessage msg = MailMessage.load("file.msg", loadOptions);
คุณสมบัติของ MsgLoadOptions
KeepOriginalEmailAddresses- ระบุว่าจะเก็บที่อยู่อีเมลเดิมไว้หรือไม่ ค่าเริ่มต้นคือfalse.PreferredTextEncoding- กำหนดการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อความค่าเริ่มต้นคือnull.PreserveEmbeddedMessageFormat- กำหนดว่ารูปแบบของข้อความที่ฝังอยู่ต้องคงไว้หรือไม่ ค่าเริ่มต้นคือfalse.PreserveRtfContent- ระบุว่าจะเก็บเนื้อหา RTF ไว้ในMailMessage. ค่าเริ่มต้นคือfalse.PreserveTnefAttachments- จัดการพฤติกรรมการโหลดไฟล์แนบ TNEF ค่าเริ่มต้นคือfalse.RemoveSignature- ตัดสินใจว่าจะลบลายเซ็นในระหว่างการโหลดหรือไม่ ค่าเริ่มต้นคือfalse.Timeout- จำกัดเวลาการจัดรูปแบบเป็นมิลลิวินาทีระหว่างการแปลง ค่าเริ่มต้นคือ 3000 มิลลิวินาทีTimeoutReachedHandler- ตัวจัดการที่เรียกเมื่อหมดเวลา
โหลดจาก TNEF
นี้ TnefLoadOptions คลาสนี้ให้ตัวเลือกเพื่อจัดการการเข้ารหัสและลบลายเซ็นขณะโหลดไฟล์ EML ที่อยู่ในรูปแบบ TNEF
// Instantiate TnefLoadOptions
TnefLoadOptions loadOptions = new TnefLoadOptions();
loadOptions.setPreferredTextEncoding(StandardCharsets.UTF_8);
loadOptions.setPreserveEmbeddedMessageFormat(true);
loadOptions.setRemoveSignature(true);
// Load the TNEF file
MailMessage tnef = MailMessage.load("file.eml", loadOptions);
คุณสมบัติของ TnefLoadOptions
MessageFormat- แสดงรูปแบบของเมล เช่น EML, MSG หรือ MHTML ค่าเริ่มต้นคือ EML คุณสมบัตินี้อ่านได้เท่านั้นใน JavaPreferredTextEncoding- ตั้งค่าการเข้ารหัสที่ต้องการสำหรับหัวข้อและเนื้อความข้อความ ค่าเริ่มต้นคือnull.PreserveEmbeddedMessageFormat- แสดงว่าต้องคงรูปแบบของข้อความที่ฝังอยู่หรือไม่ ค่าเริ่มต้นคือfalse.RemoveSignature- ระบุว่าต้องลบลายเซ็นในระหว่างการโหลดหรือไม่ ค่าเริ่มต้นคือfalse.
คงรูปแบบข้อความฝังระหว่างการโหลด
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
EmlLoadOptions options = new EmlLoadOptions();
options.setPreserveEmbeddedMessageFormat(true);
MailMessage mail = MailMessage.load(dataDir + "tnefWithMsgInside.eml", options);
int fileFormat = FileFormatUtil.detectFileFormat(
mail.getAttachments().get_Item(0).getContentStream()).getFileFormatType();
System.out.println("Embedded message file format: " + fileFormat);
บันทึกและแปลงอีเมล
Aspose.Email ทำให้การแปลงประเภทข้อความใดๆ ไปยังรูปแบบอื่นเป็นเรื่องง่าย. เพื่อแสดงคุณลักษณะนี้, โค้ดในบทความนี้โหลดข้อความสามประเภทจากดิสก์และบันทึกกลับเป็นรูปแบบอื่น. คลาสฐาน SaveOptions และคลาส EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions สำหรับการตั้งค่าเพิ่มเติมเมื่อบันทึก MailMessage สามารถใช้เพื่อบันทึกข้อความเป็นรูปแบบอื่น. บทความนี้แสดงวิธีใช้คลาสเหล่านี้เพื่อบันทึกอีเมลตัวอย่างเป็น:
- รูปแบบ EML.
- Outlook MSG.
- รูปแบบ MHTML.
- รูปแบบ HTML.
โหลดและบันทึกอีเมล
โค้ดต่อไปนี้แสดงวิธีโหลดข้อความ EML และบันทึกลงดิสก์ในรูปแบบเดียวกัน
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage msg = MailMessage.load(dataDir + "test.eml", new EmlLoadOptions());
// Save the email message to disk by using the SaveOptions
msg.save(dataDir + "LoadAndSaveFileAsEML_out.eml", SaveOptions.getDefaultEml());
ขอบเขตของข้อความต้นฉบับ
เมื่อโหลดข้อความ EML แล้วบันทึกกลับเป็น EML ขอบเขตของข้อความต้นฉบับจะถูกเก็บไว้ตามค่าเริ่มต้น ดังนั้นไม่ต้องตั้งค่าใด ๆ เพื่อรักษาขอบเขต เวอร์ชันก่อนของไลบรารีมี EmlSaveOptions.PreserveOriginalBoundaries คุณสมบัตินี้สำหรับวัตถุประสงค์ดังกล่าว คุณสมบัตินี้ได้ถูกลบออกแล้ว และการรักษาขอบเขตเดิมเป็นพฤติกรรมเริ่มต้นแล้ว
ปรับแต่งขอบเขต MIME
หากคุณต้องการให้ขอบเขตตามรูปแบบที่กำหนดเฉพาะ ให้ใช้ BoundariesTemplate คุณสมบัติของ EmlSaveOptions คลาส กำหนดเทมแพลตที่ใช้สร้างขอบเขต MIME สำหรับข้อความที่บันทึก ค่าตั้งต้นคือ null, ซึ่งหมายความว่าขอบเขตถูกสร้างโดยไลบรารี รองรับไวลด์การ์ดสองตัวในเทมแพลต:
{#}- หมายเลขขอบเขต{guid}- GUID ที่สร้างขึ้น
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลด EML และบันทึกเป็น EML พร้อมขอบเขต MIME ที่กำหนดเอง
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = MailMessage.load(dataDir + "test.eml");
// Save as EML with a custom boundary template
EmlSaveOptions emlSaveOptions = new EmlSaveOptions(MailMessageSaveType.getEmlFormat());
emlSaveOptions.setBoundariesTemplate("boundary_{#}_{guid}");
eml.save(dataDir + "CustomBoundaries_out.eml", emlSaveOptions);
บันทึกเป็น EML คงไฟล์แนบ TNEF
โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกเป็น EML โดยคงไฟล์แนบ TNEF ไว้.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = MailMessage.load(dataDir + "TnefAttachments.eml");
// Save as EML with the TNEF attachments preserved
EmlSaveOptions emlSaveOptions = new EmlSaveOptions(MailMessageSaveType.getEmlFormat());
emlSaveOptions.setFileCompatibilityMode(FileCompatibilityMode.PreserveTnefAttachments);
eml.save(dataDir + "PreserveTNEFAttachment_out.eml", emlSaveOptions);
บันทึก EML เป็น MSG
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML และแปลงเป็น MSG โดยใช้ตัวเลือกที่เหมาะสมจาก SaveOptions.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
// Initialize and load an existing EML file
MailMessage eml = MailMessage.load(dataDir + "test.eml");
// Save the email message to disk in Unicode format
eml.save(dataDir + "LoadingEMLSavingToMSG_out.msg", SaveOptions.getDefaultMsgUnicode());
บันทึก EML เป็น MSG พร้อมคงวันที่
นี้ MsgSaveOptions คลาสนี้ช่วยให้คุณบันทึกข้อความต้นฉบับเป็นไฟล์ Outlook Message (MSG) พร้อมรักษาวันที่. โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกข้อความเป็น MSG พร้อมรักษาวันที่เดิม.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = MailMessage.load(dataDir + "test.eml");
// Save as MSG with the original dates preserved
MsgSaveOptions msgSaveOptions = new MsgSaveOptions(MailMessageSaveType.getOutlookMessageFormatUnicode());
msgSaveOptions.setPreserveOriginalDates(true);
eml.save(dataDir + "SavingAsMSGWithPreservedDates_out.msg", msgSaveOptions);
บันทึก EML เป็น MHTML
ตัวเลือกต่างๆ ของ MHTML สามารถใช้เพื่อให้ได้ผลลัพธ์ที่ต้องการ. โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML เข้าไปใน MailMessage และแปลงเป็น MHTML พร้อมวันที่ข้อความในระบบ UTC.
String dataDir = "data/";
// Set options for MHTML output
MhtSaveOptions saveOptions = SaveOptions.getDefaultMhtml();
// Save the message date as a UTC date
saveOptions.setPreserveOriginalDate(false);
// Initialize and load an existing EML file
try (MailMessage mailMessage = MailMessage.load(dataDir + "Message.eml")) {
mailMessage.save(dataDir + "Message_out.mhtml", saveOptions);
}
จัดรูปแบบส่วนหัว MHT อย่างทั่วโลกเมื่อบันทึกจาก EML
ด้วย Aspose.Email คุณสามารถใช้ตัวเลือกการจัดรูปแบบทั่วโลกสำหรับส่วนหัว Mht. ตัวเลือกทั่วโลกกำหนดการจัดรูปแบบร่วมของส่วนหัว Mht สำหรับทุก MhtSaveOptions อินสแตนซ์. ฟีเจอร์นี้ออกแบบมาเพื่อหลีกเลี่ยงการตั้งค่ารูปแบบสำหรับแต่ละอินสแตนซ์ของ MhtSaveOptions.
ใช้เมธอดต่อไปนี้ของ GlobalFormattingOptions คลาสและตัวอย่างโค้ดด้านล่างเพื่อกำหนดรูปแบบของส่วนหัว Mht:
- setPageHeaderFormat(String value) - PageHeaderFormat สำหรับอินสแตนซ์ของ HeadersFormattingOptions หาก DefaultPageHeaderFormat ไม่ได้ตั้งค่า.
- setHeaderFormat(String value) - HeaderFormat สำหรับอินสแตนซ์ของ HeadersFormattingOptions หาก DefaultHeaderFormat ไม่ได้ตั้งค่า.
- setBeforeHeadersFormat(String value) - BeforeHeadersFormat สำหรับอินสแตนซ์ของ HeadersFormattingOptions หาก BeforeHeadersFormat ไม่ได้ตั้งค่า.
- setAfterHeadersFormat(String value) - AfterHeadersFormat สำหรับอินสแตนซ์ของ HeadersFormattingOptions หาก AfterHeadersFormat ไม่ได้ตั้งค่า.
// The formatting is set once and applies to every MhtSaveOptions instance created afterwards
GlobalFormattingOptions.setPageHeaderFormat("<div><h1>{0}</h1></div>");
GlobalFormattingOptions.setHeaderFormat("<div>{0}: {1}</div>");
GlobalFormattingOptions.setBeforeHeadersFormat("<div style='font-family:Arial'>");
GlobalFormattingOptions.setAfterHeadersFormat("</div>");
// saveOptions1 and saveOptions2 have the same MHT header formatting
MhtSaveOptions saveOptions1 = new MhtSaveOptions();
MhtSaveOptions saveOptions2 = new MhtSaveOptions();
แปลง EML เป็น MHTML พร้อมการตั้งค่าเพิ่มเติม
นี้ MhtSaveOptions คลาสนี้ให้ตัวเลือกเพิ่มเติมสำหรับการบันทึกอีเมลเป็นรูปแบบ MHTML. ตัวนับค่า MhtFormatOptions ทำให้สามารถเขียนข้อมูลอีเมลเพิ่มเติมลงไฟล์ MHTML ได้. ฟิลด์เพิ่มเติมต่อไปนี้สามารถเขียนได้:
- WriteHeader - เขียนส่วนหัวอีเมลลงไฟล์ผลลัพธ์.
- WriteOutlineAttachments - เขียนไฟล์แนบโครงร่างลงไฟล์ผลลัพธ์.
- WriteCompleteEmailAddress - เขียนที่อยู่อีเมลเต็มลงไฟล์ผลลัพธ์.
- NoEncodeCharacters - ไม่ควรใช้การเข้ารหัสการส่งอักขระ.
- HideExtraPrintHeader - ซ่อนส่วนหัวพิมพ์พิเศษจากบนไฟล์ผลลัพธ์.
- WriteCompleteToEmailAddress - เขียนที่อยู่อีเมลผู้รับเต็มลงไฟล์ผลลัพธ์.
- WriteCompleteFromEmailAddress - เขียนที่อยู่อีเมลผู้ส่งเต็มลงไฟล์ผลลัพธ์.
- WriteCompleteCcEmailAddress - เขียนที่อยู่อีเมลเต็มของผู้รับ CC ลงไฟล์ผลลัพธ์.
- WriteCompleteBccEmailAddress - เขียนที่อยู่อีเมลเต็มของผู้รับ BCC ลงไฟล์ผลลัพธ์.
- RenderCalendarEvent - เขียนข้อความจากเหตุการณ์ปฏิทินลงไฟล์ผลลัพธ์.
- SkipByteOrderMarkInBody - ข้ามไบต์ BOM ในส่วนข้อความของอีเมล.
- RenderVCardInfo - เขียนข้อความจากมุมมอง VCard ทางเลือกลงในไฟล์ผลลัพธ์.
- DisplayAsOutlook - แสดงส่วนหัว From.
- RenderTaskFields - เขียนฟิลด์งานเฉพาะลงในไฟล์ผลลัพธ์.
- ไม่มี - ไม่ได้ระบุการตั้งค่า.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีแปลงไฟล์ EML เป็น MHTML พร้อมการตั้งค่าเพิ่มเติม.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = MailMessage.load(dataDir + "test.eml");
// Save as MHT with a header
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.setMhtFormatOptions(MhtFormatOptions.WriteHeader | MhtFormatOptions.HideExtraPrintHeader);
eml.save(dataDir + "ConvertingToMHTMLWithOptionalSettings_out.mht", mhtSaveOptions);
เรนเดอร์เหตุการณ์ปฏิทินขณะแปลงเป็น MHTML
นี้ MhtFormatOptions.RenderCalendarEvent ตัวเลือกนี้ทำให้เหตุการณ์ปฏิทินถูกเรนเดอร์ออกเป็น MHTML. โค้ดตัวอย่างต่อไปนี้แสดงวิธีเรนเดอร์เหตุการณ์ปฏิทินขณะแปลงเป็น MHTML.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage msg = MailMessage.load(dataDir + "Meeting with Recurring Occurrences.msg");
MhtSaveOptions options = new MhtSaveOptions();
options.setMhtFormatOptions(MhtFormatOptions.WriteHeader | MhtFormatOptions.RenderCalendarEvent);
// Format the output details if required - optional.
// set_Item overrides a template, and adds it when it is not in the collection yet.
options.getFormatTemplates().set_Item(MhtTemplateName.START,
"<span class='headerLineTitle'>Start:</span><span class='headerLineText'>{0}</span><br/>");
options.getFormatTemplates().set_Item(MhtTemplateName.END,
"<span class='headerLineTitle'>End:</span><span class='headerLineText'>{0}</span><br/>");
options.getFormatTemplates().set_Item(MhtTemplateName.RECURRENCE,
"<span class='headerLineTitle'>Recurrence:</span><span class='headerLineText'>{0}</span><br/>");
options.getFormatTemplates().set_Item(MhtTemplateName.RECURRENCE_PATTERN,
"<span class='headerLineTitle'>RecurrencePattern:</span><span class='headerLineText'>{0}</span><br/>");
options.getFormatTemplates().set_Item(MhtTemplateName.ORGANIZER,
"<span class='headerLineTitle'>Organizer:</span><span class='headerLineText'>{0}</span><br/>");
options.getFormatTemplates().set_Item(MhtTemplateName.REQUIRED_ATTENDEES,
"<span class='headerLineTitle'>RequiredAttendees:</span><span class='headerLineText'>{0}</span><br/>");
msg.save(dataDir + "Meeting with Recurring Occurrences_out.mhtml", options);
ส่งออกอีเมลเป็น MHT โดยไม่มีภาพในบรรทัด
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = MailMessage.load(dataDir + "Message.msg", new MsgLoadOptions());
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.setSkipInlineImages(true);
eml.save(dataDir + "EmlToMhtmlWithoutInlineImages_out.mht", mhtSaveOptions);
ส่งออกอีเมลเป็น MHT พร้อมเขตเวลาแบบกำหนดเอง
นี้ MailMessage class ให้ setTimeZoneOffset วิธีการตั้งค่าโซนเวลาที่กำหนดเองเมื่อส่งออกเป็น MHT. โค้ดตัวอย่างต่อไปนี้แสดงวิธีส่งออกอีเมลเป็น MHT พร้อมโซนเวลาที่กำหนดเอง.
String dataDir = "data/";
MailMessage msg = MailMessage.load(dataDir + "Message.msg", new MsgLoadOptions());
msg.setDate(new Date());
// Set the time zone offset in milliseconds
msg.setTimeZoneOffset(5 * 60 * 60 * 1000);
MhtSaveOptions mhtOptions = new MhtSaveOptions();
mhtOptions.setMhtFormatOptions(MhtFormatOptions.WriteHeader);
msg.save(dataDir + "ExportToMHTWithCustomTimezone_out.mhtml", mhtOptions);
การส่งออกอีเมลเป็น EML
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการส่งออกอีเมลเป็นไฟล์ EML.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = MailMessage.load(dataDir + "Message.msg", new MsgLoadOptions());
eml.save(dataDir + "testEml_out.eml", SaveOptions.getDefaultEml());
บันทึกอีเมลเป็น HTML
นี้ HtmlSaveOptions คลาสที่ช่วยให้คุณส่งออกเนื้อความของข้อความเป็น HTML. โค้ดตัวอย่างต่อไปนี้แสดงวิธีการบันทึกข้อความเป็น HTML.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage msg = MailMessage.load(dataDir + "Message.msg");
msg.save(dataDir + "SavingMessageAsHTML_out1.html", SaveOptions.getDefaultHtml());
// or
MailMessage eml = MailMessage.load(dataDir + "test.eml");
HtmlSaveOptions options = SaveOptions.getDefaultHtml();
options.setHtmlFormatOptions(HtmlFormatOptions.WriteHeader | HtmlFormatOptions.WriteCompleteEmailAddress);
eml.save(dataDir + "SavingMessageAsHTML_out2.html", options);
บันทึกข้อความอีเมลเป็น HTML ด้วยเส้นทางสัมพันธ์ไปยังทรัพยากร
เมื่อส่งออกข้อความอีเมลเป็นรูปแบบ HTML, สามารถเลือกบันทึกทรัพยากรอีเมลด้วยเส้นทางสัมพันธ์ได้. ฟีเจอร์นี้ให้ความยืดหยุ่นมากขึ้นในการเชื่อมโยงทรัพยากรในไฟล์ HTML ผลลัพธ์, ทำให้การแชร์และแสดงอีเมลที่บันทึกไว้บนระบบต่างๆ ง่ายขึ้น. HtmlSaveOptions.UseRelativePathToResources คุณสมบัตินี้ให้ความสามารถในการบันทึกทรัพยากรด้วยเส้นทางแบบสัมพันธ์. ค่าเริ่มต้นของคุณสมบัติคือ false (ทรัพยากรถูกบันทึกด้วยเส้นทางแบบเต็ม). เมื่อตั้งค่าเป็น true, ทรัพยากรถูกบันทึกด้วยเส้นทางแบบสัมพันธ์. ไฟล์ HTML ที่ใช้เส้นทางแบบสัมพันธ์จะพกพาได้ง่ายและสามารถดูได้อย่างถูกต้องโดยไม่ขึ้นกับโครงสร้างไฟล์ของสภาพแวดล้อมโฮสติ้ง. คุณสามารถเลือกใช้เส้นทางแบบเต็มหรือแบบสัมพันธ์ตามความต้องการของคุณ. คุณสามารถกำหนดเส้นทางแบบกำหนดเองสำหรับทรัพยากรโดยใช้ ResourceHtmlRenderingHandler เหตุการณ์.
บันทึกด้วยเส้นทางสัมพันธ์เริ่มต้นไปยังทรัพยากร
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "message.msg");
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
htmlSaveOptions.setResourceRenderingMode(ResourceRenderingMode.SaveToFile);
htmlSaveOptions.setUseRelativePathToResources(true);
msg.save(dataDir + "target.html", htmlSaveOptions);
ในกรณีนี้, ทรัพยากรถูกบันทึกใน [html file name].files โฟลเดอร์, มีเส้นทางเดียวกับไฟล์ .html, และ HTML จะอ้างอิงทรัพยากรผ่านเส้นทางแบบสัมพันธ์.
บันทึกด้วยเส้นทางสัมบูรณ์ไปยังทรัพยากร
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "message.msg");
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
htmlSaveOptions.setResourceRenderingMode(ResourceRenderingMode.SaveToFile);
htmlSaveOptions.setUseRelativePathToResources(false);
msg.save(dataDir + "target.html", htmlSaveOptions);
เช่นในกรณีแรก, ทรัพยากรถูกบันทึกใน [html file name].files โฟลเดอร์โดยค่าเริ่มต้น, แต่ HTML จะอ้างอิงทรัพยากรด้วยเส้นทางแบบเต็ม.
เส้นทางสัมพันธ์แบบกำหนดเองโดยใช้เหตุการณ์ ResourceHtmlRenderingHandler
String dataDir = "data/";
MapiMessage msg = MapiMessage.load(dataDir + "message.msg");
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
htmlSaveOptions.setResourceRenderingMode(ResourceRenderingMode.SaveToFile);
htmlSaveOptions.setUseRelativePathToResources(true);
htmlSaveOptions.setResourceHtmlRenderingHandler(new ResourceHtmlRenderingHandler() {
@Override
public void invoke(Object sender, ResourceHtmlRenderingEventArgs args) {
if (sender instanceof AttachmentBase) {
AttachmentBase attachment = (AttachmentBase) sender;
// UseRelativePathToResources is true, so assign a relative path to PathToResourceFile
args.setPathToResourceFile("images/" + attachment.getContentType().getName());
}
}
});
msg.save(dataDir + "A Day in the Park.html", htmlSaveOptions);
โดยใช้ ResourceHtmlRenderingHandler เหตุการณ์, คุณสามารถตั้งค่าเส้นทางสัมพันธ์หรือสัมบูรณ์แบบกำหนดเองสำหรับทรัพยากร. เมื่อปรับแต่งเส้นทางด้วย ResourceHtmlRenderingHandler ตัวจัดการเหตุการณ์, กำหนดเส้นทางแบบสัมพันธ์ให้กับ PathToResourceFile คุณสมบัติเมื่อ UseRelativePathToResources ตั้งค่าเป็น true, และใช้เส้นทางแบบเต็มเมื่อเป็น false, เพื่อให้การอ้างอิงถูกต้อง.
รักษาไอคอนที่กำหนดเองในข้อความขณะแปลงเป็น HTML
บางครั้งข้อความมีไฟล์แนบแบบอินไลน์ที่แสดงเป็นรูปไอคอนในตัวข้อความ. ข้อความลักษณะนี้อาจทำให้เกิดปัญหาเมื่อต้องแปลงเป็น HTML เนื่องจากรูปไอคอนหายไป เพราะไอคอนไฟล์แนบไม่ได้ถูกเก็บโดยตรงในข้อความ.
Aspose.Email ให้คุณปรับแต่งไอคอนสำหรับไฟล์แนบเมื่อต้องแปลงข้อความเป็น HTML. ไอคอนเหล่านี้ ResourceHtmlRenderingHandler เหตุการณ์นี้ใช้เพื่อปรับแต่งการแสดงผลของไฟล์ทรัพยากร (เช่น ไฟล์แนบ) เมื่อบันทึกข้อความอีเมลเป็นไฟล์ HTML. ในตัวอย่างโค้ดด้านล่าง ตัวจัดการเหตุการณ์จะตั้งค่าเส้นทางไปยังไฟล์ทรัพยากร (ไอคอน) อย่างไดนามิกตามประเภทเนื้อหาของไฟล์แนบ. สิ่งนี้ทำให้สามารถแสดงผลทรัพยากรในผลลัพธ์ HTML ได้ตามประเภทไฟล์ที่กำหนดเอง.
String dataDir = "data/";
HtmlSaveOptions options = new HtmlSaveOptions();
options.setResourceHtmlRenderingHandler(new ResourceHtmlRenderingHandler() {
@Override
public void invoke(Object sender, ResourceHtmlRenderingEventArgs e) {
AttachmentBase attachment = (AttachmentBase) sender;
String name = attachment.getContentType().getName();
e.setCaption(name);
if (name.endsWith(".pdf")) {
e.setPathToResourceFile("pdf_icon.png");
} else if (name.endsWith(".docx")) {
e.setPathToResourceFile("word_icon.jpg");
} else if (name.endsWith(".jpg")) {
e.setPathToResourceFile("jpeg_icon.png");
} else {
e.setPathToResourceFile("not_found_icon.png");
}
}
});
options.setResourceRenderingMode(ResourceRenderingMode.SubstituteFromFile);
MailMessage mailMessage = MailMessage.load(dataDir + "message.msg");
mailMessage.save(dataDir + "message.html", options);
ตั้งเวลาและเขตเวลาเมื่อบันทึก EML เป็น HTML
ผู้ใช้ Aspose.Email สามารถตั้งรูปแบบการแสดงเวลาและโซนเวลาที่ HtmlSaveOptions. ส่วน HeadersFormattingOptions คลาสนี้ช่วยให้คุณระบุตัวเลือกการจัดรูปแบบส่วนหัวเมื่อบันทึก MailMessage เป็นรูปแบบ MHTML หรือ HTML. ค่าคงที่ต่อไปนี้ของ HtmlFormatOptions คลาสที่ระบุฟิลด์ที่จะปรากฏในไฟล์ผลลัพธ์:
- RenderCalendarEvent - ระบุว่าต้องเขียนข้อความจากกิจกรรมปฏิทินลง HTML
- RenderVCardInfo - ระบุว่าต้องเขียนข้อความจากมุมมอง VCard แสดงผลลง HTML
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีตั้งเวลาและโซนเวลาเมื่อบันทึก EML เป็น HTML:
String dataDir = "data/";
MailMessage msg = MailMessage.load(dataDir + "Message.eml");
HtmlSaveOptions options = new HtmlSaveOptions();
options.setHtmlFormatOptions(HtmlFormatOptions.WriteHeader);
options.getFormatTemplates().set_Item(MhtTemplateName.DATE_TIME, "MM d yyyy HH:mm tt");
msg.save(dataDir + "Message_out.html", options);
บันทึกเป็น HTML โดยไม่ฝังทรัพยากร
โดยค่าเริ่มต้น, ทรัพยากรของข้อความจะฝังไว้ใน HTML ผลลัพธ์เป็น data URIs ตั้งค่า ResourceRenderingMode เป็น ResourceRenderingMode.SaveToFile เพื่อเขียนเป็นไฟล์แยกข้างไฟล์ HTML แทน
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = MailMessage.load(dataDir + "EmailWithAttandEmbedded.eml");
HtmlSaveOptions options = new HtmlSaveOptions();
// Write the resources next to the HTML file instead of embedding them into it
options.setResourceRenderingMode(ResourceRenderingMode.SaveToFile);
options.setUseRelativePathToResources(true);
eml.save(dataDir + "EmailWithAttandEmbedded_out.html", options);
การบันทึกข้อความเป็นไฟล์เทมเพลต Outlook (.oft)
เทมเพลต Outlook เป็นรูปแบบระดับ MAPI ดังนั้นข้อความจะถูกแปลงเป็น MapiMessage แรกแล้วเขียนด้วย saveAsTemplate. โค้ดต่อไปนี้แสดงวิธีบันทึกข้อความเป็นเทมเพลต Outlook (.oft)
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = "data/";
MailMessage eml = new MailMessage("test@from.to", "test@to.to", "template subject", "Template body");
// An Outlook template is a MAPI-level format, so convert the message first
MapiMessage msg = MapiMessage.fromMailMessage(eml);
msg.saveAsTemplate(dataDir + "EmlAsOft_out.oft");