function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ilewi121ilewi121 

MIME Settings for ICS as multipart/alternative

I am building an email with Messaging.SingleEmailMessage. The email should include TXT, HTML, and ICS versions and be received by an Outlook recipient as a calendar invite (not as an email with an attachment). The key is in setting the ICS attachment as an option in the same "content-type: multipart/alternative" section as the TXT and HTML versions. However, when I build the email with setPlainTextBody, setHtmlBody, and setFileAttachments, it sets the ICS attachment in its own "content-type" section.

How can I write my code to bring the .ICS file into the "Content-type: multipart/alternative" block with the TXT & HTML?

My current code:
public static void sendEventInviteICS(String bodyTXT, String bodyHTML, String subject, String fromAddress, String displayName, List<String> toAddress, List<String> ccAddress){
    //Create Message
    Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
    msg.setReplyTo(fromAddress);
    msg.setSenderDisplayName(displayName);
    msg.setToAddresses(toAddress);
    msg.setCcAddresses(ccAddress);
    msg.setSubject(subject);
    //Create TXT Version
    msg.setPlainTextBody(bodyTXT);
    //Create HTML Version
    msg.setHTMLBody(bodyHTML);
    //Create ICS Attachment
    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
    efa.setFileName('Meeting Invite.ics');
    efa.setContentType('text/calendar');
    Blob b = buildICSAttachment();
    efa.setBody(b);
    msg.setFileAttachments(new Messaging.EmailFileAttachment[]{efa});
    //Send the Email
    Messaging.sendEmail(new Messaging.SingleEmailMessage[]{msg});
}
MIME result from current code:
Date: Tue, 9 Dec 2014 21:16:26 +0000
From: Isaac Lewis <ilewis@afs.net>
Reply-To: <djillumine@gmail.com>
To: "ilewis@afs.net" <ilewis@afs.net>
Subject: Sandbox: This is the subject
MIME-Version: 1.0
Content-type: multipart/mixed;
	boundary="B_3500983009_1157947"

> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--B_3500983009_1157947
Content-type: multipart/alternative;
	boundary="B_3500983009_1123381"


--B_3500983009_1123381
Content-type: text/plain;
	charset="US-ASCII"
Content-transfer-encoding: 7bit

This is the text body


--B_3500983009_1123381
Content-type: text/html;
	charset="US-ASCII"
Content-transfer-encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8">
</head>
<body>
<i>This is the html body</i>
</body>
</html>


--B_3500983009_1123381--


--B_3500983009_1157947
Content-type: text/calendar; name="Attachment Name.ics"
Content-disposition: attachment;
	filename="Attachment Name.ics"
Content-transfer-encoding: base64

QkVHSU46VkNBTEVOREFSDQpNRVRIT0Q6UkVRVUVTVA0KUFJPRElEOk1pY3Jvc29mdCBFeGNo=

--B_3500983009_1157947--
Desired MIME Result (boundaries around ICS have been moved to be part of the "Content-type: multipart/alternative" block):
Date: Tue, 9 Dec 2014 21:16:26 +0000
From: Isaac Lewis <ilewis@afs.net>
Reply-To: <djillumine@gmail.com>
To: "ilewis@afs.net" <ilewis@afs.net>
Subject: Sandbox: This is the subject
MIME-Version: 1.0
Content-type: multipart/mixed;
	boundary="B_3500983009_1157947"

> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--B_3500983009_1157947
Content-type: multipart/alternative;
	boundary="B_3500983009_1123381"


--B_3500983009_1123381
Content-type: text/plain;
	charset="US-ASCII"
Content-transfer-encoding: 7bit

This is the text body


--B_3500983009_1123381
Content-type: text/html;
	charset="US-ASCII"
Content-transfer-encoding: quoted-printable

<html>
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8">
</head>
<body>
<i>This is the html body</i>
</body>
</html>

--B_3500983009_1123381
Content-type: text/calendar; name="Attachment Name.ics"
Content-disposition: attachment;
	filename="Attachment Name.ics"
Content-transfer-encoding: base64

QkVHSU46VkNBTEVOREFSDQpNRVRIT0Q6UkVRVUVTVA0KUFJPRElEOk1pY3Jvc29mdCBFeGNo=

--B_3500983009_1123381--

--B_3500983009_1157947--




 
ilewi121ilewi121
This one gets close... it shows up as an ICS in an Outlook inbox, but no "Accept" or "Decline" buttons show:
https://developer.salesforce.com/forums/ForumsMain?id=906F00000008yOnIAI
Charles Troster 4Charles Troster 4
A similar use case has popped up for one of our clients. Did you ever find a resolution to make Outlook entirely happy with an ICS invite?
Avi MrAvi Mr
In the above class sendEventInviteICS class, please change 

efa.setContentType('text/calendar');  to  efa.setContentType('text/calendar; charset=utf-8; method=REQUEST');

Please refer below link with similar issue:

https://developer.salesforce.com/forums/?id=906F0000000BP8TIAW

Hope this works for you.