Làm việc với các mục Lịch Outlook
Outlook lưu các cuộc hẹn và họp như các mục lịch. Aspose.Email cho C++ đại diện cho một mục như vậy bằng MapiCalendar lớp: nó có thể được tạo từ đầu, có nhắc nhở và múi giờ, và lưu dưới định dạng MSG của Outlook hoặc dưới dạng tệp iCalendar (ICS). Một số tính năng của định dạng ICS, như nhiều báo thức độc lập hoặc tệp nhúng, được đạt được thông qua tiêu chuẩn Appointment lớp thay vì; cả hai xuất hiện bên dưới. Các mục lịch được lưu trong tệp PST được đề cập trong Làm việc với mục Lịch trong tệp PST.
Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:
#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <Calendar/Appointment.h>
#include <Calendar/AppointmentSaveFormat.h>
#include <Calendar/AppointmentReminder.h>
#include <Calendar/AppointmentReminderCollection.h>
#include <Calendar/CalendarReader.h>
#include <Calendar/ReminderAction.h>
#include <Calendar/ReminderAttachment.h>
#include <Calendar/ReminderAttachmentCollection.h>
#include <Calendar/ReminderAttendee.h>
#include <Calendar/ReminderAttendeeCollection.h>
#include <Calendar/ReminderDuration.h>
#include <Calendar/ReminderRelated.h>
#include <Calendar/ReminderTrigger.h>
#include <Mapi/MapiCalendar.h>
#include <Mapi/MapiCalendarIcsSaveOptions.h>
#include <Mapi/MapiCalendarState.h>
#include <Mapi/MapiCalendarTimeZone.h>
#include <Mapi/MapiElectronicAddress.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiConversionOptions.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiRecipientTrackStatus.h>
#include <Mapi/OutlookMessageFormat.h>
#include <Mapi/BodyContentType.h>
#include <system/collections/list_ext.h>
#include <system/convert.h>
#include <system/object_ext.h>
#include <system/timezone_info.h>
#include <system/timespan.h>
#include <system/uri.h>
#include <system/io/file.h>
#include <system/io/memory_stream.h>
#include <system/io/path.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Calendar;
Tạo và Lưu một Mục Lịch
Bộ khởi tạo đơn giản nhất của MapiCalendar lấy vị trí, tiêu đề, nội dung và thời gian bắt đầu và kết thúc của sự kiện. Lưu ghi mục theo định dạng đã chọn với AppointmentSaveFormat: Ics tạo ra một tệp iCalendar mà hầu hết các chương trình lịch đều có thể nhập, Msg một mục Outlook.
// location, subject, body, start and end
SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"LAKE ARGYLE WA 6743", u"Appointment",
u"This is a very important meeting", DateTime(2025, 10, 2, 13, 0, 0), DateTime(2025, 10, 2, 14, 0, 0));
// iCalendar, readable by almost any calendar program
calendar->Save(u"CalendarItem.ics", AppointmentSaveFormat::Ics);
// Outlook's own format
calendar->Save(u"CalendarItem.msg", AppointmentSaveFormat::Msg);
Console::WriteLine(u"Saved: " + calendar->get_Subject());
Thêm Nhắc nhở
Một Nhắc nhở Hiển thị
Một mục Outlook có một nhắc nhở duy nhất. ReminderSet bật nó lên và ReminderDelta đặt số phút trước khi bắt đầu nó sẽ kích hoạt. Trong tệp ICS, nhắc nhở trở thành một VALARM khối; mẫu in ra các dòng chính - TRIGGER:-PT45M và ACTION:DISPLAY.
SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"Home", u"Dentist",
u"Bring the insurance card", DateTime(2025, 10, 2, 9, 0, 0), DateTime(2025, 10, 2, 10, 0, 0));
calendar->set_ReminderSet(true);
calendar->set_ReminderDelta(45); // minutes before the start of the event
calendar->Save(u"CalendarWithDisplayReminder.ics", AppointmentSaveFormat::Ics);
for (String line : IO::File::ReadAllLines(u"CalendarWithDisplayReminder.ics"))
{
if (line.StartsWith(u"BEGIN:VALARM") || line.StartsWith(u"TRIGGER") || line.StartsWith(u"ACTION"))
{
Console::WriteLine(line);
}
}
Một Nhắc nhở Âm thanh
ReminderFileParameter đặt tên tệp âm thanh để phát thay vì mặc định. Nhắc nhở sau đó được ghi dưới dạng báo thức âm thanh: ACTION:AUDIO với một ATTACH dòng chỉ tới tệp.
SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"Home", u"Dentist",
u"Bring the insurance card", DateTime(2025, 10, 2, 9, 0, 0), DateTime(2025, 10, 2, 10, 0, 0));
calendar->set_ReminderSet(true);
calendar->set_ReminderDelta(58); // minutes before the start of the event
calendar->set_ReminderFileParameter(u"Alarm01.wav"); // the sound to play instead of the default one
calendar->Save(u"CalendarWithAudioReminder.ics", AppointmentSaveFormat::Ics);
for (String line : IO::File::ReadAllLines(u"CalendarWithAudioReminder.ics"))
{
if (line.StartsWith(u"TRIGGER") || line.StartsWith(u"ACTION") || line.StartsWith(u"ATTACH"))
{
Console::WriteLine(line);
}
}
Nhiều Nhắc nhở trong một Sự kiện iCalendar
Định dạng iCalendar cho phép bất kỳ số lượng báo thức nào cho mỗi sự kiện, mỗi báo thức có trigger và hành động riêng. Appointment phơi bày chúng thông qua Nhắc nhở bộ sưu tập của AppointmentReminder các đối tượng:
- ReminderTrigger - hoặc là một thời điểm cố định hoặc một ReminderDuration liên quan tới thời điểm bắt đầu hoặc kết thúc của sự kiện;
RepeatvàDuration- bao nhiêu lần nữa báo thức sẽ phát và ở khoảng thời gian nào;- ReminderAction -
Audio,Display,EmailhoặcProcedure, với tệp âm thanh, văn bản, email hoặc chương trình làm nội dung.
Mẫu thêm một báo thức của mỗi loại; tệp đã lưu chứa bốn VALARM khối.
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"bbb@bmail.com", String(u"First attendee")));
SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"Meeting Location: Room 5",
DateTime(1997, 3, 18, 18, 30, 0), DateTime(1997, 3, 18, 19, 30, 0),
MakeObject<MailAddress>(u"aaa@amail.com", String(u"Organizer")), attendees);
// a sound at a fixed moment, repeated 4 more times every 15 minutes
SharedPtr<AppointmentReminder> audio = MakeObject<AppointmentReminder>();
audio->set_Trigger(MakeObject<ReminderTrigger>(DateTime(1997, 3, 17, 13, 30, 0, DateTimeKind::Utc)));
audio->set_Repeat(4);
audio->set_Duration(MakeObject<ReminderDuration>(TimeSpan(0, 15, 0)));
audio->set_Action(ReminderAction::Audio);
audio->get_Attachments()->Add(MakeObject<ReminderAttachment>(MakeObject<Uri>(u"ftp://Host.com/pub/sounds/bell-01.aud")));
appointment->get_Reminders()->Add(audio);
// a message 30 minutes before the start, repeated 2 more times every 15 minutes
SharedPtr<AppointmentReminder> display = MakeObject<AppointmentReminder>();
display->set_Trigger(MakeObject<ReminderTrigger>(MakeObject<ReminderDuration>(TimeSpan(0, -30, 0)), ReminderRelated::Start));
display->set_Repeat(2);
display->set_Duration(MakeObject<ReminderDuration>(TimeSpan(0, 15, 0)));
display->set_Action(ReminderAction::Display);
display->set_Description(u"Breakfast meeting with executive team at 8:30 AM EST");
appointment->get_Reminders()->Add(display);
// an email two days before the start, with a subject, a body and a link to a document
SharedPtr<AppointmentReminder> email = MakeObject<AppointmentReminder>();
email->set_Trigger(MakeObject<ReminderTrigger>(MakeObject<ReminderDuration>(TimeSpan(-2, 0, 0, 0)), ReminderRelated::Start));
email->get_Attendees()->Add(MakeObject<ReminderAttendee>(u"john_doe@host.com"));
email->set_Action(ReminderAction::Email);
email->set_Summary(u"REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING");
email->set_Description(u"A draft agenda needs to be sent out to the attendees of the weekly managers meeting.");
email->get_Attachments()->Add(MakeObject<ReminderAttachment>(MakeObject<Uri>(u"http://Host.com/templates/agenda.doc")));
appointment->get_Reminders()->Add(email);
// a program started at a fixed moment, repeated 23 more times every hour
SharedPtr<AppointmentReminder> procedure = MakeObject<AppointmentReminder>();
procedure->set_Trigger(MakeObject<ReminderTrigger>(DateTime(1998, 1, 1, 5, 0, 0, DateTimeKind::Utc)));
procedure->set_Repeat(23);
procedure->set_Duration(MakeObject<ReminderDuration>(TimeSpan(1, 0, 0)));
procedure->set_Action(ReminderAction::Procedure);
procedure->get_Attachments()->Add(MakeObject<ReminderAttachment>(MakeObject<Uri>(u"ftp://Host.com/novo-procs/felizano.exe")));
appointment->get_Reminders()->Add(procedure);
appointment->Save(u"AppointmentWithReminders.ics", AppointmentSaveFormat::Ics);
int32_t alarms = 0;
for (String line : IO::File::ReadAllLines(u"AppointmentWithReminders.ics"))
{
if (line == u"BEGIN:VALARM")
{
alarms++;
}
if (line.StartsWith(u"ACTION"))
{
Console::WriteLine(line);
}
}
Console::WriteLine(u"VALARM blocks: " + Convert::ToString(alarms));
Đặt Múi Giờ cho Một Sự Kiện
MapiCalendarTimeZone có thể được tạo trực tiếp từ một TimeZoneInfo đối tượng - ở đây là múi giờ của máy tính chạy chương trình. Được gán cho StartDateTimeZone và EndDateTimeZone, nó làm cho đầu ra ICS hiển thị thời gian ở múi giờ đó, ví dụ DTSTART;TZID=Europe/Moscow:20251002T160000.
// a MAPI time zone built from the time zone of this computer
SharedPtr<MapiCalendarTimeZone> timeZone = MakeObject<MapiCalendarTimeZone>(TimeZoneInfo::get_Local());
SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"Office", u"Planning",
u"Quarter planning", DateTime(2025, 10, 2, 13, 0, 0), DateTime(2025, 10, 2, 14, 0, 0));
calendar->set_StartDateTimeZone(timeZone);
calendar->set_EndDateTimeZone(timeZone);
calendar->Save(u"CalendarWithTimeZone.ics", AppointmentSaveFormat::Ics);
for (String line : IO::File::ReadAllLines(u"CalendarWithTimeZone.ics"))
{
if (line.StartsWith(u"TZID") || line.StartsWith(u"DTSTART") || line.StartsWith(u"BEGIN:VTIMEZONE"))
{
Console::WriteLine(line);
}
}
Thêm và Đọc Đính Kèm
Các tệp đính kèm vào một Appointment thông qua Tệp Đính Kèm bộ sưu tập được nhúng trong tệp ICS, vì vậy chúng đi kèm và trở lại khi tệp được mở bằng Appointment::Load.
SharedPtr<MailAddressCollection> attendees = MakeObject<MailAddressCollection>();
attendees->Add(MakeObject<MailAddress>(u"attendee@domain.com"));
SharedPtr<Appointment> appointment = MakeObject<Appointment>(u"Home",
DateTime(2025, 10, 2, 9, 0, 0), DateTime(2025, 10, 2, 10, 0, 0),
MakeObject<MailAddress>(u"organizer@domain.com"), attendees);
// attach files from disk
for (String file : { String(u"1.jpg"), String(u"portrait.jpg") })
{
appointment->get_Attachments()->Add(MakeObject<Attachment>(
MakeObject<IO::MemoryStream>(IO::File::ReadAllBytes(file)), IO::Path::GetFileName(file)));
}
appointment->Save(u"AppointmentWithAttachments.ics", AppointmentSaveFormat::Ics);
// the attachments are embedded in the ICS file and come back when it is loaded
SharedPtr<Appointment> loaded = Appointment::Load(u"AppointmentWithAttachments.ics");
Console::WriteLine(u"Attachments: " + Convert::ToString(loaded->get_Attachments()->get_Count()));
for (SharedPtr<Attachment> attachment : loaded->get_Attachments())
{
Console::WriteLine(u" " + attachment->get_Name());
}
Kiểm tra Trạng Thái Phản Hồi của Người Tham Dự Cuộc Họp
Mỗi người nhận lời mời họp ghi lại cách họ trả lời. MapiRecipient::get_RecipientTrackStatus trả về nó dưới dạng một MapiRecipientTrackStatus giá trị - None trong khi chưa có câu trả lời, thì Tentative, Accepted hoặc Declined.
SharedPtr<MapiMessage> message = MapiMessage::Load(u"Test_Meeting.msg");
for (SharedPtr<MapiRecipient> recipient : message->get_Recipients())
{
// None, Tentative, Accepted or Declined
Console::WriteLine(recipient->get_DisplayName() + u": " + ObjectExt::ToString(recipient->get_RecipientTrackStatus()));
}
Kiểm soát Đầu ra ICS của Một Mục Lịch
MapiCalendarIcsSaveOptions là đối tượng tùy chọn cho định dạng ICS. KeepOriginalDateTimeStamp ghi DTSTAMP của mục nguồn thay vì thời gian hiện tại, và ProductIdentifier đặt PRODID dòng ghi tên chương trình tạo ra tệp.
SharedPtr<MapiCalendar> calendar = MakeObject<MapiCalendar>(u"LAKE ARGYLE WA 6743", u"Appointment",
u"This is a very important meeting", DateTime(2025, 10, 2, 13, 0, 0), DateTime(2025, 10, 2, 14, 0, 0));
SharedPtr<MapiCalendarIcsSaveOptions> icsSaveOptions = MakeObject<MapiCalendarIcsSaveOptions>();
icsSaveOptions->set_KeepOriginalDateTimeStamp(true); // do not refresh DTSTAMP
icsSaveOptions->set_ProductIdentifier(u"Foo Ltd"); // the PRODID of the output file
calendar->Save(u"CalendarWithProductId.ics", icsSaveOptions);
Console::WriteLine(IO::File::ReadAllText(u"CalendarWithProductId.ics").Substring(0, 120));
Kiểm tra Tệp Lịch trước Khi Đọc
CalendarReader mô tả tệp đã mở trước khi một sự kiện duy nhất được đọc: Count là số lượng VEVENT thành phần, IsMultiEvents cho biết có hơn một không, Version trả về phiên bản iCalendar và Method các METHOD thuộc tính - PUBLISH, REQUEST, CANCEL và cứ như vậy. LoadAsMultiple trả về tất cả các sự kiện một lần thay vì duyệt qua từng cái.
SharedPtr<CalendarReader> reader = MakeObject<CalendarReader>(u"US-Holidays.ics");
Console::WriteLine(u"Events in the calendar: " + Convert::ToString(reader->get_Count()));
Console::WriteLine(u"iCalendar version: " + reader->get_Version());
Console::WriteLine(u"Method: " + Convert::ToString(static_cast<int32_t>(reader->get_Method())));
Console::WriteLine(u"More than one event: " + Convert::ToString(reader->get_IsMultiEvents()));
// all the events at once instead of one by one
auto appointments = reader->LoadAsMultiple();
Console::WriteLine(u"Loaded: " + Convert::ToString(appointments->get_Count()));
Giữ Phần Nội dung HTML và Đặt Trạng Thái Rõ Ràng
Hai chi tiết quan trọng khi một cuộc hẹn được chuyển thành mục Outlook.
Mặc định phần nội dung được chuyển thành RTF, vì Outlook lưu trữ như vậy cho các mục lịch. Đặt ForcedRtfBodyForAppointment đến false giữ phần nội dung HTML thay thế.
Trạng thái của một mục lịch thường tuân theo vòng đời của nó: một mục vừa được tạo là một Meeting, và nó trở thành Received khi nó đến hộp thư của người nhận. SetStateForced gán giá trị trực tiếp, đây là cách giữ thông tin người tổ chức khi một cuộc họp nhận được được lưu dưới dạng MSG. Khi đó mục có thể không còn phù hợp để chuyển tiếp hoặc gửi lại.
// keep the HTML body of an appointment instead of converting it to RTF
SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"Test_Meeting.msg");
SharedPtr<MapiConversionOptions> conversionOptions = MakeObject<MapiConversionOptions>();
conversionOptions->set_Format(OutlookMessageFormat::Unicode);
conversionOptions->set_ForcedRtfBodyForAppointment(false); // the default is true
SharedPtr<MapiMessage> mapiMessage = MapiMessage::FromMailMessage(mailMessage, conversionOptions);
Console::WriteLine(u"Body type: " + ObjectExt::ToString(mapiMessage->get_BodyType()));
// set the state of a calendar item explicitly
SharedPtr<MapiCalendar> appointment = MakeObject<MapiCalendar>(u"LAKE ARGYLE WA 6743", u"Appointment",
u"This is a very important meeting",
DateTime(2025, 5, 10, 12, 30, 0, DateTimeKind::Utc),
DateTime(2025, 5, 10, 13, 30, 0, DateTimeKind::Utc));
SharedPtr<MapiElectronicAddress> organizer = MakeObject<MapiElectronicAddress>();
organizer->set_EmailAddress(u"test@aaa.com");
organizer->set_DisplayName(u"test display Name");
appointment->set_Organizer(organizer);
// Meeting is the state of a new item; Received is what an item gets in the recipient's inbox
appointment->SetStateForced(MapiCalendarState::Meeting | MapiCalendarState::Received);
appointment->Save(u"ForcedState.msg", AppointmentSaveFormat::Msg);
Đọc và Đặt Định Danh Sản Phẩm của Mục Lịch
Một tệp iCalendar ghi lại sản phẩm tạo ra nó trong PRODID trường. Aspose.Email giữ định danh này khi mục lịch được chuyển đổi giữa ICS và MSG, vì vậy bạn có thể truy vết nguồn gốc của mục. Định danh có sẵn thông qua các thành viên sau:
Appointment::get_ProductId()vàAppointment::set_ProductId()- lấy hoặc đặt định danh của sản phẩm đã tạo một Appointment.MapiCalendar::get_ProductId()vàMapiCalendar::set_ProductId()- lấy hoặc đặt định danh của sản phẩm đã tạo một MapiCalendar.MapiCalendarSaveOptions::set_ProductIdentifier()- đặt định danh được ghi khi mục lịch được lưu bằng MapiCalendarMsgSaveOptions hoặc MapiCalendarIcsSaveOptions.
Đoạn mã mẫu sau đọc định danh sản phẩm từ tệp ICS, chuyển đổi cuộc hẹn sang MSG với AppointmentMsgSaveOptions, lưu mục lịch với định danh mới, và sau đó ghi định danh khác vào tệp ICS:
#include <Calendar/Appointment.h>
#include <Calendar/AppointmentMsgSaveOptions.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiCalendar.h>
#include <Mapi/MapiCalendarMsgSaveOptions.h>
#include <Mapi/MapiCalendarIcsSaveOptions.h>
#include <Mapi/MapiItemType.h>
#include <system/io/memory_stream.h>
#include <system/console.h>
using namespace System;
using namespace Aspose::Email::Calendar;
using namespace Aspose::Email::Mapi;
// Read the product identifier from an iCalendar file
SharedPtr<Appointment> appointment = Appointment::Load(u"meeting.ics");
Console::WriteLine(u"Appointment product ID: " + appointment->get_ProductId());
// The identifier is kept when the appointment is converted to MSG
auto msgStream = MakeObject<IO::MemoryStream>();
appointment->Save(msgStream, MakeObject<AppointmentMsgSaveOptions>());
msgStream->set_Position(0);
SharedPtr<MapiMessage> msg = MapiMessage::Load(msgStream);
if (msg->get_SupportedType() == MapiItemType::Calendar)
{
auto calendar = System::DynamicCast<MapiCalendar>(msg->ToMapiMessageItem());
Console::WriteLine(u"MapiCalendar product ID: " + calendar->get_ProductId());
// Save the calendar item to MSG with a different product identifier
auto options = MakeObject<MapiCalendarMsgSaveOptions>();
options->set_ProductIdentifier(u"-//Contoso//Calendar 2.0//EN");
calendar->Save(u"meeting_out.msg", options);
// The identifier set on the item is written to the PRODID field of an ICS file
calendar->set_ProductId(u"-//Contoso//Calendar 3.0//EN");
calendar->Save(u"meeting_out.ics", MakeObject<MapiCalendarIcsSaveOptions>());
}
Xem Thêm
- Quản lý Định kỳ - các mẫu lặp lại của mục lịch và nhiệm vụ Outlook.
- Quản lý Cuộc hẹn iCalendar - tiêu chuẩn
Appointmentlớp. - Làm việc với mục Lịch trong tệp PST - các mục lịch được lưu trong tệp PST.