Add a Calendar to a Mail Message in C# and VB.NET

GemBox.Email allows you to add a calendar to a MailMessage, which can be later sent or saved to a file. It is possible to add several calendars to a single message.

Alternatively, it is also possible to get all calendars from a loaded or received message.

The following example shows how to create a calendar, add it to a message, and save it to a file.

Screenshot
Load Screenshot

See the full code below, use Run Example to execute.

using GemBox.Email;
using GemBox.Email.Calendar;
using System;
using System.Linq;
using System.Text;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new message
        MailMessage message = new MailMessage(new MailAddress("sender@example.com", "Sender"),
                              new MailAddress("first.receiver@example.com", "First receiver"));

        message.Subject = "This is a mail message with a calendar";
        message.BodyText = "Hi! I am sending you my calendar.";

        // Create new calendar.
        Calendar calendar = new Calendar();
        Event e = new Event();
        e.Organizer = message.Sender;
        e.Name = "GemBox conference";
        e.Start = new DateTime(2018, 1, 10, 9, 0, 0, DateTimeKind.Utc);
        e.End = new DateTime(2018, 1, 12, 18, 0, 0, DateTimeKind.Utc);
        e.Attendees.Add(message.To.First()); 
        calendar.Events.Add(e);

        // Add a calendar to the mail message.
        message.Calendars.Add(calendar);

        // ... a mail message can be sent using SMTP or saved to a file.

        message.Save("Add Calendar to a Message.eml");
    }
}
Imports System
Imports System.Text
Imports GemBox.Email
Imports GemBox.Email.Calendar

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' Create new message
        Dim message = New MailMessage(New MailAddress("sender@example.com", "Sender"),
                      New MailAddress("first.receiver@example.com", "First receiver"))

        message.Subject = "This is a mail message with a calendar"
        message.BodyText = "Hi! I am sending you my calendar."

        ' Create new calendar.
        Dim calendar As Calendar = New Calendar
        Dim e As [Event] = New [Event]
        e.Organizer = message.Sender
        e.Name = "GemBox conference"
        e.Start = new DateTime(2018, 1, 10, 9, 0, 0, DateTimeKind.Utc)
        e.End = new DateTime(2018, 1, 12, 18, 0, 0, DateTimeKind.Utc)
        e.Attendees.Add(message.To.First)
        calendar.Events.Add(e)

        ' Add a calendar to the mail message.
        message.Calendars.Add(calendar)

        ' ... a mail message can be sent using SMTP or saved to a file.

        message.Save("Add Calendar to a Message.eml")

    End Sub

End Module