การจัดการรายการปฏิทิน Outlook
บทความนี้ครอบคลุม Outlook MapiCalendar รายการปฏิทินที่บันทึกเป็น MSG เพื่อทำงานกับการนัดหมาย iCalendar (ICS) ตามมาตรฐานและคำขอประชุม ดู จัดการนัดหมาย; เพื่อจัดเก็บและอ่านรายการปฏิทินภายในไฟล์ PST ดู การจัดการรายการปฏิทินในไฟล์ PST.
Aspose.Email MapiCalendar คลาสนี้ให้เมธอดและแอตทริบิวต์เพื่อกำหนดคุณสมบัติต่าง ๆ ของรายการปฏิทิน ส่วนนี้ให้ตัวอย่างโค้ดสำหรับ:
- สร้างและบันทึกรายการปฏิทิน
- บันทึกรายการปฏิทินเป็นไฟล์ MSG
- บันทึก Product IDs สำหรับรายการ MAPI Calendar ไปยังไฟล์ ICS
- รับจำนวนเหตุการณ์ทั้งหมด
- เพิ่มการเตือนแบบแสดงผล
- เพิ่มการเตือนด้วยเสียง
- เพิ่ม/ดึงไฟล์แนบจากไฟล์ปฏิทิน
- ตรวจสอบสถานะของผู้รับในคำขอการประชุม
- สร้าง MAPI Calendar TimeZone จาก Standard Timezone
- ตั้งการเตือนความจำสำหรับนัดหมาย
- แปลง Appointment EML เป็น MSG ด้วยเนื้อหา HTML
- ตั้งค่าสถานะของรายการปฏิทิน MAPI ด้วยตนเอง
สร้างและบันทึกรายการปฏิทิน
โค้ดตัวอย่างต่อไปนี้แสดงวิธีสร้างและบันทึกรายการปฏิทินในรูปแบบ ICS.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Create the appointment
MapiCalendar calendar = new MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
new DateTime(2012, 10, 2, 13, 0, 0),
new DateTime(2012, 10, 2, 14, 0, 0));
calendar.Save(dataDir + "CalendarItem_out.ics", AppointmentSaveFormat.Ics);
บันทึกรายการปฏิทินเป็นไฟล์ MSG
โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกรายการปฏิทินเป็นไฟล์ MSG.
calendar.Save(dataDir + "CalendarItemAsMSG_out.Msg", AppointmentSaveFormat.Msg);
บันทึก Product IDs สำหรับรายการ MAPI Calendar ไปยังไฟล์ ICS
นี้ ProductIdentifier คุณสมบัติของ MapiCalendarIcsSaveOptions คลาสนี้ใช้เพื่อบันทึกรายการปฏิทิน MAPI ลงในไฟล์ iCalendar (ICS) โดยคงข้อมูลวันและเวลาต้นฉบับไว้รวมถึงตัวระบุผลิตภัณฑ์ที่กำหนดเอง. คุณสมบัตินี้ระบุรหัสของผลิตภัณฑ์ที่สร้างอ็อบเจ็กต์ iCalendar.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีทำงานกับข้อมูล iCalendar (ICS) ภายในอ็อบเจ็กต์ปฏิทิน MAPI:
var icsSaveOptions = new MapiCalendarIcsSaveOptions
{
KeepOriginalDateTimeStamp = true,
ProductIdentifier = "Foo Ltd"
};
mapiCalendar.Save("my.ics", icsSaveOptions);
รับจำนวนเหตุการณ์ทั้งหมด
นี้ CalendarReader คลาสช่วยให้จัดการเหตุการณ์ปฏิทินได้อย่างง่ายดาย คุณสมบัติและเมธอดต่อไปนี้ช่วยให้ทำงานกับหลายเหตุการณ์:
- CalendarReader.Count - Property Count ของคลาส CalendarReader ช่วยให้คุณดึงจำนวนคอมโพเนนต์ Vevent (เหตุการณ์) ที่อยู่ในปฏิทันทำให้ติดตามจำนวนเหตุการณ์ทั้งหมดได้ง่ายขึ้น.
- CalendarReader.IsMultiEvents - Property นี้กำหนดว่าปฏิทินมีหลายเหตุการณ์หรือไม่ ให้ค่า boolean แสดงว่ามีมากกว่าหนึ่งเหตุการณ์ ช่วยระบุว่าปฏิทินเป็นแบบมีเหตุการณ์เดียวหรือหลายเหตุการณ์.
- CalendarReader.Method - Property Method ได้ประเภทเมธอดของ iCalendar ที่เชื่อมโยงกับอ็อบเจกต์ปฏิทิน คืนค่าประเภทเมธอด เช่น “REQUEST”, “PUBLISH”, หรือ “CANCEL” ให้ข้อมูลเชิงลึกเกี่ยวกับวัตถุประสงค์ของปฏิทิน.
- CalendarReader.Version - รับค่า Version ของ iCalendar.
- CalendarReader.LoadAsMultiple() เมธอดนี้ช่วยให้โหลดรายการเหตุการณ์จากปฏิทินที่มีหลายเหตุการณ์ได้ ส่งคืนรายการของอ็อบเจกต์ Appointment ทำให้เข้าถึงและประมวลผลแต่ละเหตุการณ์ได้โดยง่าย.
ตัวอย่างโค้ดต่อไปนี้สาธิตวิธีที่คุณสามารถนำความสามารถเหล่านี้ไปใช้งานในโปรเจกต์ของคุณ:
var reader = new CalendarReader(fileName);
Console.WriteLine("Calendar contains " + reader.Count + " events");
Console.WriteLine("The Version of the calendar is " + reader.Version);
Console.WriteLine("The Method of the calendar is " + reader.Method);
Console.WriteLine("Does the calendar contain multiple events? - " + reader.IsMultiEvents);
List<Appointment> appointments = reader.LoadAsMultiple();
เพิ่มการเตือนแบบแสดงผล
โค้ด snippet ต่อไปนี้แสดงวิธีเพิ่มการเตือนแบบแสดงผลลงในปฏิทิน.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
// Create Appointment
Appointment app = new Appointment("Home", DateTime.Now.AddHours(1), DateTime.Now.AddHours(1), "organizer@domain.com", "attendee@gmail.com");
MailMessage msg = new MailMessage();
msg.AddAlternateView(app.RequestApointment());
MapiMessage mapi = MapiMessage.FromMailMessage(msg);
MapiCalendar calendar = (MapiCalendar)mapi.ToMapiMessageItem();
// Set calendar Properties
calendar.ReminderSet= true;
calendar.ReminderDelta = 45;//45 min before start of event
string savedFile = (dataDir + "calendarWithDisplayReminder.ics");
calendar.Save(savedFile, AppointmentSaveFormat.Ics);
เพิ่มการเตือนด้วยเสียง
โค้ดตัวอย่างต่อไปนี้แสดงวิธีเพิ่มการเตือนด้วยเสียงในปฏิทิน.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
Appointment app = new Appointment("Home", DateTime.Now.AddHours(1), DateTime.Now.AddHours(1), "organizer@domain.com", "attendee@gmail.com");
MailMessage msg = new MailMessage();
msg.AddAlternateView(app.RequestApointment());
MapiMessage mapi = MapiMessage.FromMailMessage(msg);
MapiCalendar calendar = (MapiCalendar)mapi.ToMapiMessageItem();
// Set calendar properties
calendar.ReminderSet = true;
calendar.ReminderDelta = 58;//58 min before start of event
calendar.ReminderFileParameter = dataDir + "Alarm01.wav";
string savedFile = (dataDir + "calendarWithAudioReminder_out.ics");
calendar.Save(savedFile, AppointmentSaveFormat.Ics);
เพิ่ม/ดึงไฟล์แนบจากไฟล์ปฏิทิน
โค้ดตัวอย่างต่อไปนี้แสดงวิธีเพิ่ม/ดึงไฟล์แนบจากไฟล์ปฏิทิน.
string[] files = new string[3];
files[0] = dataDir + "attachment_1.doc";
files[1] = dataDir + "download.png";
files[2] = dataDir + "Desert.jpg";
Appointment app1 = new Appointment("Home", DateTime.Now.AddHours(1), DateTime.Now.AddHours(1), "organizer@domain.com", "attendee@gmail.com");
foreach (string file in files)
{
using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(file)))
{
app1.Attachments.Add(new Attachment(ms, Path.GetFileName(file)));
}
}
app1.Save(dataDir + "appWithAttachments_out.ics", AppointmentSaveFormat.Ics);
Appointment app2 = Appointment.Load(dataDir + "appWithAttachments_out.ics");
Console.WriteLine(app2.Attachments.Count);
foreach (Attachment att in app2.Attachments)
Console.WriteLine(att.Name);
ตรวจสอบสถานะของผู้รับในคำขอการประชุม
โค้ด snippet ต่อไปนี้แสดงวิธีแสดงสถานะของผู้รับจากคำขอการประชุม.
MapiMessage message = MapiMessage.FromFile(fileName);
foreach (MapiRecipient recipient in message.Recipients)
{
Console.WriteLine(recipient.RecipientTrackStatus);
}
สร้าง MAPI Calendar TimeZone จาก Standard Timezone
โค้ด snippet ต่อไปนี้แสดงวิธีสร้าง MapiCalendarTimeZone จาก Standard Timezone.
MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone(TimeZoneInfo.Local);
ตั้งการเตือนความจำสำหรับการนัดหมาย
สามารถเพิ่มการเตือนเมื่อสร้างการนัดหมายได้ การเตือนเหล่านี้สามารถทำงานตามเกณฑ์ต่าง ๆ เช่น ก่อนเริ่มกำหนดเวลา n นาที, ทำซ้ำ n ครั้งที่ช่วงเวลา n. สามารถใช้แท็กต่าง ๆ เพื่อสร้างการกระตุ้นเหล่านี้ในสคริปต์ที่อยู่ระหว่าง BEGIN:VALARM และ END:VALARM ภายในการนัดหมาย. มีรูปแบบหลายแบบที่สามารถตั้งการเตือนบนการนัดหมายได้.
เพิ่มแท็กเพื่อตั้งการเตือนความจำ
โค้ดตัวอย่างต่อไปนี้แสดงวิธีตั้งการเตือนโดยเพิ่มแท็ก.
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Outlook();
string location = "Meeting Location: Room 5";
DateTime startDate = new DateTime(1997, 3, 18, 18, 30, 00),
endDate = new DateTime(1997, 3, 18, 19, 30, 00);
MailAddress organizer = new MailAddress("aaa@amail.com", "Organizer");
MailAddressCollection attendees = new MailAddressCollection();
attendees.Add(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.Trigger = new ReminderTrigger(new DateTime(1997, 3, 17, 13, 30, 0, DateTimeKind.Utc));
audioReminder.Repeat = 4;
audioReminder.Duration = new ReminderDuration(new TimeSpan(0, 15, 0));
audioReminder.Action = ReminderAction.Audio;
ReminderAttachment attach = new ReminderAttachment(new Uri("ftp://Host.com/pub/sounds/bell-01.aud"));
audioReminder.Attachments.Add(attach);
target.Reminders.Add(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(new TimeSpan(0, -30, 0));
displayReminder.Trigger = new ReminderTrigger(dur, ReminderRelated.Start);
displayReminder.Repeat = 2;
displayReminder.Duration = new ReminderDuration(new TimeSpan(0, 15, 0));
displayReminder.Action = ReminderAction.Display;
displayReminder.Description = "Breakfast meeting with executive team at 8:30 AM EST";
target.Reminders.Add(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(new TimeSpan(-2, 0, 0, 0));
emailReminder.Trigger = new ReminderTrigger(dur1, ReminderRelated.Start);
ReminderAttendee attendee = new ReminderAttendee("john_doe@host.com");
emailReminder.Attendees.Add(attendee);
emailReminder.Action = ReminderAction.Email;
emailReminder.Summary = "REMINDER: SEND AGENDA FOR WEEKLY STAFF MEETING";
emailReminder.Description = @"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.Attachments.Add(attach1);
target.Reminders.Add(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.Trigger = new ReminderTrigger(new DateTime(1998, 1, 1, 5, 0, 0, DateTimeKind.Utc));
procReminder.Repeat = 23;
procReminder.Duration = new ReminderDuration(new TimeSpan(1, 0, 0));
procReminder.Action = ReminderAction.Procedure;
ReminderAttachment attach2 = new ReminderAttachment(new Uri("ftp://Host.com/novo-procs/felizano.exe"));
procReminder.Attachments.Add(attach2);
target.Reminders.Add(procReminder);
target.Save(dataDir + "savedFile_out.ics");
แปลง Appointment EML เป็น MSG ด้วยเนื้อหา HTML
ตั้งแต่เวอร์ชัน 19.3, Aspose.Email มีความสามารถในการแปลง Appointment EML เป็น MSG พร้อมคงเนื้อหา HTML ของการนัดหมายไว้. Aspose.Email มี MapiConversionOptions.ForcedRtfBodyForAppointment คุณสมบัติที่มีค่าเริ่มต้นเป็น true. เมื่อค่าของ MapiConversionOptions.ForcedRtfBodyForAppointment หากตั้งค่าเป็น true, เนื้อหาการนัดหมายจะถูกแปลงเป็นรูปแบบ RTF. เพื่อคงรูปแบบเนื้อหาการนัดหมายในรูปแบบ HTML, ตั้งค่าของ MapiConversionOptions.ForcedRtfBodyForAppointment เป็น false.
ตัวอย่างต่อไปนี้แสดงการใช้งานของ MapiConversionOptions.ForcedRtfBodyForAppointment คุณสมบัติสำหรับคงรูปแบบเนื้อหาการนัดหมายในรูปแบบ HTML.
// Outlook directory
string dataDir = RunExamples.GetDataDir_Outlook();
MailMessage mailMessage = MailMessage.Load(dataDir + "TestAppointment.eml");
MapiConversionOptions conversionOptions = new MapiConversionOptions();
conversionOptions.Format = OutlookMessageFormat.Unicode;
// default value for ForcedRtfBodyForAppointment is true
conversionOptions.ForcedRtfBodyForAppointment = false;
MapiMessage mapiMessage = MapiMessage.FromMailMessage(mailMessage, conversionOptions);
Console.WriteLine("Body Type: " + mapiMessage.BodyType);
mapiMessage.Save(dataDir + "TestAppointment_out.msg");
ตั้งค่าสถานะของรายการ MAPI Calendar ด้วยตนเอง
ตั้งค่าสถานะของอ็อบเจกต์ MAPI Calendar อย่างชัดเจน โดยละเลยพฤติกรรมเริ่มต้น ซึ่งทำให้ควบคุมสถานะเหตุการณ์ปฏิทินได้ดีขึ้น โดยเฉพาะเมื่อจัดการคำขอการประชุมที่ได้รับ โดยค่าเริ่มต้นเมื่อสร้างการประชุม สถานะจะเป็น MapiCalendarState.Meeting. เมื่อได้รับในกล่องจดหมายของผู้รับ, มันจะเปลี่ยนเป็นโดยอัตโนมัติเป็น MapiCalendarState.Received, และคลาสข้อความของมันจะอัปเดตเป็น “IPM.Schedule.Meeting.Request”. การใช้ SetStateForced อนุญาตให้ตั้งค่าสถานะเป็น Received ด้วยตนเอง ซึ่งอาจเป็นประโยชน์สำหรับการเก็บข้อมูลผู้จัดเมื่อบันทึกปฏิทินเป็นไฟล์ MSG อย่างไรก็ตามอาจทำให้ไม่สามารถส่งต่อหรือส่งซ้ำการประชุมได้อย่างเหมาะสม.
ตัวอย่างโค้ดด้านล่างนี้แสดงวิธีสร้าง MapiCalendar อ็อบเจกต์, กำหนดผู้จัดและตั้งค่าสถานะของมันอย่างชัดเจนเป็นทั้งสอง Meeting และ Received โดยใช้ SetStateForced. จากนั้นบันทึกรายการปฏิทินเป็นไฟล์ .msg
MapiCalendar appointment = new MapiCalendar(
"LAKE ARGYLE WA 6743",
"Appointment",
"This is a very important meeting :)",
new DateTime(2024, 5, 10, 12, 30, 0, DateTimeKind.Utc),
new DateTime(2024, 5, 10, 13, 30, 0, DateTimeKind.Utc));
appointment.Organizer = new MapiElectronicAddress
{
EmailAddress = "test@aaa.com",
DisplayName = "test display Name"
};
appointment.SetStateForced(MapiCalendarState.Meeting | MapiCalendarState.Received);
appointment.Save("appointment.msg", AppointmentSaveFormat.Msg);
ดูเพิ่มเติม
- จัดการนัดหมาย — ทำงานกับการนัดหมาย iCalendar (ICS) และคำขอประชุม.
- การจัดการรายการปฏิทินในไฟล์ PST — จัดเก็บและอ่านรายการปฏิทินภายในไฟล์ PST.