email - How to send a iCal invite with Mailgun Rest API (C#) -


i attempting add calendar invite in ical format email sent via mailgun api. have far:

var request = new restrequest();  request.addparameter("domain", this.domain, parametertype.urlsegment); request.resource = "{domain}/messages"; request.addparameter("from", contactdetails.senderaddress); request.addparameter("to", contactdetails.recipientaddress); request.addparameter("subject", message.subject); request.addparameter("text", message.textbody); request.addparameter("html", message.htmlbody);  if (!string.isnullorwhitespace(message.icalattachment)) {     request.addfilebytes("attachment",                           encoding.utf8.getbytes(message.icalattachment),                           "invite.ics",                           "text/calendar"); }  request.method = method.post; return request; 

this results in calendar being included in email attachment, not alternative view of email. attachment works fine in gmail in outlook appears attachment file must first click on, agree adding calendar outlook calendar. there way use rest api calendar invites sent correctly, alternative email views?

to clear, how send calendar invite using .net smtpclient:

var contenttype = new contenttype("text/calendar"); if (contenttype.parameters != null) {     contenttype.parameters.add("method", "request");     contenttype.charset = "utf-8"; }  // same way add html view message request.alternateviews.add(     alternateview.createalternateviewfromstring(         message.icalattachment,          contenttype)); 

special mailgun support pointing me in right direction. relevant part or response was:

you can use /message.mime endpoint construct mime calendar invite: https://documentation.mailgun.com/api-sending.html#sending

creating mime message isnt easy using /message endpoint there several .net libraries available this. used mimekit in example.

var request = new restrequest();  request.addparameter("domain", this.domain, parametertype.urlsegment); request.resource = "{domain}/messages.mime"; request.addparameter("to", contactdetails.recipientaddress); request.addfile(     "message",      encoding.utf8.getbytes(buildmimecontent(message)),      "message.mime");  request.method = method.post; return request; 

the mime content want create contain multipart/mixed body, in turn contain multipart/alternative every attachment. calendar invite attached twice, alternative view , attachment. aid in compatibilitiy across different email clients.

the implementation of buildmimecontent(message) looks following:

// create alternative views var textbody = new textpart("plain") { text = message.textbody }; var htmlbody = new textpart("html") { text = message.htmlbody };  // add views multipart/alternative var alternative = new multipart("alternative"); alternative.add(textbody); alternative.add(htmlbody);  if (!string.isnullorwhitespace(message.calendarinvite)) {     // add calendar alternative view     // encoded base64, 7bit work     var calendarbody = new textpart("calendar")     {         text = message.calendarinvite,         contenttransferencoding = contentencoding.base64     };      // clients wont recognise alternative view without      // method=request header     calendarbody.contenttype.parameters.add("method", "request");     alternative.add(calendarbody); }  // create multipart/mixed contain multipart/alternative // , attachments var multipart = new multipart("mixed") { alternative }; if (!string.isnullorwhitespace(message.calendarinvite)) {     // add calendar attachment     var calattachment = new mimepart("application", "ics")     {         contentdisposition = new contentdisposition(contentdisposition.attachment),         contenttransferencoding = contentencoding.base64,         filename = "invite.ics",         contentobject = new contentobject(generatestreamfromstring(message.calendarinvite))     };      multipart.add(calattachment); }  // todo: add other attachements 'multipart' here.  // build final mime message var mimemessage = new mimemessage(); mimemessage.from.add(getmimeaddress(message.messageinfo.sendername, message.messageinfo.senderaddress)); mimemessage.to.add(getmimeaddress(message.messageinfo.recipientname, message.messageinfo.recipientaddress)); mimemessage.subject = message.subject; mimemessage.body = multipart;  // parse , return mime message return mimemessage.tostring(); 

warning people testing office 365

office365 extremely picky when comes validating calendar invites. in order not message 1 below, need ensure vcal's organizer email address matches email's from address. not possible if using mailgun's sandbox test environment.

not supported calendar mesage


Comments