cancel
Showing results for 
Search instead for 
Did you mean: 

How to attach image in body of the mail.

Ginukuntla
Level 2

HI,

We are trying to insert a image within the body of email using Email-POP3/SMTP VBO

We have tried these logics in body of the send message action:

"<html><p><img src="&[Attachment Path]&"> name </img><p><html>" and also "<html><p><img id="&"ImageID"&" src="&[Attachment Path]&""&":image/png;base64, Base64Code"&"""<p><html>"

Enabled body is HTML = True
But we are receiving message as red cross mark image in the email body.

Please find attachments below for your reference:

Ginukuntla_0-1715771927456.png

Please assist me to solve this issue, Thanks.

 

9 REPLIES 9

leogarp
Level 3

Hi Ginukuntla

Can you check this site : Send Email with Embedded Images in VB.NET (emailarchitect.net)

It uses VB net and html.

Regard

expertcr
Staff
Staff

Just remember that when using HTML and calling images, they need to be available on the Internet; if they are on the local machine or intranet, you will keep having that issue.

Thankyou @expertcr 

Is there any alternative solution to achieve and to overcome this issue.

Erika_Papp
Level 4

Hi @Ginukuntla,

Use this VBO to convert the image itself to base64: Output -> [imageBase64] 

You can find it on Digital Exchange.

Erika_Papp_0-1716278933850.png

After that just add the following code to the HTML email message:

Erika_Papp_1-1716279157514.png

 

I hope this helps.

 

Dear Ginukuntla,

Enter the body of your email in plain text (along with image you want to embed) to https://wordtohtml.net/ , and the website will quickly format it into HTML code according to the correct format. Then include that in the body of your email action.

This website will provide you with the HTML code that will function with Outlook and all other mail clients after encoding your image into base 64.

Thanks,

Shahabeel

 

Thanks, @Erika_Papp 

I have tried using utility- convert the image to base64, and added the code in the body that you have mentioned. but still facing the same issue.

Please find the below screenshot for reference:

Ginukuntla_0-1716468148878.png

Is there any alternate solution for this.

Thanks in advance.

 

Hi @Ginukuntla,

You can send the images as part of the email body by following the below steps:

  • Open the SMTP/POP3 business object and make a copy of the action - 'Send Email' and you can name it as 'Send Email With Image' as shown below:
    devneetmohanty07_0-1716507468318.png

     



  • Now, add a data item called 'ImagePath' of text type and add it as an input argument to your code stage as well:

    devneetmohanty07_1-1716507691430.png

     

  • Now, in the code tab enter the following code:

 

 

SmtpClient client = new SmtpClient();
try
{
	client.Host = Server;
	client.Port = (int)Port;
	if (Username != "")
		client.Credentials = new NetworkCredential(Username,Password);
	client.EnableSsl = UseSSL;

	using(MailMessage mail = new MailMessage())
	{
		mail.From = new MailAddress(From);
		mail.To.Add(To);
		mail.Subject = Subject;
		mail.IsBodyHtml = BodyIsHTML;
		
		
		// Embed image in the email body
                if (BodyIsHTML && !string.IsNullOrEmpty(ImagePath) && File.Exists(ImagePath))
                {
                    LinkedResource inlineImage = new LinkedResource(ImagePath, MediaTypeNames.Image.Jpeg);
                    inlineImage.ContentId = Guid.NewGuid().ToString();

                    string htmlBody = Body + $"<br><br><img src='cid:{inlineImage.ContentId}' />";
                    AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
                    avHtml.LinkedResources.Add(inlineImage);

                    mail.AlternateViews.Add(avHtml);
                }
                else
                {
                    mail.Body = Body;
                }

		foreach(DataRow dr in Attachments.Rows)
		{
			string file = dr["Path"].ToString();
			Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
			ContentDisposition dis = data.ContentDisposition;
			dis.CreationDate = File.GetCreationTime(file);
			dis.ModificationDate = File.GetLastWriteTime(file);
			dis.ReadDate = File.GetLastAccessTime(file);
			mail.Attachments.Add(data);
		}

		client.Send(mail);
	}
}
catch(Exception ex)
{
	string msg = ex.Message;
	if(ex.InnerException != null) {
		msg += " - " + ex.InnerException.Message;
	}
	throw new Exception(msg);
}
finally
{
	IDisposable disposableClient = client as IDisposable;
	if (disposableClient!=null)
		disposableClient.Dispose();
}

 

 

To summarize the change, we are creating an alternate view that will comprise of our image as part of the HTML content of the email body.

  • Now, you can pass the configuration parameters for your SMTP call and set IsHTMLBody parameter as 'True' and pass the image file path as well:

    devneetmohanty07_2-1716508453518.png

     

     

  • First, run the 'Configure' page and then run the 'Send Email With Image' page to test it from the object studio itself:

    devneetmohanty07_3-1716508607893.pngdevneetmohanty07_4-1716508796841.png
  • Now, when you have tested the action, ensure you add the ImagePath variable as an Input Parameter in the Start stage, reset or clear all the initial values from your data items in both 'Configure' and 'Send Email With Image' page and finally publish the action 'Send Email With Image' prior to saving the business object and testing the same from Process Studio.

     

     


Hope it helps you out and if my solution resolves your query, then please mark it as the best answer

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Technical Business Analyst,
WonderBotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------

Hi @devneetmohanty07 

Is it possible to send multiple images in body of the mail. If Possible, please assist me.

For Example, if we have three images in our folder, after sending mail I want to see three images in body of that mail.

Thanks in advance.

Hi @rokkam_saiteja ,

Certainly, if your requirement is to send multiple images as part of the email body itself, I would advise to follow the same approach with I have suggested in the previous replies with certain tweaks:

  • Open the SMTP/POP3 business object and make a copy of the action - 'Send Email' and you can name it as 'Send Email With Multiple Images' as shown below:

    devneetmohanty07_0-1716583077001.png

     


  • Now, add a collection called 'ImagePaths' with a field value called 'ImagePath' of text type and add it as an input argument to your code stage as well:

    devneetmohanty07_1-1716583264852.png

     

  •  Now, in the code tab enter the following code:

 

SmtpClient client = new SmtpClient();

try
{
    client.Host = Server;
    client.Port = (int)Port;
    if (!string.IsNullOrEmpty(Username))
        client.Credentials = new NetworkCredential(Username, Password);
    client.EnableSsl = UseSSL;

    using (MailMessage mail = new MailMessage())
    {
        mail.From = new MailAddress(From);
        mail.To.Add(To);
        mail.Subject = Subject;
        mail.IsBodyHtml = BodyIsHTML;

        // Embed image in the email body
        if (BodyIsHTML && ImagePaths.Rows.Count > 0)
        {
            string htmlBody = Body;
            LinkedResource[] linkedResources = new LinkedResource[ImagePaths.Rows.Count];
            for (int i = 0; i < ImagePaths.Rows.Count; i++)
            {
                string imageFile = ImagePaths.Rows[i]["ImagePath"].ToString();
                linkedResources[i] = new LinkedResource(imageFile, MediaTypeNames.Image.Jpeg)
                {
                    ContentId = Guid.NewGuid().ToString()
                };

                // Append image HTML to the body
                htmlBody += $"<br><br><img src='cid:{linkedResources[i].ContentId}' style='width:200px; height:auto;'/>";
            }

            AlternateView avHtml = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
            foreach (var linkedResource in linkedResources)
            {
                avHtml.LinkedResources.Add(linkedResource);
            }
            mail.AlternateViews.Add(avHtml);
        }
        else
        {
            mail.Body = Body;
        }

        // Add attachments
        foreach (DataRow dr in Attachments.Rows)
        {
            string file = dr["Path"].ToString();
            Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
            ContentDisposition dis = data.ContentDisposition;
            dis.CreationDate = File.GetCreationTime(file);
            dis.ModificationDate = File.GetLastWriteTime(file);
            dis.ReadDate = File.GetLastAccessTime(file);
            mail.Attachments.Add(data);
        }

        client.Send(mail);
    }
}
catch (Exception ex)
{
    string msg = ex.Message;
    if (ex.InnerException != null)
    {
        msg += " - " + ex.InnerException.Message;
    }
    throw new Exception(msg);
}
finally
{
    IDisposable disposableClient = client as IDisposable;
    if (disposableClient != null)
        disposableClient.Dispose();
}

 


Main difference in this code is that, instead of one image path we are iterating over the collection of image paths and setting up an array of LinkedResource objects based on the number of image paths provided.

Then, each of these objects have a contentId which is getting appended to an HTML string with an adjustable width so that they do not span all across the email body.

Finally, we create an alternate view out of these HTML strings and then the LinkedResource objects are also added to the alternate view as well.

  •  Now, you can pass the configuration parameters for your SMTP call and set IsHTMLBody parameter as 'True' and pass the image file paths as part of the collection:
    devneetmohanty07_2-1716583808396.png

     



  • First, run the 'Configure' page and then run the 'Send Email With Multiple Images' page to test it from the object studio itself:
    devneetmohanty07_3-1716584018715.png

     



  • Now, when you have tested the action, ensure you add the ImagePaths collection as an Input Parameter in the Start stage, reset or clear all the initial values from your data items in both 'Configure' and 'Send Email With Multiple Images' page and finally publish the action 'Send Email With Multiple Images' prior to saving the business object and testing the same from Process Studio.

    From process studio, just ensure you have a collection with all your image paths saved in individual rows with the field name set as 'ImagePath' and you can pass that to your newly created action now.


Hope it helps you out and if my solution resolves your query, then please mark it as the best answer

Regards,
Devneet Mohanty
Intelligent Process Automation Consultant | Technical Business Analyst,
WonderBotz India Pvt. Ltd.
Blue Prism Community MVP | Blue Prism 7x Certified Professional
Website: https://devneet.github.io/
Email: devneetmohanty07@gmail.com

----------------------------------