• Avi Mr
  • NEWBIE
  • 35 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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--