5

I am attempting to attach an ics file to an email using the SendGrid sdk provided by them for C#.

Here's my code:

using SendGrid;
using SendGrid.Helpers.Mail;

----

var client = new SendGridClient(APIKey);
var from = new EmailAddress(email.From);
var to = new EmailAddress(email.To);
            
var msg = MailHelper.CreateSingleEmail(from, to, "test", "test");

var testcontent = "BEGIN:VCALENDAR" + Environment.NewLine;
testcontent += "PRODID:-//Google Inc//Google Calendar 70.9054//EN" + Environment.NewLine;
testcontent += "VERSION:2.0" + Environment.NewLine;
testcontent += "CALSCALE:GREGORIAN" + Environment.NewLine;
testcontent += "METHOD:REQUEST" + Environment.NewLine;
testcontent += "BEGIN:VEVENT" + Environment.NewLine;
testcontent += "DTSTART:20220418T003000Z" + Environment.NewLine;
testcontent += "DTEND:20220418T010000Z" + Environment.NewLine;
testcontent += "DTSTAMP:20220418T001419Z" + Environment.NewLine;
testcontent += "ORGANIZER;CN=Your Name:mailto:yourname@youremail.com" + Environment.NewLine;
testcontent += "UID:2gfanpbtlo35h4dk5qg53c6msa@google.com" + Environment.NewLine;
testcontent += "ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE;CN=anotheremail@email.com;X-NUM-GUESTS=0:mailto:anotheremail@email.com" + Environment.NewLine;
testcontent += "ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE;CN=Your Name;X-NUM-GUESTS=0:mailto:yourname@youremail.com" + Environment.NewLine;
testcontent += "X-MICROSOFT-CDO-OWNERAPPTID:404715488" + Environment.NewLine;
testcontent += "CREATED:20220418T001418Z" + Environment.NewLine;
testcontent += "DESCRIPTION:test" + Environment.NewLine;
testcontent += "LAST-MODIFIED:20220418T001418Z" + Environment.NewLine;
testcontent += "LOCATION:" + Environment.NewLine;
testcontent += "SEQUENCE:0" + Environment.NewLine;
testcontent += "STATUS:CONFIRMED" + Environment.NewLine;
testcontent += "SUMMARY:test" + Environment.NewLine;
testcontent += "TRANSP:OPAQUE" + Environment.NewLine;
testcontent += "END:VEVENT" + Environment.NewLine;
testcontent += "END:VCALENDAR";

var base64 = Encoding.UTF8.GetBytes(testcontent);
var attachment = new Attachment();
attachment.Content = Convert.ToBase64String(base64); 
attachment.Filename = email.AttachmentName;
attachment.Disposition = "attachment"; 
attachment.Type = "text/calendar;method=request";
msg.AddAttachment(attachment);

var sg_response = client.SendEmailAsync(msg).GetAwaiter().GetResult();

This code sends the email just fine however I receive the ics file that is attached as a simple attachment and not as a "properly interpreted" calendar invite.

For example, if I use Gmail to create a calendar item and invite an email account that uses Outlook I receive an email that looks like the following:

enter image description here

Now if I use the above snippet of code to send the exact same ICS file that was generated by Gmail, it simply displays as an attachment. Like the following:

enter image description here

So clearly the issue is not the formatting of the contents of ICS file itself but something in how I am transmitting the email to SendGrid or how I am attaching the ICS file to the email message.

However everything I've seen on Stack Overflow seems to point to making sure the MIME/Content-Type is "text/calendar" but I feel like I'm doing this correctly and it doesn't seem to matter what I do.

Jeremy
  • 81
  • 1
  • 8
  • Have you taken a look at the answers from this question? There may be something helpful there, but I am not sure it'll solve your question: https://stackoverflow.com/questions/67149571/how-to-send-ics-calendar-invite-through-sendgrid-so-that-it-renders-in-email-cl – Swimburger Apr 22 '22 at 16:54
  • So I finally got around looking into this, and when I run your sample, outlook and gmail both render a calendar widget to add the ICS file to their respective calendar applications. My outlook does look different than your outlook, is your outlook an older Exchange web mail server? – Swimburger Apr 26 '22 at 21:05
  • It doesn't look right in either Outlook Web Access or Microsoft® Outlook® for Microsoft 365 MSO (Version 2203 Build 16.0.15028.20218) 64-bit.... which I'm pretty sure is the latest and greatest. – Jeremy Apr 27 '22 at 14:16
  • If I send the invite to a Gmail-based account it does render correctly. So what's happening leads me to believe that it's very MS/Outlook related. – Jeremy Apr 27 '22 at 14:17
  • Here's what it looks like in my consumer Outlook: https://imgur.com/a/y1blcMy – Swimburger Apr 27 '22 at 17:21

0 Answers0