How to send embedded images in an email using UiPath?
Issue Overview
In most email clients, it is possible to send embedded images in an email. A common question is how to do this using UiPath. This guide will explain a few different ways that this can be done.
Technical Overview
This guide will cover three ways of embedding in image in a UiPath email.
-
Using a base64 encoded image - This is one of the easiest methods but clients like Gmail will block the image from being displayed
-
Embedding the image by including it as an email attachment - This is also very easy to do but clients like Gmail will block the image from being displayed
-
Embedding the image by including it as a linked resource - This should work for most email clients but is the most complicated.
Each one of these methods consists of three steps:
-
Creating the email body in html
-
Creating the mail message object
-
and finally, Sending the message.
All the examples will use the following HTML string for the body of the message.
HTML BASE
<html>
<body>
<div>
<p>Hi All,</p>
<p>This is a test of an email with an embedded image.</p>
<p class='MsoNormal'>
<img src=’IMAGE STRING’/>
</p>
<p>There is text above and below the image.</p>
</div>
</body>
</html>
For convenience here is the same string without spacing.
<html><body><div><p>Hi All,</p><p>This is a test of an email with an embedded image.</p><p class='MsoNormal'><img src=’IMAGE STRING’/></p><p>There is text above and below the image.</p></div></body></html>
1. Create a new automation.
2. Create an assign activity. It will assign the above HTML base to a variable called HtmlBody.
3. Encode the image in to base64. Doing this programmatically is complicated and there are free sites online that can be used for this. Just google “Encode image to base64”
4. The encoding string should look something like: “data:image/png;base64, <a very long encoded random looking string>”. Add the part “data:image/png;base64,”
5. Replace the IMAGE STRING tag with the encoded string.
a. i.e. <img src="data:image/png;base64,iVBORw0KGgoAA…TkSuQmCC">
6. Pass the string to the Send Outlook Messages activity.
7. Make sure the isBodyHTML property is checked.
8. Try sending the email.
9. Please see the attached automation.
Using the Attachment Method
1. Create a new automation
2. Create an assign activity. It will assign the above HTML base to a variable called HtmlBody.
3. Replace the IMAGE STRING tag with a contend-id reference.
a. The Content ID reference consists of the string CID and the name of the image. For example, if the name of the image was “image.png” the replacement string would be “cid:image.png”. Just the name of the image needs to be specified.
b. Example updated image tag: <img src="cid:image.png">
4. Pass the string to the Send Outlook Messages activity.
5. In the Send Outlook Messages activity, go to Properties and Attachments. Specify the path to the image that is to be embedded. The path can be relative to the project directory.
6. Make sure the isBodyHTML property is checked.
7. Try sending the email.
8. The attached image should show up as an embedded image in the email.
9. Please see the attached automation.
Using the Linked Resource Method
1. Create a new automation.
2. Create an assign activity. It will assign the above HTML base to a variable called HtmlBody.
3. Replace the IMAGE STRING tag with a contend-id reference.
a. The Content ID reference consists of the string CID and the name of the image. For example, if the name of the image was “image.png” the replacement string would be “cid:image.png”. Just the name of the image needs to be specified.
b. Example updated image tag: <img src="cid:image.png">
4. Create a linked resource using the image. The linked resource needs to have the name “image.png”.
5. The MailMessage object cannot be sent using the built in UiPath tools. Instead the email needs to be sent using the vb.net code. Currently, the UiPath mail activities will ignore any linked resources associated with any MailMessage object passed to them.
6. Please see the attached automation.