Send email as calendar invite/appointment in SendGrid C#

Viewed 9888

I would like to send an email with calendar invite/appointment to both Outlook as well as non-Outlook client like gmail/yahoo. My application is hosted on Azure and I am using SendGrid for sending emails. Emails part is working just fine but I haven't found any fully working solution that works with both Outlook and other email clients. Here's the code snippet I am using to send email:

var client = new SendGridClient(this.apiKey);
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(
            new EmailAddress(Sender, SenderName),
            recipients, subject, textcontent, htmlcontent);

if (isMeetingRequest)
{
    Attachment attachment = new Attachment(); 
    attachment.Filename = "calendar.ics";
    attachment.Content = htmlcontent;
    attachment.Type = "text/calendar";
    msg.Attachments = new List<Attachment> { attachment };
}
await client.SendEmailAsync(msg);

The htmlContent comes from another coding snippet that forms the calendar invite string:

private static string MeetingRequestString(string from, List<string> toUsers, string subject, string desc, DateTime startTime, DateTime endTime)
    {
        StringBuilder str = new StringBuilder();

        str.AppendLine("BEGIN:VCALENDAR");
        str.AppendLine("PRODID:-//Microsoft Corporation//Outlook 12.0 MIMEDIR//EN");
        str.AppendLine("VERSION:2.0");
        str.AppendLine(string.Format("METHOD:REQUEST"));
        str.AppendLine("BEGIN:VEVENT");

        str.AppendLine(string.Format("DTSTART:{0:yyyyMMddTHHmmssZ}", startTime));
        str.AppendLine(string.Format("DTSTAMP:{0:yyyyMMddTHHmmss}", DateTime.Now));
        str.AppendLine(string.Format("DTEND:{0:yyyyMMddTHHmmssZ}", endTime));
        str.AppendLine(string.Format("UID:{0}", Guid.NewGuid().ToString()));
        str.AppendLine(string.Format("DESCRIPTION:{0}", desc.Replace("\n", "<br>")));
        str.AppendLine(string.Format("X-ALT-DESC;FMTTYPE=text/html:{0}", desc.Replace("\n", "<br>")));
        str.AppendLine(string.Format("SUMMARY:{0}", subject));

        str.AppendLine(string.Format("ORGANIZER;CN=\"{0}\":MAILTO:{1}", from, from));
        str.AppendLine(string.Format("ATTENDEE;CN=\"{0}\";RSVP=TRUE:mailto:{1}", string.Join(",", toUsers), string.Join(",", toUsers)));

        str.AppendLine("BEGIN:VALARM");
        str.AppendLine("TRIGGER:-PT15M");
        str.AppendLine("ACTION:DISPLAY");
        str.AppendLine("DESCRIPTION:Reminder");
        str.AppendLine("END:VALARM");
        str.AppendLine("END:VEVENT");
        str.AppendLine("END:VCALENDAR");

        return str.ToString();
    }

This does not seem to work. Any pointers?

2 Answers

I'm also facing the same problem It works great with Gmail but in Outlook, I see the invite.ics attachment in email but I am not seeing in Outlook Calendar. When I try to open the invite.ics from email, it displays the "Add this internet Calendar to Outlook" dialog with some warning content. If I click Yes, it adds to the calendar. So by default, the invite is not added to the calendar in Outlook

Related