将 ICS 转换为其他格式
将ICS转换为EML
对于日历事件或预约的表示,Aspose.Email 具有 Appointment 类。以下代码示例演示了将ICS转换为EML的过程:
- 使用以下方式加载待转换的 ICS 文件: Appointment.load 方法。
- 创建一个新的 MailMessage 用于保存日历数据的对象。
- 使用以下方式将来自 ICS 文件的预约添加为 EML 的备用视图:
requestApointment()方法。 - 使用以下方式保存带有转换数据的 EML 文件: 保存 使用… 方法 EmlSaveOptions 将保存类型指定为
EmlFormat.
// Load the ICS file to be converted
Appointment ics = Appointment.load("My File.ics");
// Create an EML
MailMessage eml = new MailMessage();
// Add the appointment to the EML
eml.addAlternateView(ics.requestApointment());
// Save the EML
eml.save("Saved File.eml", new EmlSaveOptions(MailMessageSaveType.getEmlFormat()));
将ICS转换为EMLX
要将 ICS 文件转换为 EMLX 格式,请遵循 将ICS转换为EML 章节并按以下代码行保存 .emlx 文件:
Appointment ics = Appointment.load("My File.ics");
MailMessage eml = new MailMessage();
eml.addAlternateView(ics.requestApointment());
// Save as an EMLX
eml.save("Saved File.emlx", new EmlSaveOptions(MailMessageSaveType.getEmlxFormat()));
将ICS转换为HTML
以下代码示例演示了转换过程:
- 使用以下方式加载待转换的 ICS 文件: Appointment.load 方法。
- 创建一个新的 MailMessage 用于保存日历数据的对象。
- 使用以下方式将来自 ICS 文件的预约添加为 EML 的备用视图:
requestApointment()方法。 - 使用以下方式保存带有转换数据的 EML 文件: 保存 使用… 方法 HtmlSaveOptions 为已保存的 HTML 文件提供格式选项。在此特定情况下,
HtmlFormatOptions.WriteHeader用于在输出文件中包含 HTML 头部,而HtmlFormatOptions.RenderCalendarEvent用于以适合显示的格式渲染 EML 消息中包含的所有日历事件。
// Load the ICS file to be converted
Appointment ics = Appointment.load("My File.ics");
// Create an EML
MailMessage eml = new MailMessage();
// Add the appointment to the EML
eml.addAlternateView(ics.requestApointment());
// Save the EML as HTML
HtmlSaveOptions htmlSaveOptions = new HtmlSaveOptions();
htmlSaveOptions.setHtmlFormatOptions(HtmlFormatOptions.WriteHeader | HtmlFormatOptions.RenderCalendarEvent);
eml.save("Saved File.html", htmlSaveOptions);
使用该枚举的其他值 HtmlFormatOptions 枚举和属性 HtmlSaveOptions 用于根据需要设置格式选项的类。
将ICS转换为MBOX
以下代码示例演示了将 ICS 转换为 MBOX 的过程。它加载一个 ICS 文件,创建一个 EML 消息,将来自该 ICS 文件的预约详情添加到 EML 消息中,然后将 EML 消息写入 MBOX 存储文件。
- 使用以下方式加载待转换的 ICS 文件: Appointment.load 方法。
- 创建一个新的 MailMessage 用于保存日历数据的对象。
- 使用以下方式将来自 ICS 文件的预约添加为 EML 的备用视图:
requestApointment()方法。 - 创建一个新的 MboxrdStorageWriter 对象。
- 通过将消息内容以 MBOX 格式写入指定的 MBOX 文件,将 EML 消息添加到存储中。
// Load the ICS file to be converted
Appointment ics = Appointment.load("My File.ics");
// Create an EML
MailMessage eml = new MailMessage();
// Add the appointment to the EML
eml.addAlternateView(ics.requestApointment());
// Create an MBOX storage and add the EML to it
try (MboxrdStorageWriter mboxStorage = new MboxrdStorageWriter("Saved File.mbox", new MboxSaveOptions())) {
mboxStorage.writeMessage(eml);
}
将ICS转换为MHTML
以下代码示例演示了转换过程的所有步骤:
- 使用以下方式加载待转换的 ICS 文件: Appointment.load 方法。
- 创建一个新的 MailMessage 用于保存转换后数据的对象。
- 使用以下方式将来自 ICS 文件的预约添加为 EML 的备用视图:
requestApointment()方法。 - 使用以下方式保存带有转换数据的 EML 文件: 保存 使用… 方法 MhtSaveOptions 为 MHTML 文件提供保存选项。在此特定情况下,
MhtFormatOptions.WriteHeader用于在输出文件中包含电子邮件消息头,而MhtFormatOptions.RenderCalendarEvent用于以适合显示的格式渲染 EML 消息中包含的所有日历事件。
// Load the ICS file to be converted
Appointment ics = Appointment.load("My File.ics");
// Create an EML
MailMessage eml = new MailMessage();
// Add the appointment to the EML
eml.addAlternateView(ics.requestApointment());
// Save the EML as MHTML
MhtSaveOptions mhtSaveOptions = new MhtSaveOptions();
mhtSaveOptions.setMhtFormatOptions(MhtFormatOptions.WriteHeader | MhtFormatOptions.RenderCalendarEvent);
eml.save("Saved File.mht", mhtSaveOptions);
此方法可确保转换后的 MHTML 文件保留日历事件的细节和格式,从而在各种平台和电子邮件客户端之间实现高效的共享和查看。
随意使用该枚举的其他值 MhtFormatOptions 枚举和属性 MhtSaveOptions 用于根据需要设置格式选项的类。
将 ICS 转换为 MSG
将 ICS(iCalendar)文件转换为 MSG 格式对于提高与 Microsoft Outlook 的兼容性是合理的,因为 MSG 文件常用于存储邮件、约会和其他 Outlook 相关数据。以下代码示例演示如何加载 ICS 文件并将其保存为 MSG 文件而不丢失任何数据或格式:
- 使用以下方式加载 ICS 文件 Appointment.load 并创建一个 Appointment 来自文件中存储的日历数据的对象。
- 调用
save在 Appointment 对象上的方法,将从 ICS 文件加载的预约数据转换并保存为指定位置的 MSG 文件。
// Load the ICS file to be converted and save it as an MSG
Appointment.load("My File.ics").save("Saved File.msg", AppointmentSaveFormat.Msg);
将ICS转换为OFT
该过程包括加载 ICS 文件并将其保存为 MSG 文件,然后进一步转换为 OFT 格式:
- 创建一个新的流对象,将预约数据存储在内存中。
- 加载约会数据自 ICS 文件。使用以下方式将约会数据以 MSG 格式保存到流对象中
save()方法。 - 使用以下方式创建新的 MapiMessage 对象,从流中加载预约数据 MapiMessage.load() 方法。
- 使用以下方式将加载的 MapiMessage 数据保存为 Outlook 模板文件
save()使用… 方法SaveOptions.getDefaultOft()格式选项。
// Load the ICS file to be converted and save it as an MSG into a stream
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
Appointment.load("My File.ics").save(msgStream, AppointmentSaveFormat.Msg);
// Save the MSG as an OFT
MapiMessage.load(new ByteArrayInputStream(msgStream.toByteArray()))
.save("Saved File.oft", SaveOptions.getDefaultOft());
将ICS转换为OST
Aspose.Email 提供了加载 ICS 文件、保存为 MSG 文件、打开 OST 文件、访问文件内日历文件夹并轻松将 MSG 文件添加到日历文件夹的功能:
- 创建一个流以存储预约数据。
- 使用…从 ICS 文件加载约会数据 Appointment.load() 方法,并使用…以 MSG 格式保存到流中
save方法。 - 使用以下方式加载个人存储文件 fromFile 方法的 PersonalStorage 类。传入
true作为第二个参数打开存储进行写入。 - 使用以下方式从个人存储文件检索日历文件夹
getPredefinedFolder方法。 - 使用
addMessage方法,将约会消息添加到 OST 文件的日历文件夹中。
// Load the ICS file to be converted and save it as an MSG into a stream
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
Appointment.load("My File.ics").save(msgStream, AppointmentSaveFormat.Msg);
// Open an OST file
try (PersonalStorage pst = PersonalStorage.fromFile("Saved File.ost", true)) {
// Get the calendar folder
FolderInfo calendarFolder = pst.getPredefinedFolder(StandardIpmFolder.Appointments);
// Add the MSG to the calendar folder
calendarFolder.addMessage(MapiMessage.load(new ByteArrayInputStream(msgStream.toByteArray())));
}
将 ICS 转换为 PST
以下代码示例演示了转换过程:
- 创建一个流来保存约会数据。
- 使用…从 ICS 文件加载约会数据 Appointment.load() 方法,并使用…以 MSG 格式保存到流中
save方法。 - 使用…创建具有指定文件名的新 PST 文件 PersonalStorage.create 方法。
- 使用…在 PST 文件中创建用于存储约会的日历文件夹
createPredefinedFolder方法。 - 使用…将约会消息添加到 PST 文件中的日历文件夹
addMessage方法。
// Load the ICS file to be converted and save it as an MSG into a stream
ByteArrayOutputStream msgStream = new ByteArrayOutputStream();
Appointment.load("My File.ics").save(msgStream, AppointmentSaveFormat.Msg);
// Create a PST file
try (PersonalStorage pst = PersonalStorage.create("Saved File.pst", FileFormatVersion.Unicode)) {
// Create a calendar folder
FolderInfo calendarFolder = pst.createPredefinedFolder("Calendar", StandardIpmFolder.Appointments);
// Add the MSG to the calendar folder
calendarFolder.addMessage(MapiMessage.load(new ByteArrayInputStream(msgStream.toByteArray())));
}