I’m trying to include an iCal calendar invitation as an alternative view in emails sent through Mailgun’s REST API. Right now my code adds the calendar data as a file attachment instead of embedding it properly in the email.
Here’s my current implementation:
var mailRequest = new RestRequest();
mailRequest.AddParameter("domain", this.mailDomain, ParameterType.UrlSegment);
mailRequest.Resource = "{domain}/messages";
mailRequest.AddParameter("from", senderEmail.Address);
mailRequest.AddParameter("to", recipientEmail.Address);
mailRequest.AddParameter("subject", emailContent.Title);
mailRequest.AddParameter("text", emailContent.PlainText);
mailRequest.AddParameter("html", emailContent.HtmlContent);
if (!string.IsNullOrWhiteSpace(emailContent.CalendarData))
{
mailRequest.AddFileBytes("attachment",
Encoding.UTF8.GetBytes(emailContent.CalendarData),
"calendar.ics",
"text/calendar");
}
mailRequest.Method = Method.POST;
return mailRequest;
The problem is that Gmail handles this fine, but Outlook shows it as a downloadable file that users have to manually open and accept. I need it to work like a proper calendar invitation.
When I use the standard .NET mail classes, I can add it as an alternative view like this:
var calendarContentType = new ContentType("text/calendar");
if (calendarContentType.Parameters != null)
{
calendarContentType.Parameters.Add("method", "REQUEST");
calendarContentType.CharSet = "UTF-8";
}
mailMessage.AlternateViews.Add(
AlternateView.CreateAlternateViewFromString(
emailContent.CalendarData,
calendarContentType));
Is there a way to achieve the same result using Mailgun’s REST API so the calendar invite appears as an integrated part of the email instead of just an attachment?