Tải lên và Lưu Tin nhắn
Tải lên và Lưu Tin nhắn Email
Phát hiện Định dạng Tệp
Aspose.Email API cung cấp khả năng phát hiện định dạng tệp của tệp tin tin nhắn đã cung cấp. DetectFileFormat phương thức của FileFormatUtil lớp có thể được sử dụng để đạt được điều này. Các lớp và phương thức sau có thể được dùng để phát hiện định dạng tệp đã tải.
- FileFormatType Lớp
- FileFormatInfo Lớp
- FileFormatUtil Lớp
- FileFormatUtil.detectFileFormat(Stream) Method
- FileFormatUtil.detectFileFormat(String) Method
Đoạn mã dưới đây cho bạn thấy cách phát hiện định dạng tệp.
// 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());
Phát Hiện Tệp Tin Nhắn RFC
Máy chủ mail và công cụ lưu trữ thường lưu trữ tin nhắn trong các tệp chứa nội dung MIME nhưng không phải là tệp EML hoặc MHT chuẩn, ví dụ, các tệp có .trn phần mở rộng. Các tệp như vậy được xác định là tin nhắn RFC: detectFileFormat trả về FileFormatType.RfcMessage giá trị cho chúng. Điều này cho phép bạn nhận dạng các tệp này và xử lý chúng như email thay vì bỏ qua chúng như một định dạng không rõ.
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());
}
Tải một Tin nhắn Email
Để tải một tin nhắn với các tùy chọn tải cụ thể, Aspose.Email cung cấp LoadOptions lớp có thể được sử dụng như sau:
// 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);
Tải từ EML
Phần này mô tả cách tải tệp EML vào đối tượng MailMessage bằng cách sử dụng EmlLoadOptions lớp. Lớp này cung cấp các tùy chọn để tùy chỉnh cách tệp EML được tải, như bảo toàn định dạng tin nhắn nhúng hoặc kiểm soát hành vi tải tệp đính kèm TNEF.
- Khởi tạo một thể hiện của EmlLoadOptions.
- Cấu hình các tùy chọn tải theo nhu cầu.
- Sử dụng
MailMessage.load()phương thức để tải tệp EML với các tùy chọn được chỉ định.
// 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);
Thuộc tính của EmlLoadOptions
PreferredTextEncoding- đặt mã hoá ưu tiên cho tiêu đề và nội dung tin nhắn. Mặc định lànull.PreserveEmbeddedMessageFormat- cho biết có nên bảo toàn định dạng của các tin nhắn nhúng trong quá trình tải hay không. Mặc định làfalse.PreserveTnefAttachments- kiểm soát hành vi tải tệp đính kèm TNEF. Mặc định làfalse.RemoveSignature- chỉ định liệu chữ ký có nên được loại bỏ khi tải hay không. Mặc định làfalse.
Tải Tin Nhắn Có Chữ Ký/Không Có Chữ Ký
Bảo tồn chữ ký được hỗ trợ mặc định khi tải các tệp EML. Để loại bỏ chữ ký, đặt setRemoveSignature(true):
EmlLoadOptions loadOptions = new EmlLoadOptions();
loadOptions.setRemoveSignature(true);
MailMessage msg = MailMessage.load("signed.eml", loadOptions);
Tải từ EMLX
Cái EmlxLoadOptions lớp cung cấp các tùy chọn tương tự như EmlLoadOptions, cho phép kiểm soát mã hóa, loại bỏ chữ ký và định dạng tin nhắn nhúng.
// 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);
Thuộc tính của EmlxLoadOptions
PreferredTextEncoding- đặt mã hoá ưu tiên cho tiêu đề và nội dung tin nhắn. Mặc định lànull.PreserveEmbeddedMessageFormat- cho biết có nên bảo tồn định dạng của các tin nhắn nhúng không. Mặc định làfalse.RemoveSignature- chỉ định có nên xóa chữ ký khi tải không. Mặc định làfalse.
Tải từ HTML
Cái HtmlLoadOptions lớp được thiết kế riêng để xử lý nội dung HTML, với các tùy chọn quản lý tài nguyên và thêm chế độ xem văn bản thuần.
// 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);
Thuộc tính của HtmlLoadOptions
PathToResources- định nghĩa đường dẫn tới thư mục chứa các tệp tài nguyên (hình ảnh, ví dụ).PreferredTextEncoding- đặt mã hoá ưu tiên cho tiêu đề và nội dung tin nhắn. Mặc định lànull.PreserveEmbeddedMessageFormat- xác định có nên bảo tồn định dạng của các tin nhắn nhúng không. Mặc định làfalse.shouldAddPlainTextView(boolean)- chỉ định có nên thêm một chế độ xem văn bản thuần của thân tin nhắn. Mặc định làfalse. Lưu ý tên truy cập: nó không phải làsetShouldAddPlainTextView.
Tải từ MHTML
Cái MhtmlLoadOptions lớp cung cấp các tùy chọn để quản lý mã hoá, bảo tồn định dạng tin nhắn nhúng và xử lý tệp đính kèm 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);
Thuộc tính của MhtmlLoadOptions
MessageFormat- đại diện cho định dạng tin nhắn mail, có thể là EML, MSG hoặc MHTML. Mặc định là EML. Thuộc tính này chỉ đọc trong Java.PreferredTextEncoding- đặt mã hoá ưu tiên cho tiêu đề và nội dung tin nhắn. Mặc định lànull.PreserveEmbeddedMessageFormat- xác định có nên bảo tồn định dạng của các tin nhắn nhúng không. Mặc định làfalse.PreserveTnefAttachments- kiểm soát hành vi tải tệp đính kèm TNEF. Mặc định làfalse.RemoveSignature- chỉ định có nên xóa chữ ký khi tải không. Mặc định làfalse.
Tải từ MSG
Cái MsgLoadOptions lớp cung cấp nhiều thuộc tính để xử lý cách tải các tệp MSG, bao gồm các tùy chọn bảo tồn nội dung RTF hoặc quản lý tệp đính kèm 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);
Thuộc tính của MsgLoadOptions
KeepOriginalEmailAddresses- cho biết có nên giữ các địa chỉ email gốc không. Mặc định làfalse.PreferredTextEncoding- định nghĩa mã hoá ưu tiên cho tiêu đề và nội dung tin nhắn. Mặc định lànull.PreserveEmbeddedMessageFormat- xác định có nên bảo tồn định dạng của các tin nhắn nhúng không. Mặc định làfalse.PreserveRtfContent- chỉ định có nên giữ nội dung RTF trong phần thânMailMessage. Mặc định làfalse.PreserveTnefAttachments- quản lý hành vi tải tệp đính kèm TNEF. Mặc định làfalse.RemoveSignature- quyết định có nên xóa chữ ký khi tải không. Mặc định làfalse.Timeout- giới hạn thời gian định dạng tính bằng mili giây trong quá trình chuyển đổi. Mặc định là 3000 ms.TimeoutReachedHandler- trình xử lý được kích hoạt khi thời gian chờ hết hạn.
Tải từ TNEF
Cái TnefLoadOptions lớp cung cấp các tùy chọn để quản lý mã hoá và xóa chữ ký khi tải tệp EML ở định dạng 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);
Thuộc tính của TnefLoadOptions
MessageFormat- đại diện cho định dạng tin nhắn mail, như EML, MSG hoặc MHTML. Mặc định là EML. Thuộc tính này chỉ đọc trong Java.PreferredTextEncoding- đặt mã hoá ưu tiên cho tiêu đề và nội dung tin nhắn. Mặc định lànull.PreserveEmbeddedMessageFormat- cho biết có nên bảo tồn định dạng của các tin nhắn nhúng không. Mặc định làfalse.RemoveSignature- chỉ định có nên xóa chữ ký khi tải không. Mặc định làfalse.
Bảo tồn Định dạng Tin nhắn Nhúng khi Tải
// 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);
Lưu và Chuyển Đổi Tin nhắn Email
Aspose.Email giúp dễ dàng chuyển đổi bất kỳ loại tin nhắn nào sang định dạng khác. Để minh họa tính năng này, mã trong bài viết này tải ba loại tin nhắn từ đĩa và lưu chúng lại ở các định dạng khác. Lớp cơ sở SaveOptions và các lớp EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions cho các cài đặt bổ sung khi lưu MailMessage có thể được sử dụng để lưu tin nhắn sang các định dạng khác. Bài viết trình bày cách sử dụng các lớp này để lưu một email mẫu dưới dạng:
- Định dạng EML.
- Outlook MSG.
- Định dạng MHTML.
- Định dạng HTML.
Tải và Lưu một Tin nhắn Email
Đoạn mã sau cho bạn thấy cách tải một tin nhắn EML và lưu nó vào đĩa ở cùng định dạng.
// 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());
Ranh giới của Tin nhắn Gốc
Khi một tin nhắn EML được tải và lưu lại dưới dạng EML, các ranh giới của tin nhắn nguồn được giữ nguyên theo mặc định, vì vậy không cần cấu hình để giữ chúng. Các phiên bản trước của thư viện đã cung cấp một EmlSaveOptions.PreserveOriginalBoundaries thuộc tính cho mục đích này. Thuộc tính đó đã bị loại bỏ, và việc bảo tồn các ranh giới gốc hiện là hành vi mặc định.
Tùy chỉnh các Ranh giới MIME
Nếu bạn cần các ranh giới tuân theo một mẫu cụ thể, hãy sử dụng BoundariesTemplate thuộc tính của EmlSaveOptions class. Nó định nghĩa mẫu được sử dụng để tạo ra các ranh giới MIME cho tin nhắn đã lưu. Giá trị mặc định là null, có nghĩa là các ranh giới được tạo ra bởi thư viện. Hai ký tự đại diện được hỗ trợ trong mẫu:
{#}- số ranh giới.{guid}- một GUID được tạo.
Đoạn mã sau cho bạn thấy cách tải EML và lưu nó dưới dạng EML với các ranh giới MIME tùy chỉnh.
// 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);
Lưu dưới dạng EML Giữ lại Tệp đính kèm TNEF
Đoạn mã dưới đây cho bạn thấy cách lưu dưới dạng EML giữ lại các tệp đính kèm 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);
Lưu EML sang MSG
Đoạn mã dưới đây cho bạn thấy cách tải một tin nhắn EML và chuyển đổi nó sang MSG bằng tùy chọn thích hợp từ 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());
Lưu EML sang MSG với Ngày được Bảo tồn
Cái MsgSaveOptions lớp cho phép bạn lưu tin nhắn nguồn dưới dạng tệp Outlook Message (MSG) giữ nguyên các ngày. Đoạn mã sau cho bạn thấy cách lưu một tin nhắn dưới dạng MSG với các ngày gốc được bảo tồn.
// 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);
Lưu EML dưới dạng MHTML
Các tùy chọn khác nhau của MHTML có thể được sử dụng để đạt được kết quả mong muốn. Đoạn mã dưới đây cho bạn cách tải một tin nhắn EML vào MailMessage và chuyển đổi nó sang MHTML với ngày tin nhắn theo hệ thống 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);
}
Định dạng Tiêu đề MHT Toàn cục khi Lưu từ EML
Với Aspose.Email, bạn có thể sử dụng các tùy chọn định dạng toàn cục cho tiêu đề Mht. Các tùy chọn toàn cục thiết lập định dạng chung của tiêu đề Mht cho tất cả MhtSaveOptions các thể hiện. Tính năng này được thiết kế để tránh việc đặt định dạng cho mỗi thể hiện của MhtSaveOptions.
Sử dụng các phương thức sau của GlobalFormattingOptions lớp và mẫu mã dưới đây để đặt định dạng cho tiêu đề Mht:
- setPageHeaderFormat(String value) - PageHeaderFormat cho các thể hiện của HeadersFormattingOptions nếu DefaultPageHeaderFormat chưa được đặt.
- setHeaderFormat(String value) - HeaderFormat cho các thể hiện của HeadersFormattingOptions nếu DefaultHeaderFormat chưa được đặt.
- setBeforeHeadersFormat(String value) - BeforeHeadersFormat cho các thể hiện của HeadersFormattingOptions nếu BeforeHeadersFormat chưa được đặt.
- setAfterHeadersFormat(String value) - AfterHeadersFormat cho các thể hiện của HeadersFormattingOptions nếu AfterHeadersFormat chưa được đặt.
// 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();
Chuyển đổi EML sang MHTML với Các Cài đặt Tùy chọn
Cái MhtSaveOptions lớp cung cấp các tùy chọn bổ sung để lưu các tin nhắn email ở định dạng MHTML. Bộ liệt kê MhtFormatOptions cho phép ghi thêm thông tin email vào MHTML đầu ra. Các trường bổ sung sau có thể được ghi:
- WriteHeader - ghi tiêu đề email vào tệp đầu ra.
- WriteOutlineAttachments - ghi các tệp đính kèm dạng outline vào tệp đầu ra.
- WriteCompleteEmailAddress - ghi đầy đủ địa chỉ email vào tệp đầu ra.
- NoEncodeCharacters - không nên sử dụng mã hóa chuyển đổi cho ký tự.
- HideExtraPrintHeader - ẩn tiêu đề in thêm ở đầu tệp đầu ra.
- WriteCompleteToEmailAddress - ghi đầy đủ địa chỉ email người nhận vào tệp đầu ra.
- WriteCompleteFromEmailAddress - ghi đầy đủ địa chỉ email người gửi vào tệp đầu ra.
- WriteCompleteCcEmailAddress - ghi đầy đủ địa chỉ email của các người nhận CC vào tệp đầu ra.
- WriteCompleteBccEmailAddress - ghi đầy đủ địa chỉ email của các người nhận BCC vào tệp đầu ra.
- RenderCalendarEvent - ghi văn bản từ sự kiện lịch vào tệp đầu ra.
- SkipByteOrderMarkInBody - bỏ qua các byte Byte Order Mark (BOM) trong phần nội dung tin nhắn.
- RenderVCardInfo - ghi văn bản từ chế độ xem thay thế VCard vào tệp đầu ra.
- DisplayAsOutlook - hiển thị tiêu đề From.
- RenderTaskFields - ghi các trường nhiệm vụ cụ thể vào tệp đầu ra.
- None - không có cài đặt nào được chỉ định.
Đoạn mã dưới đây cho bạn thấy cách chuyển đổi các tệp EML sang MHTML với các cài đặt tùy chọn.
// 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);
Render Sự kiện Lịch khi Chuyển đổi sang MHTML
Cái MhtFormatOptions.RenderCalendarEvent tùy chọn này hiển thị các sự kiện lịch vào đầu ra MHTML. Đoạn mã sau cho bạn thấy cách hiển thị các sự kiện lịch khi chuyển đổi sang 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);
Xuất Email sang MHT mà không có Hình ảnh Nhúng
// 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);
Xuất Email sang MHT với TimeZone Tùy chỉnh
Cái MailMessage class cung cấp setTimeZoneOffset phương thức để thiết lập múi giờ tùy chỉnh khi xuất ra MHT. Đoạn mã sau cho bạn thấy cách xuất email sang MHT với múi giờ tùy chỉnh.
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);
Xuất Email sang EML
Đoạn mã dưới đây cho bạn thấy cách xuất email sang định dạng 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());
Lưu Email dưới dạng HTML
Cái HtmlSaveOptions lớp cho phép bạn xuất nội dung tin nhắn ra HTML. Đoạn mã dưới đây cho bạn thấy cách lưu một tin nhắn dưới dạng 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);
Lưu Tin nhắn Email dưới dạng HTML với Đường dẫn Tương đối tới Tài nguyên
Khi xuất tin nhắn email sang định dạng HTML, có thể chọn lưu tài nguyên email với đường dẫn tương đối. Tính năng này cung cấp sự linh hoạt hơn trong cách liên kết tài nguyên trong tệp HTML đầu ra, giúp việc chia sẻ và hiển thị email đã lưu trên các hệ thống khác nhau dễ dàng hơn. HtmlSaveOptions.UseRelativePathToResources thuộc tính cung cấp khả năng lưu tài nguyên với đường dẫn tương đối. Giá trị mặc định của thuộc tính là false (các tài nguyên được lưu với đường dẫn tuyệt đối). Khi đặt thành true, các tài nguyên được lưu với đường dẫn tương đối. Tệp HTML với đường dẫn tương đối dễ di chuyển hơn và có thể được xem đúng bất kể cấu trúc tệp môi trường máy chủ. Bạn có thể chọn giữa đường dẫn tuyệt đối và tương đối tùy theo yêu cầu. Bạn có thể định nghĩa các đường dẫn tùy chỉnh cho tài nguyên bằng cách sử dụng ResourceHtmlRenderingHandler sự kiện.
Lưu với Đường dẫn Tương đối Mặc định tới Tài nguyên
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);
Trong trường hợp này, các tài nguyên được lưu trong [html file name].files thư mục, cùng đường dẫn với tệp .html, và HTML tham chiếu tới các tài nguyên qua đường dẫn tương đối.
Lưu với Đường dẫn Tuyệt đối tới Tài nguyên
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);
Như trong trường hợp đầu tiên, các tài nguyên được lưu trong [html file name].files thư mục mặc định, nhưng HTML tham chiếu tới các tài nguyên bằng đường dẫn tuyệt đối.
Đường dẫn Tương đối Tùy chỉnh sử dụng Sự kiện 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);
Bằng cách sử dụng ResourceHtmlRenderingHandler sự kiện, bạn có thể đặt đường dẫn tài nguyên tùy chỉnh dưới dạng tương đối hoặc tuyệt đối. Khi tùy chỉnh đường dẫn với ResourceHtmlRenderingHandler bộ xử lý sự kiện, gán một đường dẫn tương đối cho PathToResourceFile thuộc tính khi UseRelativePathToResources được đặt thành true, và một đường dẫn tuyệt đối khi nó là false, để đảm bảo tham chiếu đúng.
Giữ lại Biểu tượng Tùy chỉnh trong Tin nhắn khi Chuyển đổi sang HTML
Đôi khi một tin nhắn chứa các tệp đính kèm nội tuyến được hiển thị dưới dạng hình ảnh biểu tượng trong phần nội dung. Những tin nhắn như vậy có thể gây ra vấn đề khi chuyển đổi sang HTML, vì các hình ảnh biểu tượng bị mất. Điều này do các biểu tượng đính kèm không được lưu trữ trực tiếp trong tin nhắn.
Aspose.Email cho phép bạn tùy chỉnh các biểu tượng cho tệp đính kèm khi chuyển đổi tin nhắn sang HTML. Các ResourceHtmlRenderingHandler sự kiện được sử dụng để tùy chỉnh việc hiển thị các tệp tài nguyên (như tệp đính kèm) khi lưu tin nhắn email dưới dạng tệp HTML. Trong mẫu mã dưới đây, trình xử lý sự kiện được dùng để đặt đường dẫn tới các tệp tài nguyên (icon) một cách động dựa trên loại nội dung của tệp đính kèm. Điều này cho phép tùy chỉnh việc hiển thị tài nguyên trong HTML đầu ra dựa trên loại tệp.
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);
Đặt Thời gian và Múi giờ khi Lưu EML dưới dạng HTML
người dùng Aspose.Email có thể thiết lập các định dạng hiển thị thời gian và múi giờ trong HtmlSaveOptions. Thuộc tính HeadersFormattingOptions lớp cho phép bạn chỉ định các tùy chọn định dạng tiêu đề khi lưu MailMessage sang định dạng MHTML hoặc HTML. Các hằng số sau của HtmlFormatOptions lớp xác định các trường sẽ được hiển thị trong tệp đầu ra:
- RenderCalendarEvent - chỉ ra rằng văn bản từ sự kiện lịch nên được viết vào HTML đầu ra.
- RenderVCardInfo - chỉ ra rằng văn bản từ chế độ xem thay thế VCard nên được viết vào HTML đầu ra.
Đoạn mã mẫu sau cho thấy cách đặt thời gian và múi giờ khi lưu EML sang 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);
Lưu dưới dạng HTML mà không nhúng tài nguyên
Mặc định, các tài nguyên của tin nhắn được nhúng vào HTML đầu ra dưới dạng data URI. Đặt ResourceRenderingMode đến ResourceRenderingMode.SaveToFile để ghi chúng như các tệp riêng biệt bên cạnh 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);
Lưu Tin nhắn dưới dạng Tệp Mẫu Outlook (.oft)
Một mẫu Outlook là định dạng mức MAPI, vì vậy tin nhắn được chuyển đổi thành một MapiMessage đầu tiên và sau đó được viết với saveAsTemplate. Đoạn mã sau cho bạn thấy cách lưu một tin nhắn dưới dạng tệp mẫu 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");