How to Add an Image to an Email Sent with UiPath?
One of the most commonly used activities in automations carried out with UiPath is sending emails. These emails can contain information messages about the process performed, details of any erroneous processes if there are any, and many other pieces of information. In some cases, we may need to attach an image file to the email to be sent. We have two different methods for this: 1- sending the image file as an attachment, 2- sending the image file within the mail body. The first method is the easier one, where we can easily send the image by adding the file path to the attachments list. Most of us are already familiar with this method.
In this article, I will explain how we can add an image file to the body of an email when performing the email sending process in UiPath automation projects.
Now let’s see how to perform this process. First of all, if we want an image content in the body of the email, the body of the email must be in HTML format. To ensure that the body is in HTML format, we need to check the “IsBodyHtml” checkbox in all email sending activities.
The next step is to add an HTML code to the mail body. To indicate that we will add an image in our HTML code, we will use the img tag (for easy remembrance, img stands for image), and we will write the base64 format of the image we want to add as the source (src) property of this img tag. Now let’s look at how to perform this step step-by-step below.
First, we need to store the file path of the image we want to add to the email in a String variable. Let’s assume we have a variable named ImageFilePath that contains the file path of our image.
To convert this image file to Base64, we will need two more variables. Let’s create them in the Variables tab:
The variable should be of type “Array of Byte” and let’s name it ImageByteArray. The variable should be of type “String” and let’s name it ImageBase64Text.
We need to perform two consecutive assignments. Here they are:
ImageByteArray = File.ReadAllBytes(ImageFilePath) ImageBase64Text = Convert.ToBase64String(ImageByteArray)
Finally, in the part where you want to add the image to the mail body, you need to add this expression:
“<img src=’data:image/png;base64,” + ImageBase64Text + “‘>”
Hope to see you again in another article, stay well.