Maurice van Lieshout
Maurice van Lieshout

Reputation: 141

How to send an iCalendar the same way Outlook does in .NET

Currently I'm able to send an email with MailKit with an ics file as attachment created with iCal.net. This works but I only get to see it as an attachment. When I create an appointment manually in Outlook I'll get the fancy UI.

Outlook Example

When I create it programmatically I'll get this.

enter image description here

I want to create the fancy UI programmatically. It seems it has nothing to do with the ICS because I manually created an ICS file through Outlook. And used that ics file to send through my application and still no success.

static void Main(string[] args)
    {
        string serializedCalendar = CreateRecurringEvent();
        File.WriteAllText(@"F:\test.ics", serializedCalendar);
        SendMail();
    }

    private static void SendMail()
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Joey Tribbiani", "[email protected]"));
        message.To.Add(new MailboxAddress("Monica Geller", "[email protected]"));
        message.To.Add(new MailboxAddress("Chandler Muriel Bing", "[email protected]"));
        message.Subject = "How you doin?";

        var builder = new BodyBuilder
        {
            TextBody = @"<bodytext>"
        };

        builder.Attachments.Add(@"F:\test.ics");
        message.Body = builder.ToMessageBody();

        using (var client = new SmtpClient())
        {
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            client.Connect("<host>", 235236);
            client.Authenticate("<username>", "<password>");
            client.Send(message);
            client.Disconnect(true);
        }
    }

    private static string CreateRecurringEvent()
    {
        DateTime now = DateTime.Now;
        DateTime later = now.AddHours(1);
        var rrule = new RecurrencePattern(FrequencyType.Daily, 1) { Count = 5 };

        var e = new CalendarEvent
        {
            Start = new CalDateTime(now),
            End = new CalDateTime(later),
            RecurrenceRules = new List<RecurrencePattern> { rrule }
        };

        var calendar = new Calendar();
        calendar.Events.Add(e);

        var serializer = new CalendarSerializer();
        return serializer.SerializeToString(calendar);
    }
}

Upvotes: 2

Views: 2683

Answers (1)

jstedfast
jstedfast

Reputation: 38608

Here's how you can create the meeting request that will work with Outlook:

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Joey Tribbiani", "[email protected]"));
message.To.Add(new MailboxAddress("Monica Geller", "[email protected]"));
message.To.Add(new MailboxAddress("Chandler Muriel Bing", "[email protected]"));
message.Subject = "How you doin?";

var mixed = new Multipart ("mixed");
mixed.Add (new TextPart ("plain") {
    ContentTransferEncoding = ContentEncoding.QuotedPrintable,
    Text = "Would you like to join this meeting?"
});

var ical = new TextPart ("calendar") {
    ContentTransferEncoding = ContentEncoding.Base64,
    Text = CreateRecurringEvent (),
};

ical.ContentType.Parameters.Add ("method", "REQUEST");

mixed.Add (ical);

message.Body = mixed;

Upvotes: 3

Related Questions