Including Pictures in an Outlook Email
First of all, you are setting the plain text MailItem.Body
property.
Secondly, create an attachment and set the PR_ATTACH_CONTENT_ID
property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
) using Attachment.PropertyAccessor.SetProperty
.
Your HTML body (MailItem.HTMLBody
property) would then need to reference that image attachment through the cid attribute:
<img src="cid:xyz">
where xyz is the value of the PR_ATTACH_CONTENT_ID
property.
Look at an existing message with OutlookSpy (I am its author - click IMessage button).
EDIT: sample script (off the top of my head):
attachment = MailItem.Attachments.Add("c:\temp\MyPicture.jpg")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
MailItem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"
How to embed an image into an Outlook HTML email message?
If a manual step in the process is an option, you can do it like this:
- create the HTML automatically or manually as a saved HTML file, with
<img>
tags that link to locally-saved image files - attach the HTML to an Outlook email using the 'Insert as text' option tucked away under the down-arrow on the "Insert" button when selecting the HTML file as an attachment.
This populates the HTML email with the HTML from the file and embeds all images. I believe this tool uses Base64 encoding to embed the images as described in Sam's email.
Depending on the workflow, this method might be easier than converting images to Base64 yourself.
Note that this method only works in Outlook for Windows, not Outlook for Mac.
How to insert HTML (including images) in an e-mail in Outlook 2016+
The answer is very simple. In a way, it's obvious, but in another way, it's not.
The answer is that each segment of HTML code inserted has to be a complete HTML file, including the <!DOCTYPE>
, <HTML>
and <body>
tags, not just the desired HTML code. The reason this is not obvious is because if you insert multiple code segments, each one has to be a complete HTML file, which is something you would never do when actually writing HTML. I presume that what is happening is that when Outlook detects a valid HTML file being "Inserted as Text", it strips the opening and closing <!DOCTYPE>
, <HTML>
and <body>
tags and then inserts the code that was between them -- as HTML, not as text.
So, the solution I found was that instead of the single line of code shown in the question, I need to "Insert as Text" a file containing:
<!DOCTYPE html>
<html>
<body>
<img src="https://www.lenetek.com/blog/how-to-create-html-emails-in-outlook/images/attach_file.jpg" alt="Random online image">
</body>
</html>
When I insert that as text in my e-mail, I see the image, not the code.
In all the sources I found online that said to use "Insert as Text" to insert HTML in Outlook, none of them said it had to be a complete HTML file instead of just the desired code. So maybe this Q&A will be helpful to someone else, if I'm not the only person who had to scratch my head for a long time before thinking of that.
========================
Added details about using HTML in the e-mail:
As pointed out in the Lenetek article linked in the question, Outlook does not support all HTML tags. In particular, for embedding images, I have found:
When sending from Outlook:
Outlook does not support <figure>
and <FigCaption>
. I found that an image and caption placed in those tags were rendered inline, just ignoring the tags. For floating to the right margin, I found I was able to get the same results by replacing <figure>
with <table>
and then placing the image and its caption each inside of <TR><TD>...</TD></TR>
.
When receiving in Outlook:
There are differences in how different e-mail clients interpret HTML, which is probably why some e-mails come with a link at the top for viewing the e-mail in one's browser. In particular, I've read that Outlook is not well behaved in this regard. And that was the case with my right-floated image referred to above.
After doing the "Insert as Text" trick, the image appeared correctly at the right margin in the draft e-mail in Outlook, but when it was sent, the CSS style float
attribute was ignored and the table appeared by itself at the left margin with no text wrapped around it. I was able to fix this by, in the <table>
tag, replacing the style attribute float: right;
with the old-fashioned HTML attribute align="right"
. With that, the image and caption appeared correctly at the right margin when received in Outlook. I have not tested what it looks like in other e-mail clients.
Receiver can't see images in Outlook Email
It sure sounds like you are sending in the RTF format. You need to send an HTML formatted message with with an image added as an attachment and referenced by the HTML body.
Embed picture in outlook mail body excel vba
You need to add the image and hide it. The position 0
will add and hide it.
.Attachments.Add Fname, 1, 0
The 1
is the Outlook Constant olByValue
Once you add the image then you have to use "cid:FILENAME.jpg"
as shown below.
Try this
With OutMail
.To = tName
.CC = ""
.BCC = ""
.Subject = STAT.Range("C1").Value
.Attachments.Add Fname, 1, 0
.HTMLBody = "<html><p>Summary of Claim Status.</p>" & _
"<img src=""cid:Claims.jpg""height=520 width=750>"
.Display
End With
Screenshot
Related Topics
Case-Insensitive Dictionary with String Key-Type in C#
Which Files in a Visual C# Studio Project Don't Need to Be Versioned
Using Ssl and Sslstream for Peer to Peer Authentication
How to Use Async to Increase Winforms Performance
How to Download/Upload Files From/To Sharepoint 2013 Using Csom
Httpclient Single Instance with Different Authentication Headers
Converting an Int[] to Byte[] in C#
How to Get Next (Or Previous) Enum Value in C#
Overriding Gethashcode for Mutable Objects
How to Use Acrylic Accent in Windows 10 Creators Update
How to Programmatically Limit My Program's CPU Usage to Below 70%
C#: Getting Names of Properties in a Chain from Lambda Expression
How to Use Custom Authorize Attribute for Roles as Well as a Specific User
Intersect with a Custom Iequalitycomparer Using Linq
Convert List to Dictionary Using Linq and Not Worrying About Duplicates
Explicitly Freeing Memory in C#
Allow Access Permission to Write in Program Files of Windows 7