Zarządzanie elementami kalendarza Outlook
Ten artykuł opisuje Outlook MapiCalendar elementy kalendarza zapisane jako MSG. Aby pracować ze standardowymi spotkaniami iCalendar (ICS) oraz żądaniami spotkań, zobacz Zarządzanie spotkaniami; aby przechowywać i odczytywać elementy kalendarza wewnątrz pliku PST, zobacz Zarządzanie elementami kalendarza w plikach PST.
Aspose.Email MapiCalendar klasa dostarcza metody i atrybuty do ustawiania różnych właściwości elementu kalendarza. Ta sekcja zawiera przykłady kodu dla:
- Utwórz i zapisz elementy kalendarza
- Zapisz elementy kalendarza jako pliki MSG
- Zapisz identyfikatory produktów dla elementów MAPI Calendar do formatu ICS
- Pobierz łączną liczbę wydarzeń
- Dodaj przypomnienia wyświetlane
- Dodaj przypomnienia dźwiękowe
- Dodaj/Pobierz załączniki z plików kalendarza
- Sprawdź status odbiorców w zaproszeniach na spotkania
- Utwórz strefę czasową MAPI Calendar z standardowej strefy czasowej
- Ustaw przypomnienia dla spotkań
- Konwertuj spotkanie EML do MSG z treścią HTML
- Ustaw stan elementów kalendarza MAPI ręcznie
Utwórz i zapisz elementy kalendarza
Poniższy fragment kodu pokazuje, jak utworzyć i zapisać element kalendarza w formacie 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);
Zapisz elementy kalendarza jako pliki MSG
Poniższy fragment kodu pokazuje, jak zapisać element kalendarza jako MSG.
calendar.Save(dataDir + "CalendarItemAsMSG_out.Msg", AppointmentSaveFormat.Msg);
Zapisz identyfikatory produktów dla elementów MAPI Calendar do formatu ICS
Ten ProductIdentifier właściwość MapiCalendarIcsSaveOptions klasa jest używana do zapisania elementu kalendarza MAPI do pliku iCalendar (ICS), zachowując oryginalne informacje o dacie i czasie oraz niestandardowy identyfikator produktu. Właściwość określa identyfikator produktu, który utworzył obiekt iCalendar.
Poniższy przykład kodu pokazuje, jak pracować z danymi iCalendar (ICS) w obiekcie kalendarza MAPI:
var icsSaveOptions = new MapiCalendarIcsSaveOptions
{
KeepOriginalDateTimeStamp = true,
ProductIdentifier = "Foo Ltd"
};
mapiCalendar.Save("my.ics", icsSaveOptions);
Pobierz łączną liczbę wydarzeń
Ten CalendarReader klasa umożliwia łatwą obsługę zdarzeń kalendarza. Poniższe właściwości i metoda pozwalają pracować z wieloma zdarzeniami:
- CalendarReader.Count - Właściwość Count klasy CalendarReader pozwala pobrać liczbę komponentów Vevent (wydarzeń) obecnych w kalendarzu, ułatwiając śledzenie łącznej liczby wydarzeń.
- CalendarReader.IsMultiEvents - Ta właściwość określa, czy kalendarz zawiera wiele wydarzeń. Zwraca wartość boolowską wskazującą, czy kalendarz zawiera więcej niż jedno wydarzenie, pomagając w identyfikacji kalendarzy jednowydarzeniowych lub wielowydarzeniowych.
- CalendarReader.Method - Właściwość Method uzyskuje typ metody iCalendar powiązany z obiektem kalendarza. Zwraca typ metody, taki jak „REQUEST”, „PUBLISH” lub „CANCEL”, dostarczając cennych informacji o przeznaczeniu kalendarza.
- CalendarReader.Version - Pobiera wersję iCalendar.
- CalendarReader.LoadAsMultiple() Ta metoda umożliwia ładowanie listy wydarzeń z kalendarza zawierającego wiele wydarzeń. Zwraca listę obiektów Appointment, umożliwiając łatwy dostęp i przetwarzanie każdego wydarzenia osobno.
Poniższy przykład kodu demonstruje, jak można wdrożyć te możliwości w projekcie:
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();
Dodaj wyświetlane przypomnienia
Poniższy fragment kodu pokazuje, jak dodać wyświetlane przypomnienie do kalendarza.
// 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);
Dodaj przypomnienia dźwiękowe
Poniższy fragment kodu pokazuje, jak dodać przypomnienie audio do kalendarza.
// 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);
Dodaj/Pobierz załączniki z plików kalendarza
Poniższy fragment kodu pokazuje, jak dodać/pobrać załączniki z plików kalendarza.
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);
Sprawdź status odbiorców w zaproszeniach na spotkania
Poniższy fragment kodu pokazuje, jak wyświetlić status odbiorców z zaproszenia na spotkanie.
MapiMessage message = MapiMessage.FromFile(fileName);
foreach (MapiRecipient recipient in message.Recipients)
{
Console.WriteLine(recipient.RecipientTrackStatus);
}
Utwórz strefę czasową MAPI Calendar z standardowej strefy czasowej
Poniższy fragment kodu pokazuje, jak utworzyć MapiCalendarTimeZone z standardowej strefy czasowej.
MapiCalendarTimeZone timeZone = new MapiCalendarTimeZone(TimeZoneInfo.Local);
Ustaw przypomnienia dla spotkań
Przypomnienie można dodać podczas tworzenia spotkania. Te alarmy mogą być wyzwalane na podstawie różnych kryteriów, takich jak n minut przed rozpoczęciem planu, powtarzanie n razy w odstępach n. Różne tagi mogą być użyte do stworzenia tych wyzwalaczy w skrypcie umieszczonym pomiędzy BEGIN:VALARM i END:VALARM w obrębie spotkania. Istnieje wiele wariantów ustawiania przypomnienia w spotkaniu.
Dodaj tagi, aby ustawić przypomnienia
Poniższy fragment kodu pokazuje, jak ustawić przypomnienie poprzez dodanie tagów.
// 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");
Konwertuj spotkanie EML do MSG z treścią HTML
Od wersji 19.3, Aspose.Email umożliwia konwersję spotkania w formacie EML do MSG zachowując treść HTML spotkania. Aspose.Email zapewnia MapiConversionOptions.ForcedRtfBodyForAppointment właściwości, która ma domyślną wartość true. Gdy wartość MapiConversionOptions.ForcedRtfBodyForAppointment jest ustawiona na true, treść spotkania jest konwertowana do formatu RTF. Aby zachować format treści spotkania w HTML, ustaw wartość MapiConversionOptions.ForcedRtfBodyForAppointment na false.
Poniższy przykład demonstruje użycie MapiConversionOptions.ForcedRtfBodyForAppointment właściwość, aby zachować format treści spotkania w formacie 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");
Ustaw stan elementów MAPI Calendar ręcznie
Ustaw stan obiektu MAPI Calendar jednoznacznie, nadpisując domyślne zachowanie. Pozwala to na lepszą kontrolę nad stanami wydarzeń kalendarza, szczególnie przy obsłudze otrzymanych zaproszeń na spotkania. Domyślnie, gdy spotkanie jest tworzone, jego stan jest MapiCalendarState.Meeting. Po otrzymaniu w skrzynce odbiorczej odbiorcy, automatycznie zmienia się na MapiCalendarState.Received, a jego klasa wiadomości jest zaktualizowana do „IPM.Schedule.Meeting.Request”. Używając SetStateForced pozwala ręcznie ustawić stan na Received, co może być przydatne przy zachowywaniu informacji o organizatorze podczas zapisywania kalendarza jako plik MSG. Jednak może to uniemożliwić prawidłowe przekazywanie lub ponowne wysyłanie spotkania.
Poniższy przykład kodu demonstruje, jak utworzyć MapiCalendar obiekt, przypisz organizatora i jednoznacznie ustaw jego stan na oba Meeting i Received przy użyciu SetStateForced. Następnie zapisuje element kalendarza jako plik .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);
Zobacz także
- Zarządzanie spotkaniami — pracować z spotkaniami iCalendar (ICS) oraz żądaniami spotkań.
- Zarządzanie elementami kalendarza w plikach PST — przechowywać i odczytywać elementy kalendarza wewnątrz pliku PST.