MailBee. NET Objects Tutorials

Create a meeting request item

MailBee.NET can be used to create or read vCalendar entries. vCalendar entries are used to exchange the information about appointments, meeting requests, reminders, events, etc. vCalendar format is used by many e-mail programs including Outlook.

A vCalendar record is just a text document. vCalendar records are stored in a MIME message as a single body part having "text/calendar" content type. The content of a vCalendar record can vary but the typical vCalendar record looks like below:

BEGIN:VCALENDAR
BEGIN:VEVENT
DTSTART:20081212T122059Z
DTEND:20081212T132059Z
LOCATION:NY Office
SUMMARY:Meeting to discuss the new project
END:VEVENT
END:VCALENDAR

The DTSTART and DTEND entries are combinations of date and time in YYYYMMDDThhmmssZ format where YYYY=year, MM=month, DD=day of the month, T delimits time from date, hh=hour (24-hour clock), mm=minutes, ss=seconds, Z is the end delimiter. This value represents the adjusted to Greenwich Mean Time (GMT, or UTC).

Upon receipt of the e-mail containing the vCalendar record above, your organizer software will create an appointment at 12:20:59 on December 12, 2008, the appointment will span one hour.

Let's define the vCalendar structure as follows:

C#

public struct VCalendar 
{
  public int year, month, day, hour, minute, second, duration;
  public string summary, location, method;
}

VB.NET

Public Structure VCalendar
  Public year As Integer,month As Integer,day As Integer,hour As Integer,minute As Integer,second As Integer,duration As Integer
  Public summary As String,location As String,method As String
End Structure

Thus, to create and send a vCalendar message with certain event information, fill in the VCalendar structure first:

C#

// Create calendar event
VCalendar calEvent = new VCalendar();
calEvent.summary = "Meeting to discuss the new project";
calEvent.location = "NY Office";
calEvent.year = 2008;
calEvent.month = 12;
calEvent.day = 12;
calEvent.hour = 12;
calEvent.minute = 20;
calEvent.second = 59;
calEvent.duration = 1; // in hours
calEvent.method = "REQUEST";

VB.NET

' Create calendar event
Dim calEvent As VCalendar =  New VCalendar() 
calEvent.summary = "Meeting to discuss the new project"
calEvent.location = "NY Office"
calEvent.year = 2008
calEvent.month = 12
calEvent.day = 12
calEvent.hour = 12
calEvent.minute = 20
calEvent.second = 59
calEvent.duration = 1 ' in hours
calEvent.method = "REQUEST"

Moreover, you can add extra items to describe the event in more detail. Obviously, they must be supported by your organizer program (at least conform to The Electronic Calendaring and Scheduling Exchange format - http://www.imc.org/pdi/vcal-10.txt).

Now, let's generate vCalendar body:

C#

string CreateCalendarRecord(VCalendar calEvent)
{
        string calRecord;
        calRecord  = "BEGIN:VCalendar" + "\r\n";
        calRecord += "METHOD:" + calEvent.method + "\r\n";
        calRecord += "BEGIN:VEVENT" + "\r\n";
        calRecord += "DTSTART:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00");
        calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z\r\n";
        calEvent.hour += calEvent.duration;
        calRecord += "DTEND:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00");
        calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z\r\n";
        calRecord += "LOCATION:" + calEvent.location + "\r\n";
        calRecord += "SUMMARY:" + calEvent.summary+ "\r\n";
        calRecord += "END:VEVENT" + "\r\n";
        calRecord += "END:VCalendar";
        return calRecord;
}

...
// Create calendar body
mailer.Message.BodyParts.Add("text/calendar").Text = CreateCalendarRecord(calEvent);

VB.NET

Private Function CreateCalendarRecord(ByVal calEvent As VCalendar) As String
        Dim calRecord As String
        calRecord  = "BEGIN:VCalendar" + "\r\n"
        calRecord += "METHOD:" + calEvent.method + "\r\n"
        calRecord += "BEGIN:VEVENT" + "\r\n"
        calRecord += "DTSTART:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00")
        calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z\r\n"
        calEvent.hour += calEvent.duration
        calRecord += "DTEND:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00")
        calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z\r\n"
        calRecord += "LOCATION:" + calEvent.location + "\r\n"
        calRecord += "SUMMARY:" + calEvent.summary+ "\r\n"
        calRecord += "END:VEVENT" + "\r\n"
        calRecord += "END:VCalendar"
        Return calRecord
End Function
 
...
' Create calendar body
mailer.Message.BodyParts.Add("text/calendar").Text = CreateCalendarRecord(calEvent)

After creating the vCalendar event, you can send the message (or you can set other parts such as set plain-text version of the event for those e-mail readers which do not support vCalendar format).

Code example:

C#

public struct VCalendar
{
	public int year, month, day, hour, minute, second, duration;
	public string summary, location, method;
}

static string CreateCalendarRecord(VCalendar calEvent)
{
	string calRecord;
	calRecord = "BEGIN:VCalendar" + Environment.NewLine;
	calRecord += "METHOD:" + calEvent.method + Environment.NewLine;
	calRecord += "BEGIN:VEVENT" + Environment.NewLine;
	calRecord += "DTSTART:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00");

	calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z" + Environment.NewLine;

	calEvent.hour += calEvent.duration;
	calRecord += "DTEND:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00");

	calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z" + Environment.NewLine;

	calRecord += "LOCATION:" + calEvent.location + Environment.NewLine;
	calRecord += "SUMMARY:" + calEvent.summary + Environment.NewLine;
	
	// Calculate unique ID based on current DateTime and its MD5 hash
	string strHash = string.Empty;
	foreach (byte b in (new System.Security.Cryptography.MD5CryptoServiceProvider()).ComputeHash(System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString())))
	{
		strHash += b.ToString("X2");
	}
	calRecord += "UID:" + strHash + Environment.NewLine;
	calRecord += "END:VEVENT" + Environment.NewLine;
	calRecord += "END:VCalendar";
	return calRecord;
}

// Create calendar event
VCalendar calEvent = new VCalendar();
calEvent.summary = "Meeting to discuss the new project";
calEvent.location = "NY Office";
calEvent.year = 2008;
calEvent.month = 12;
calEvent.day = 12;
calEvent.hour = 12;
calEvent.minute = 20;
calEvent.second = 59;
calEvent.duration = 1;
calEvent.method = "REQUEST";

Smtp.LicenseKey = "trial or permanent license key";
Smtp mailer = new Smtp();

mailer.Message.From.Email = "jdoe@domain.com";
mailer.Message.To.Add("bill@domain.com");
mailer.Message.Subject = "Meeting Request";

// Create calendar body
mailer.Message.BodyParts.Add("text/calendar; method=\"REQUEST\"").Text = CreateCalendarRecord(calEvent);

// Connect to SMTP server and send meeting request
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret");
mailer.Send();

VB.NET

Public Structure VCalendar
	Public year As Integer, month As Integer, day As Integer, hour As Integer, minute As Integer, second As Integer, duration As Integer
	Public summary As String, location As String, method As String
End Structure

Public Function CreateCalendarRecord(ByVal calEvent As VCalendar) As String
	Dim calRecord As String
	calRecord = "BEGIN:VCalendar" + Environment.NewLine
	calRecord += "METHOD:" + calEvent.method + Environment.NewLine
	calRecord += "BEGIN:VEVENT" + Environment.NewLine
	calRecord += "DTSTART:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00")

	calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z" + Environment.NewLine

	calEvent.hour += calEvent.duration
	calRecord += "DTEND:" + calEvent.year.ToString("0000") + calEvent.month.ToString("00") + calEvent.day.ToString("00")

	calRecord += "T" + calEvent.hour.ToString("00") + calEvent.minute.ToString("00") + calEvent.second.ToString("00") + "Z" + Environment.NewLine

	calRecord += "LOCATION:" + calEvent.location + Environment.NewLine
	calRecord += "SUMMARY:" + calEvent.summary + Environment.NewLine

	' Calculate unique ID based on current DateTime and its MD5 hash
	Dim strHash As String
	strHash = ""
	For Each b As Byte In (New System.Security.Cryptography.MD5CryptoServiceProvider()).ComputeHash(System.Text.Encoding.Default.GetBytes(DateTime.Now.ToString()))
		strHash += b.ToString("X2")
	Next

	calRecord += "UID:" + strHash + Environment.NewLine
	calRecord += "END:VEVENT" + Environment.NewLine
	calRecord += "END:VCalendar"
	Return calRecord
End Function

Dim calEvent As VCalendar = New VCalendar()
calEvent.summary = "Meeting to discuss the new project"
calEvent.location = "NY Office"
calEvent.year = 2008
calEvent.month = 12
calEvent.day = 12
calEvent.hour = 12
calEvent.minute = 20
calEvent.second = 59
calEvent.duration = 1
calEvent.method = "REQUEST"

Smtp.LicenseKey = "trial or permanent license key"
Dim mailer As Smtp = New Smtp()

mailer.Message.From.Email = "jdoe@domain.com"
mailer.Message.To.Add("bill@domain.com")
mailer.Message.Subject = "Meeting Request"

' Create calendar body
mailer.Message.BodyParts.Add("text/calendar; method=""REQUEST""").Text = CreateCalendarRecord(calEvent)

' Connect to SMTP server and send meeting request
mailer.SmtpServers.Add("mail.domain.com", "jdoe", "secret")
mailer.Send()

You can use similar approach to create calendar entries of other formats (such as iCalendar).