使用 Outlook 日历项目

使用 MapiCalendar

Aspose.Email 的 MapiCalendar 该类提供设置日历项各种属性的方法和属性。本文提供了以下代码示例:

创建并保存日历项

以下代码片段演示了如何创建并以 ICS 格式保存日历项。

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

Calendar cal = Calendar.getInstance();
cal.set(2012, Calendar.OCTOBER, 2, 13, 0, 0);
Date startDate = cal.getTime();
cal.set(2012, Calendar.OCTOBER, 2, 14, 0, 0);
Date endDate = cal.getTime();

MapiCalendar calendar = new MapiCalendar("LAKE ARGYLE WA 6743", 
                                         "Appointment", 
                                         "This is a very important meeting :)", 
                                         startDate, 
                                         endDate);

calendar.save(dataDir + "CalendarItem_out.ics", AppointmentSaveFormat.Ics);

将日历项保存为 MSG

以下代码片段演示了如何将日历项保存为 MSG。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
calendar.save(dataDir + "CalendarItemAsMSG_out.Msg", AppointmentSaveFormat.Msg);

保存日历项为 ICS 时设置产品 ID

ProductIdentifier 属性的 MapiCalendarIcsSaveOptions 该类用于将 MAPI 日历项保存为 iCalendar (ICS) 文件,保留原始的日期和时间信息以及自定义产品标识符。此属性指定创建 iCalendar 对象的产品标识符。

以下代码示例展示了如何在 MAPI 日历对象中处理 iCalendar (ICS) 数据:

MapiCalendarIcsSaveOptions icsSaveOptions = new MapiCalendarIcsSaveOptions();
icsSaveOptions.setKeepOriginalDateTimeStamp(true);
icsSaveOptions.setProductIdentifier("Foo Ltd");

mapiCalendar.save("my.ics", icsSaveOptions);

向日历添加显示提醒

以下代码片段演示了如何向日历添加显示提醒。

// 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/";
Calendar jCalendar = Calendar.getInstance();
jCalendar.add(Calendar.HOUR, 1);
Date startDate = jCalendar.getTime();
Date endDate = jCalendar.getTime();

MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("attendee@domain.com", "Attendee"));
// Create Appointment
Appointment app = new Appointment("Home", 
                                  startDate, 
                                  endDate, 
                                  new MailAddress("organizer@domain.com", "Organizer"), 
                                  attendees);
MailMessage msg = new MailMessage();
msg.addAlternateView(app.requestApointment());
MapiMessage mapi = MapiMessage.fromMailMessage(msg);
MapiCalendar calendar = (MapiCalendar) mapi.toMapiMessageItem();

// Set calendar Properties
calendar.setReminderSet(true);
calendar.setReminderDelta(5); // 45 min before start of event

String savedFile = (dataDir + "calendarWithDisplayReminder.ics");
calendar.save(savedFile, AppointmentSaveFormat.Ics);

向日历添加音频提醒

以下代码片段演示了如何向日历添加音频提醒。

// 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/";
Calendar jCalendar = Calendar.getInstance();
jCalendar.add(Calendar.HOUR, 1);
Date startDate = jCalendar.getTime();
Date endDate = jCalendar.getTime();

MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("attendee@domain.com", "Attendee"));

Appointment app = new Appointment("Home", 
                                  startDate, 
                                  endDate, 
                                  new MailAddress("organizer@domain.com", "Organizer"), 
                                  attendees);

MailMessage msg = new MailMessage();
msg.addAlternateView(app.requestApointment());
MapiMessage mapi = MapiMessage.fromMailMessage(msg);
MapiCalendar cal = (MapiCalendar) mapi.toMapiMessageItem();

cal.setReminderSet(true);
cal.setReminderDelta(58); // 58 min before start of event
cal.setReminderFileParameter(dataDir + "Alarm01.wav");

String savedFile = dataDir + "calendarWithAudioReminder_out.ics";
cal.save(savedFile, AppointmentSaveFormat.Ics);

添加/检索日历文件附件

以下代码片段演示了如何添加/检索日历文件中的附件。

// 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/";
Calendar jCalendar = Calendar.getInstance();
jCalendar.add(Calendar.HOUR, 1);
Date startDate = jCalendar.getTime();
Date endDate = jCalendar.getTime();

String[] files = new String[3];
files[0] = "attachment_1.doc";
files[1] = "download.png";
files[2] = "Desert.jpg";

MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("attendee@domain.com", "Attendee"));

Appointment app1 = new Appointment("Home", 
                                   startDate, 
                                   endDate, 
                                   new MailAddress("organizer@domain.com", "Organizer"), 
                                   attendees);
for (String file : files) {
    app1.getAttachments().addItem(new Attachment(dataDir + file));
}

app1.save(dataDir + "appWithAttachments_out.ics", AppointmentSaveFormat.Ics);

Appointment app2 = Appointment.load(dataDir + "appWithAttachments_out.ics");
System.out.println(app2.getAttachments().size());
for (Attachment att : app2.getAttachments())
    System.out.println(att.getName());

会议请求中收件人的状态

以下代码片段演示了如何获取会议请求中收件人的状态。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";

MapiMessage message = MapiMessage.fromFile(fileName);
for (MapiRecipient recipient : message.getRecipients()) {
    System.out.println(recipient.getRecipientTrackStatus());
}

显式设置日历约会状态

Aspose.Email 允许您设置 MapiCalendar 对象显式设置,覆盖默认行为。该 setStateForced 方法提供了对日历事件状态更好的控制,特别是在处理已收到的会议请求时。默认情况下,当创建会议时,其状态是 MapiCalendarState.Meeting。当收到收件人收件箱时,它会自动更改为 MapiCalendarState.Received,其消息类已更新为 IPM.Schedule.Meeting.Request. 使用 SetStateForced 允许手动将状态设置为 Received,这在将日历保存为 MSG 文件时有助于保留组织者信息。但这可能会阻止会议的正常转发或重新发送。

以下代码示例将帮助您实现此功能:

Calendar c = Calendar.getInstance();
c.set(2024, Calendar.MAY, 10, 12, 30, 0);
Date startDate = c.getTime();
c.set(2024, Calendar.MAY, 10, 13, 30, 0);
Date endDate = c.getTime();
MapiCalendar appointment = new MapiCalendar(
        "LAKE ARGYLE WA 6743",
        "Appointment",
        "This is a very important meeting :)",
        startDate,
        endDate);

MapiElectronicAddress organizer = new MapiElectronicAddress();
organizer.setEmailAddress("test@aaa.com");
organizer.setDisplayName("test display Name");
appointment.setOrganizer(organizer);

appointment.setStateForced(MapiCalendarState.Meeting | MapiCalendarState.Received);

appointment.save("appointment.msg", AppointmentSaveFormat.Msg);

从标准时区创建 MapiCalendarTimeZone

以下代码片段演示了如何创建 MapiCalendarTimeZone 来自标准时区。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone("Eastern Standard Time");

使用已创建的约会设置提醒

在创建约会时可以添加提醒。这些闹钟可以基于不同的条件触发,例如在计划开始前 n 分钟,或每隔 n 间隔重复 n 次。可以使用不同的标签在约会内的 BEGIN:VALARM 与 END:VALARM 包括的脚本中创建这些触发器。约会的提醒设置有多种变体。

通过添加标签设置提醒

以下代码片段演示了如何通过添加标签来设置提醒。

// 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 = RunExamples.getDataDir_Outlook();
long MIN_MS = 60 * 1000;
long HR_MS = 60 * MIN_MS;
long DAY_MS = 24 * HR_MS;

String location = "Meeting Location: Room 5";
Date startDate = getDate(1997, 3, 18, 18, 30, 00);
Date endDate = getDate(1997, 3, 18, 19, 30, 00);
MailAddress organizer = new MailAddress("aaa@amail.com", "Organizer");
MailAddressCollection attendees = new MailAddressCollection();
attendees.addItem(new MailAddress("bbb@bmail.com", "First attendee"));

Appointment target = new Appointment(location, startDate, endDate, organizer, attendees);

// Audio alarm that will sound at a precise time and
// Repeat 4 more times at 15 minute intervals:
AppointmentReminder audioReminder = new AppointmentReminder();
audioReminder.setTrigger(new ReminderTrigger(getDate(1997, 3, 17, 13, 30, 0)));
audioReminder.setRepeat(4);
audioReminder.setDuration(new ReminderDuration(15 * MIN_MS));
audioReminder.setAction(ReminderAction.Audio);
ReminderAttachment attach = new ReminderAttachment(new URI("ftp://Host.com/pub/sounds/bell-01.aud"));
audioReminder.getAttachments().addItem(attach);
target.getReminders().addItem(audioReminder);


// Display alarm that will trigger 30 minutes before the
// Scheduled start of the event it is
// Associated with and will repeat 2 more times at 15 minute intervals:
AppointmentReminder displayReminder = new AppointmentReminder();
ReminderDuration dur = new ReminderDuration(-30 * MIN_MS);
displayReminder.setTrigger(new ReminderTrigger(dur, ReminderRelated.Start));
displayReminder.setRepeat(2);
displayReminder.setDuration(new ReminderDuration(15 * MIN_MS));
displayReminder.setAction(ReminderAction.Display);
displayReminder.setDescription("Breakfast meeting with executive team at 8:30 AM EST");
target.getReminders().addItem(displayReminder);

// Email alarm that will trigger 2 days before the
// Scheduled due date/time. It does not
// Repeat. The email has a subject, body and attachment link.
AppointmentReminder emailReminder = new AppointmentReminder();
ReminderDuration dur1 = new ReminderDuration(-2 * DAY_MS);
emailReminder.setTrigger(new ReminderTrigger(dur1, ReminderRelated.Start));
ReminderAttendee attendee = new ReminderAttendee("john_doe@host.com");
emailReminder.getAttendees().addItem(attendee);
emailReminder.setAction(ReminderAction.Email);
emailReminder.setSummary("REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING");
emailReminder.setDescription("A draft agenda needs to be sent out to the attendees to the weekly managers meeting (MGR-LIST). Attached is a pointer the document template for the agenda file.");
ReminderAttachment attach1 = new ReminderAttachment(new URI("http://Host.com/templates/agenda.doc"));
emailReminder.getAttachments().addItem(attach1);
target.getReminders().addItem(emailReminder);

// Procedural alarm that will trigger at a precise date/time
// And will repeat 23 more times at one hour intervals. The alarm will
// Invoke a procedure file.
AppointmentReminder procReminder = new AppointmentReminder();
procReminder.setTrigger(new ReminderTrigger(getDate(1998, 1, 1, 5, 0, 0)));
procReminder.setRepeat(23);
procReminder.setDuration(new ReminderDuration(1 * DAY_MS));
procReminder.setAction(ReminderAction.Procedure);
ReminderAttachment attach2 = new ReminderAttachment(new URI("ftp://Host.com/novo-procs/felizano.exe"));
procReminder.getAttachments().addItem(attach2);
target.getReminders().addItem(procReminder);
target.save(dataDir + "savedFile_out.ics");

将预约 EML 转换为带 HTML 正文的 MSG

自 19.3 版起,Aspose.Email 提供将预约 EML 转换为 MSG 的功能,并保留预约的 HTML 正文。Aspose.Email 提供了一个 MapiConversionOptions.ForcedRtfBodyForAppointment 属性,其默认值为 true. 当该值 MapiConversionOptions.ForcedRtfBodyForAppointment 被设置为 true 时,约会正文会转换为 RTF 格式。要保持约会正文的 HTML 格式,请设置 MapiConversionOptions.ForcedRtfBodyForAppointment 为 false.

以下示例演示了使用 MapiConversionOptions.ForcedRtfBodyForAppointment 属性,用于保持约会正文的 HTML 格式。

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

MailMessage mailMessage = MailMessage.load(dataDir + "TestAppointment.eml");

MapiConversionOptions conversionOptions = new MapiConversionOptions();
conversionOptions.setFormat(OutlookMessageFormat.Unicode);

// default value for ForcedRtfBodyForAppointment is true
conversionOptions.setForcedRtfBodyForAppointment(false);

MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage, conversionOptions);

if (mapiMessage.getBodyType() == BodyContentType.Html) {
    System.out.println("Body Type: Html");
} else if (mapiMessage.getBodyType() == BodyContentType.Rtf) {
    System.out.println("Body Type: Rtf");
}

mapiMessage.save(dataDir + "TestAppointment_out.msg");