Posts Tagged ‘iCalendar’

Send iCalendar meeting requests for different timezone

Sunday, January 1st, 2012

Usually you define time of an event in relation to UTC time zone.

If you need to define event on 9:00 o’clock in Alaska you simply need
to subtract 9 hours from event time to get the event time in UTC
(18:00:00). 18 – 9 = 9.

It all works great in December (Alaska is UTC-9), but in May, daylight
saving time is in effect in Alaska (Alaska is UTC-8 then).

If the event is recurring, in May, event is going to be held on 10:00
Alaska time (18 – 8 = 10). Which is most likely not what you want.

As the time zones in different parts of world change way to often to reflect these changes in Mail.dll,
you’ll need to specify the time zone when creating new event (including the daylight savings time).

Here’s the sample:

// C#
using Fluent = Lesnikowski.Mail.Fluent;
using Lesnikowski.Client;
using Lesnikowski.Mail;
using Lesnikowski.Mail.Appointments;

Appointment appointment = new Appointment();

// Create time zone
VTimeZone alaska = appointment.AddTimeZone();
alaska.TimeZoneId = "America/Anchorage";

// Define standard time offset
var standardRecurring = new RecurringRule();
standardRecurring.Frequency = Frequency.Yearly;
standardRecurring.ByDay.Add(Weekday.Sunday);
standardRecurring.ByMonths.Add(11);

var standard = new StandardOffset
{
    Name = "AKST",
    Start = new DateTime(1970, 11, 01, 02, 00, 00),
    OffsetFrom = TimeSpan.FromHours(-8),
    OffsetTo = TimeSpan.FromHours(-9),
    RecurringRule = standardRecurring
};

// Define daylight time offset
var daylightRecurring = new RecurringRule();
daylightRecurring.Frequency = Frequency.Yearly;
daylightRecurring.ByDay.Add(new Weekday(2, Weekday.Sunday));
daylightRecurring.ByMonths.Add(3);

var daylight = new DaylightOffset
{
    Name = "AKDT",
    Start = new DateTime(1970, 03, 08, 02, 00, 00),
    OffsetFrom = TimeSpan.FromHours(-9),
    OffsetTo = TimeSpan.FromHours(-8),
    RecurringRule = daylightRecurring
};

alaska.Standard.Add(standard);
alaska.Daylight.Add(daylight);

// Define event
Event e = appointment.AddEvent();
e.Start = new DateTime(2007, 08, 17, 12, 00, 00);
e.End = new DateTime(2007, 08, 17, 12, 30, 00);
e.Summary = "At noon in Alaska";
e.InTimeZone(alaska);

e.SetOrganizer(new Person("Alice", "alice@example.org"));

e.AddParticipant(new Participant(
    "Bob", "bob@example.org", ParticipationRole.Required, true));
e.AddParticipant(new Participant(
    "Tom", "tom@example.org", ParticipationRole.Optional, true));
e.AddParticipant(new Participant(
    "Alice", "alice@example.org", ParticipationRole.Required, true));

Alarm alarm = e.AddAlarm();
alarm.BeforeStart(TimeSpan.FromMinutes(15));

IMail email = Fluent.Mail
    .Text("Status meeting at noon in Alaska.")
    .Subject("Status meeting")
    .From("alice@example.org")
    .To("bob@example.org")
    .To("tom@example.org")
    .AddAppointment(appointment)
    .Create();

using (Smtp smtp = new Smtp())
{
    smtp.Connect("smtp.example.org"); // or ConnectSSL
    smtp.Login("user", "password");
    smtp.SendMessage(email);
    smtp.Close();
}
' VB.NET
Imports Fluent = Lesnikowski.Mail.Fluent
Imports Lesnikowski.Client
Imports Lesnikowski.Mail
Imports Lesnikowski.Mail.Appointments

Dim appointment As New Appointment()

' Create time zone
Dim alaska As VTimeZone = appointment.AddTimeZone()
alaska.TimeZoneId = "America/Anchorage"

' Define standard time offset
Dim standardRecurring = New RecurringRule()
standardRecurring.Frequency = Frequency.Yearly
standardRecurring.ByDay.Add(Weekday.Sunday)
standardRecurring.ByMonths.Add(11)

Dim standard = New StandardOffset() With { _
	.Name = "AKST", _
	.Start = New DateTime(1970, 11, 1, 2, 0, 0), _
	.OffsetFrom = TimeSpan.FromHours(-8), _
	.OffsetTo = TimeSpan.FromHours(-9), _
	.RecurringRule = standardRecurring _
}

' Define daylight time offset
Dim daylightRecurring = New RecurringRule()
daylightRecurring.Frequency = Frequency.Yearly
daylightRecurring.ByDay.Add(New Weekday(2, Weekday.Sunday))
daylightRecurring.ByMonths.Add(3)

Dim daylight = New DaylightOffset() With { _
	.Name = "AKDT", _
	.Start = New DateTime(1970, 3, 8, 2, 0, 0), _
	.OffsetFrom = TimeSpan.FromHours(-9), _
	.OffsetTo = TimeSpan.FromHours(-8), _
	.RecurringRule = daylightRecurring _
}

alaska.Standard.Add(standard)
alaska.Daylight.Add(daylight)

' Define event
Dim e As [Event] = appointment.AddEvent()
e.Start = New DateTime(2007, 8, 17, 12, 0, 0)
e.[End] = New DateTime(2007, 8, 17, 12, 30, 0)
e.Summary = "At noon in Alaska"
e.InTimeZone(alaska)

e.SetOrganizer(New Person("Alice", "alice@example.org"))

e.AddParticipant(New Participant( _
	"Bob", "bob@example.org", ParticipationRole.Required, True))
e.AddParticipant(New Participant( _
	"Tom", "tom@example.org", ParticipationRole.[Optional], True))
e.AddParticipant(New Participant( _
	"Alice", "alice@example.org", ParticipationRole.Required, True))

Dim alarm As Alarm = e.AddAlarm()
alarm.BeforeStart(TimeSpan.FromMinutes(15))

Dim email As IMail = Fluent.Mail _
	.Text("Status meeting at noon in Alaska.") _
	.Subject("Status meeting") _
	.From("alice@example.org") _
	.[To]("bob@example.org") _
	.[To]("tom@example.org") _
	.AddAppointment(appointment) _
	.Create()

Using smtp As New Smtp()
	smtp.Connect("smtp.example.org") 	' or ConnectSSL
	smtp.Login("user", "password")
	smtp.SendMessage(email)
	smtp.Close()
End Using

Send iCalendar recurring meeting requests

Friday, November 19th, 2010

First add all necessary namespaces:

// C# version

using Lesnikowski.Client;
using Lesnikowski.Mail;
using Lesnikowski.Mail.Appointments;
using Lesnikowski.Mail.Fluent;
using Lesnikowski.Mail.Headers;
' VB.NET version

Imports Lesnikowski.Client
Imports Lesnikowski.Mail
Imports Lesnikowski.Mail.Appointments
Imports Lesnikowski.Mail.Fluent
Imports Lesnikowski.Mail.Headers

Now, we’ll create an appointment, specify it’s recurring options and send it:

// C# version

Appointment appointment = new Appointment();
Event newEvent = appointment.AddEvent();
newEvent.SetOrganizer(new Person("Alice", "alice@example.org"));
newEvent.AddParticipant(new Participant("Bob", "bob@gmail.com"));
newEvent.Description = "We need to talk about the daily report";
newEvent.Summary = "Daily report meeting";
newEvent.Start = new DateTime(2010, 11, 29, 16, 0, 0);
newEvent.End = new DateTime(2010, 11, 29, 17, 0, 0);
newEvent.Priority = 5;
newEvent.Class = EventClass.Public;

RecurringRule rule = newEvent.AddRecurringRule();
rule.Interval = 1;
rule.Until = new DateTime(2011, 12, 1); // -or- rule.Count = 10;

// Every week on  Mon, Tue, Wed, Thu Fri:
rule.Frequency = Frequency.Weekly;
rule.ByDay.AddRange(new[] {
    Weekday.Monday,
    Weekday.Tuesday,
    Weekday.Wednesday,
    Weekday.Thursday,
    Weekday.Friday });

Alarm alarm = newEvent.AddAlarm();
alarm.Description = "Reminder";
alarm.BeforeStart(TimeSpan.FromMinutes(5));

IMail email = Mail.Text("Daily report meeting at 4PM")
    .Subject("Daily report meeting")
    .From(new MailBox("alice@example.org", "Alice"))
    .To(new MailBox("bob@example.org", "Bob"))
    .AddAppointment(appointment)
    .Create();

using (Smtp client = new Smtp())
{
    client.ConnectSSL("smtp.example.org");
    client.UseBestLogin("user", "password");
    client.SendMessage(email);
    client.Close();
}
' VB.NET version

Dim appointment As New Appointment()
Dim newEvent As [Event] = appointment.AddEvent()
newEvent.SetOrganizer(New Person("Alice", "alice@example.org"))
newEvent.AddParticipant(New Participant("Bob", "bob@gmail.com"))
newEvent.Description = "We need to talk about the daily report"
newEvent.Summary = "Daily report meeting"
newEvent.Start = New DateTime(2010, 11, 29, 16, 0, 0)
newEvent.[End] = New DateTime(2010, 11, 29, 17, 0, 0)
newEvent.Priority = 5
newEvent.[Class] = EventClass.[Public]

Dim rule As RecurringRule = newEvent.AddRecurringRule()
rule.Interval = 1
rule.Until = New DateTime(2011, 12, 1)
' -or- rule.Count = 10;
' Every week on  Mon, Tue, Wed, Thu Fri:
rule.Frequency = Frequency.Weekly
rule.ByDay.AddRange(New Weekday() {Weekday.Monday _
                                   , Weekday.Tuesday _
                                   , Weekday.Wednesday _
                                   , Weekday.Thursday _
                                   , Weekday.Friday})

Dim alarm As Alarm = newEvent.AddAlarm()
alarm.Description = "Reminder"
alarm.BeforeStart(TimeSpan.FromMinutes(5))

Dim email As IMail = Mail.Text("Daily report meeting at 4PM") _
    .Subject("Daily report meeting") _
    .From(New MailBox("alice@example.org", "Alice")) _
    .[To](New MailBox("bob@example.org", "Bob")) _
    .AddAppointment(appointment).Create()

Using client As New Smtp()
    client.ConnectSSL("smtp.example.org")
    client.UseBestLogin("user", "password")
    client.SendMessage(email)
    client.Close()
End Using

This is how it looks like in Gmail:

Recurring appointment

Recurring appointment

Here are the few samples of how you can set the frequency:

Daily:

// C# version

rule.Frequency = Frequency.Daily;
' VB.NET version

rule.Frequency = Frequency.Daily

Every month on last monday:

// C# version

rule.Frequency = Frequency.Monthly;
rule.ByDay.Add(Weekday.LastMonday);
' VB.NET version

rule.Frequency = Frequency.Monthly
rule.ByDay.Add(Weekday.LastMonday)

Every month on second but last monday:

// C# version

rule.Frequency = Frequency.Monthly;
rule.ByDay.Add(new Weekday(-2, Weekday.Monday));
' VB.NET version

rule.Frequency = Frequency.Monthly
rule.ByDay.Add(New Weekday(-2, Weekday.Monday))

Send iCalendar meeting requests

Monday, May 10th, 2010

Mail.dll .NET email component includes support for popular iCalendar standard.

iCalendar is used by most popular email clients like Outlook or Gmail. It allows Internet users to send meeting requests and tasks to other users, via email, or sharing files with an extension of .ics.

Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter propose another meeting date/time.

First make sure that your ‘usings’ or ‘Imports’ section includes all needed namespaces:

// C#

using Lesnikowski.Mail;
using Lesnikowski.Mail.Fluent;
using Lesnikowski.Mail.Appointments;
' VB.NET

Imports Lesnikowski.Mail
Imports Lesnikowski.Mail.Fluent
Imports Lesnikowski.Mail.Appointments

And now the code.

First we’ll create an appointment object add new event to it:

Appointment appointment = new Appointment();
Event e = appointment.AddEvent();

Then we need to set start and end dates:

e.Description = "Status meeting description";
e.Summary = "Status meeting summary";
e.Start = new DateTime(2010, 05, 10, 16, 00, 00);
e.End = new DateTime(2010, 05, 10, 17, 00, 00);

Now we’ll add all required and optional participants to it:

e.SetOrganizer(new Person("Alice", "alice@mail.com"));

e.AddParticipant(new Participant(
	"Bob", "bob@mail.com", ParticipationRole.Required, true));
e.AddParticipant(new Participant(
	"Tom", "tom@mail.com", ParticipationRole.Optional, true));
e.AddParticipant(new Participant(
	"Alice", "alice@mail.com", ParticipationRole.Required, true));

We’ll add an alarm to the event (set 15 minutes before the event start):

Alarm alarm = e.AddAlarm();
alarm.BeforeStart(TimeSpan.FromMinutes(15));

Finally we’ll create an email add the appointment to it and send it:

Mail.Text("Status meeting at 4PM.")
	.Subject("Status meeting")
	.From("alice@mail.com")
    .To("bob@mail.com")
    .AddAppointment(appointment)
    .UsingNewSmtp()
    .WithCredentials("alice@mail.com", "password")
	.Server("smtp.mail.com")
	.WithSSL()
	.Send();

The entire C# code looks as follows:

Appointment appointment = new Appointment();

Event e = appointment.AddEvent();
e.Description = "Status meeting description";
e.Summary = "Status meeting summary";
e.Start = new DateTime(2010, 05, 10, 16, 00, 00);
e.End = new DateTime(2010, 05, 10, 17, 00, 00);

e.SetOrganizer(new Person("Alice", "alice@mail.com"));

e.AddParticipant(new Participant(
	"Bob", "bob@mail.com", ParticipationRole.Required, true));
e.AddParticipant(new Participant(
	"Tom", "tom@mail.com", ParticipationRole.Optional, true));
e.AddParticipant(new Participant(
	"Alice", "alice@mail.com", ParticipationRole.Required, true));

Alarm alarm = e.AddAlarm();
alarm.BeforeStart(TimeSpan.FromMinutes(15));

Mail.Text("Status meeting at 4PM.")
	.Subject("Status meeting")
	.From("alice@mail.com")
    .To("bob@mail.com")
    .AddAppointment(appointment)
    .UsingNewSmtp()
    .WithCredentials("alice@mail.com", "password")
	.Server("smtp.mail.com")
	.WithSSL()
	.Send();

…and the VB.NET version:

Dim appointment As New Appointment()

Dim e As [Event] = appointment.AddEvent()
e.Description = "Status meeting description"
e.Summary = "Status meeting summary"
e.Start = New DateTime(2010, 5, 10, 16, 0, 0)
e.[End] = New DateTime(2010, 5, 10, 17, 0, 0)

e.SetOrganizer(New Person("Alice", "alice@mail.com"))

e.AddParticipant(New Participant( _
	"Bob", "bob@mail.com", ParticipationRole.Required, True))
e.AddParticipant(New Participant( _
	"Tom", "tom@mail.com", ParticipationRole.[Optional], True))
e.AddParticipant(New Participant( _
	"Alice", "alice@mail.com", ParticipationRole.Required, True))

Dim alarm As Alarm = e.AddAlarm()
alarm.BeforeStart(TimeSpan.FromMinutes(15))

Mail.Text("Status meeting at 4PM.") _
	.Subject("Status meeting") _
	.From("alice@mail.com") _
	.[To]("bob@mail.com") _
	.AddAppointment(appointment) _
	.UsingNewSmtp() _
	.WithCredentials("alice@mail.com", "password") _
	.Server("smtp.mail.com") _
	.WithSSL() _
	.Send()

You can download Mail.dll .NET email component here.