Working with Appointments

Load and Save an Appointment in ICS Format

The Appointment class in Aspose.Email for .NET can be used to load an appointment in ICS format as well as create a new appointment and save it to a disk in ICS format. In this article, we first create an appointment and save it to a disk in ICS format, and then we load it.

Create an Appointment and Save to a Disk in ICS Format

Following steps are required to create an appointment and save it in ICS format.

  1. Create an instance of the Appointment class and initialize it with this constructor.
  2. Pass the following arguments in the above constructor
    1. Location
    2. Summary
    3. Description
    4. Start Date
    5. End Date
    6. Organizer
    7. Attendees
  3. Call the Save() method and specify the file name and format in the arguments.

The appointment can be opened in Microsoft Outlook or any program that can load an ICS file. If the file is opened in Microsoft Outlook it automatically adds the appointment in the Outlook calendar.

The following code snippet shows you how to create and save an appointment to a disk in ICS format.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create and initialize an instance of the Appointment class
Appointment appointment = new Appointment(
"Meeting Room 3 at Office Headquarters",// Location
"Monthly Meeting", // Summary
"Please confirm your availability.", // Description
new DateTime(2015, 2, 8, 13, 0, 0), // Start date
new DateTime(2015, 2, 8, 14, 0, 0), // End date
"from@domain.com", // Organizer
"attendees@domain.com"); // Attendees
// Save the appointment to disk in ICS format
appointment.Save(dstEmail, AppointmentSaveFormat.Ics);
Console.WriteLine("Appointment created and saved to disk successfully.");

Load Appointment ICS Format

To load an appointment in ICS format, the following steps are required:

  1. Create an instance of the Appointment class.
  2. Call the Load() method by providing the path of the ICS file.
  3. Read any property to get any information from the appointment (ICS file).

The following code snippet shows you how to load an appointment in ICS format.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Load an Appointment just created and saved to disk and display its details.
Appointment loadedAppointment = Appointment.Load(dstEmail);
Console.WriteLine(Environment.NewLine + "Loaded Appointment details are as follows:");
// Display the appointment information on screen
Console.WriteLine("Summary: " + loadedAppointment.Summary);
Console.WriteLine("Location: " + loadedAppointment.Location);
Console.WriteLine("Description: " + loadedAppointment.Description);
Console.WriteLine("Start date: " + loadedAppointment.StartDate);
Console.WriteLine("End date: " + loadedAppointment.EndDate);
Console.WriteLine("Organizer: " + appointment.Organizer);
Console.WriteLine("Attendees: " + appointment.Attendees);
Console.WriteLine(Environment.NewLine + "Appointment loaded successfully from " + dstEmail);

Read Multiple Events from ICS File

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string dataDir = RunExamples.GetDataDir_Email();
List<Appointment> appointments = new List<Appointment>();
CalendarReader reader = new CalendarReader(dataDir + "US-Holidays.ics");
while (reader.NextEvent())
{
appointments.Add(reader.Current);
}
//working with appointments...

Write Multiple Events to ICS File

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
IcsSaveOptions saveOptions = new IcsSaveOptions();
saveOptions.Action = AppointmentAction.Create;
using (CalendarWriter writer = new CalendarWriter(dataDir + "WriteMultipleEventsToICS_out.ics", saveOptions))
{
for (int i = 0; i < 10; i++)
{
Appointment app = new Appointment(string.Empty, DateTime.Now, DateTime.Now, "sender@domain.com", "receiver@domain.com");
app.Description = "Test body " + i;
app.Summary = "Test summary:" + i;
writer.Write(app);
}
}

Create a Draft Appointment Request

It was shown in our earlier articles how to create and save an appointment in ICS format. It is often required to create an Appointment request in a Draft mode, so as the basic information is added and then the same draft Appointment be forwarded to other users for necessary changes according to individual uses. In order to save an Appointment in a Draft mode, the MethodType property of Appointment class should be set to AppointmentMethodType.Publish. The following code snippet shows you how to create a draft appointment request.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
string dstDraft = dataDir + "appointment-draft_out.msg";
string sender = "test@gmail.com";
string recipient = "test@email.com";
MailMessage message = new MailMessage(sender, recipient, string.Empty, string.Empty);
Appointment app = new Appointment(string.Empty, DateTime.Now, DateTime.Now, sender, recipient)
{
MethodType = AppointmentMethodType.Publish
};
message.AddAlternateView(app.RequestApointment());
MapiMessage msg = MapiMessage.FromMailMessage(message);
// Save the appointment as draft.
msg.Save(dstDraft);
Console.WriteLine(Environment.NewLine + "Draft saved at " + dstDraft);

Draft Appointment Creation from Text

The following code snippet shows you how to create a draft appointment from Text. 

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
string ical = @"BEGIN:VCALENDAR
METHOD:PUBLISH
PRODID:-//Aspose Ltd//iCalender Builder (v3.0)//EN
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN=test@gmail.com:mailto:test@gmail.com
DTSTART:20130220T171439
DTEND:20130220T174439
DTSTAMP:20130220T161439Z
END:VEVENT
END:VCALENDAR";
string sender = "test@gmail.com";
string recipient = "test@email.com";
MailMessage message = new MailMessage(sender, recipient, string.Empty, string.Empty);
AlternateView av = AlternateView.CreateAlternateViewFromString(ical, new ContentType("text/calendar"));
message.AlternateViews.Add(av);
MapiMessage msg = MapiMessage.FromMailMessage(message);
msg.Save(dataDir + "draft_out.msg");

Set Participants Status of Appointment Attendees

Aspose.Email for .NET API lets you set the status of appointment attendees while formulating a reply message. This adds the PARTSTAT property to the ICS file.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
string location = "Room 5";
DateTime startDate = new DateTime(2011, 12, 10, 10, 12, 11),
endDate = new DateTime(2012, 11, 13, 13, 11, 12);
MailAddress organizer = new MailAddress("aaa@amail.com", "Organizer");
MailAddressCollection attendees = new MailAddressCollection();
MailAddress attendee1 = new MailAddress("bbb@bmail.com", "First attendee");
MailAddress attendee2 = new MailAddress("ccc@cmail.com", "Second attendee");
attendee1.ParticipationStatus = ParticipationStatus.Accepted;
attendee2.ParticipationStatus = ParticipationStatus.Declined;
attendees.Add(attendee1);
attendees.Add(attendee2);
Appointment target = new Appointment(location, startDate, endDate, organizer, attendees);

Customize Product Identifier for ICalendar

Aspose.Email for .NET API allows to get or set the product identifier that created iCalendar object.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// The path to the File directory.
string dataDir = RunExamples.GetDataDir_Email();
string description = "Test Description";
Appointment app = new Appointment("location", "test appointment", description, DateTime.Today,
DateTime.Today.AddDays(1), "first@test.com", "second@test.com");
IcsSaveOptions saveOptions = IcsSaveOptions.Default;
saveOptions.ProductId = "Test Corporation";
app.Save(dataDir + "ChangeProdIdOfICS.ics", saveOptions);