Adding calendar invites as alternative views using Mailgun API in C#

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?

yeah, mailgun lacks native support for alternative views. you’ll have to create the mime msg by yourself, setting the right content-type headers, and use the messages.mime endpoint for sending it. it’s a bit of extra work but should do the trick!

I struggled with this exact problem for weeks before finding the right approach. The issue is that Mailgun’s standard API treats everything as either body content or attachments, with no middle ground for alternative views. What worked for me was switching to their MIME API endpoint and constructing the email structure manually. You need to build a proper multipart/alternative message where the calendar data sits alongside your HTML content at the same level in the MIME hierarchy. The critical part is ensuring the calendar section has the correct headers including Content-Type: text/calendar; method=REQUEST; charset=UTF-8. Once I got this structure right, Outlook started displaying the calendar invitations properly instead of showing them as downloadable files. The downside is you lose some of the convenience of Mailgun’s simple parameter-based API, but it’s the only way to achieve proper calendar integration across all email clients.

I had the exact same issue when migrating from System.Net.Mail to Mailgun. The solution is to manually construct the MIME message with proper multipart structure. You need to create a multipart/alternative container that includes your HTML content and the calendar invitation as separate parts, not attachments. Use Mailgun’s raw MIME endpoint instead of the regular messages endpoint. Set the calendar part with Content-Type: text/calendar and method=REQUEST parameter. I found that building the MIME structure with MimeKit library made this much easier than doing it manually with string concatenation. The key difference is that alternative views are embedded within the message structure rather than appended as separate files.