0
votes

I have a service that runs on SQL Integration Services, this service reads a mailbox, and forward readed emails to other recipients, all work's fine with inline images when these are added manually using outlook editor (drag and drop, or insert image). But when email is created using VBA and inserting images as attachments and naming their 'cid' when message is readed from EWS it do not recognize images as inline images.

This is how my app reads email (NormalizedBody) from EWS when email was sent using Outlook editor and images are inserted using insert image button: (I skiped other html tags and is showing only 'img' tags):

<img src=\"cid:[email protected]\" width=\"135\" height=\"166\" id=\"Imagem_x0020_1\">
<img src=\"cid:[email protected]\" width=\"135\" height=\"166\" id=\"Imagem_x0020_2\">
<img src=\"cid:[email protected]\" width=\"135\" height=\"166\" id=\"Imagem_x0020_3\">

And the attachments properties from 'image001' as example:

ContentId: "[email protected]"

IsInline: true

Now If I create the email using outlook VBA with this code:

Dim olApp As Outlook.Application
Dim olMail As Outlook.MailItem

Set olApp = New Outlook.Application

Set olMail = olApp.CreateItem(olMailItem)

olMail.To = "[email protected]"
olMail.Subject = "VBA MAIL TEST"


olMail.Attachments.Add "C:\Users\Public\_testeMailing\Image001.png", olByValue, 1, "Image001"
olMail.Attachments.Add "C:\Users\Public\_testeMailing\Image002.png", olByValue, 2, "Image002"
olMail.Attachments.Add "C:\Users\Public\_testeMailing\Image003.png", olByValue, 3, "Image003"

olMail.BodyFormat = olFormatHTML

Dim strHTML As String

strHTML = "<table><tr>" & _
"<td><img src='cid:Image001.png' /></td>" & _
"<td><img src='cid:Image002.png' /></td>" & _
"<td><img src='cid:Image003.png' /></td>" & _
"</tr></table>"

olMail.HTMLBody = strHTML
olMail.Send

When read NormalizedBody from EWS, the HTML img tags are shown like this:

<img src=\"cid:342EA5ED2B36174FA24B4FFE34AD84FA@1\">
<img src=\"cid:37448D8F2D450541A18641D2EE5BB152@1\">
<img src=\"cid:3E2CFE241DBFF943B815142BC3631890@1\">

And the properties of image001 are:

ContentId: null

IsInline: false

Perhaps, if I open this email using outlook client, the mail body is showing images correctly.

But if I try to create a message forward (CreateForward()) the images turn into attachaments (not inline) and cid positioning of images are shown broken.

I think that the problem is in the way that images are inserted into message when using VBA code and not with the reading from EWS is this correct? Is there better ways to create an email using 'cid' and inline images?

Thanks, and sorry about english errors.

1

1 Answers

0
votes

In the line

olMail.Attachments.Add "C:\Users\Public\_testeMailing\Image001.png", olByValue, 1, "Image001"
olMail.Attachments.Add "C:\Users\Public\_testeMailing\Image002.png", olByValue, 2, "Image002"

Your setting the position parameter which only works if the body is rtf https://docs.microsoft.com/en-us/office/vba/api/outlook.attachments.add ref(

This parameter applies only to email messages using the Rich Text format: it is the position where the attachment should be placed within the body text of the message. A value of 1 for the Position parameter specifies that the attachment should be positioned at the beginning of the message body. A value 'n' greater than the number of characters in the body of the email item specifies that the attachment should be placed at the end. A value of 0 makes the attachment hidden.

I'd suggest you try the answer in https://social.msdn.microsoft.com/Forums/vstudio/en-US/6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64/how-to-embed-image-in-html-body-in-c-into-outlook-mail?forum=vsto