15-05-24 12:24 PM
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:
Please assist me to solve this issue, Thanks.
16-05-24 09:21 AM
Hi Ginukuntla,
Can you check this site : Send Email with Embedded Images in VB.NET (emailarchitect.net)
It uses VB net and html.
Regard
16-05-24 02:32 PM
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.
20-05-24 02:22 PM
Thankyou @expertcr
Is there any alternative solution to achieve and to overcome this issue.
21-05-24 09:11 AM - edited 21-05-24 09:12 AM
Hi @Ginukuntla,
Use this VBO to convert the image itself to base64: Output -> [imageBase64]
You can find it on Digital Exchange.
After that just add the following code to the HTML email message:
I hope this helps.
21-05-24 03:43 PM
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
23-05-24 01:46 PM - edited 23-05-24 01:49 PM
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:
Is there any alternate solution for this.
Thanks in advance.
24-05-24 01:02 AM - edited 24-05-24 01:07 AM
Hi @Ginukuntla,
You can send the images as part of the email body by following the below steps:
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:
First, run the 'Configure' page and then run the 'Send Email With Image' page to test it from the object studio itself:
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.
24-05-24 02:12 PM - edited 24-05-24 02:16 PM
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.
24-05-24 09:55 PM - edited 24-05-24 09:58 PM
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:
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.