Embedding image in and attaching files to email sent through Excel VBA - html

I'm writing a piece of code to populate a spreadsheet, then email it out to a bunch of people with the spreadsheet as an attachment, along with an explanatory PDF and an HTML covering letter (featuring the company logo) in the email body.
Now, I've sent emails with Excel VBA code before, and I've formatted their layout with HTML in the .HTMLBody. But the other bits are causing me some bother.
The attachments I think I can figure out; I assume I can just use multiple .Attachments.Add with the various files, haven't tried that.
The main question is the company logo in the email body. I'm fairly well up on HTML, but I can't figure out how to actually embed a picture when I don't have a src for it.
Googling has produced a few approaches, but they've been cid-based and have had comments about only working if the reader uses Outlook.
Any suggestions? Is there a nice simple solution?

try the below code,
for attaching the image to your mail(add this under create mail object),
.Attachments.Add ThisWorkbook.Path & "\image.png"
add this into your mail body (assuming you are using table)
"<td Colspan=2><img src='cid:image.png' height=205 width=1015></td>"

Related

Hyperlink doesn't display in an Outlook 2016 with HTML

I have a macro with Excel VBA that sends an Outlook 2016 email. Several columns in the data source are used to fill several variables. One of the variables (MyWedAdd) is a web site. The website is different for each line in the data source.
In the body of the email, I need to display the hyperlink to the particular website. The macro works well and sends the Outlook messages exactly like I expect it to; with one little glitch.
The website is not being displayed in the body of the email. It is blank. Following is the line of code I'm using to display the website. I'm fairly certain I have the syntax messed up or the hyperlink to the web site would be displayed
"You may pay online at <a href=" & MyWebAdd & "</a> . Check or money order payments can also be used."
I haven't used HTML very much so this is part of the learning curve for me. Any suggestions or advice to make this work would be greatly appreciated. Thanks for your help. Stay safe....Shaves
You have the syntax wrong. It must be
"You may pay online at " & MyWebAdd & " . Check or money order payments can also be used."

How tell Outlook where image files are?

Please bear with me; there's a long story coming up. It's about programatically creating HTML e-mails from Delphi-7, sending them to Outlook, and not showing the pictures in it, so if you don't know anything about that, don't bother reading it all.
I have an application that sends variable-text E-mails, from a Delphi-7 program, to Outlook. I'm using a TMailInfo item, use a template HTML file for layout, replace placeholders with data from the program (%NAME% becomes "Johnson" etc), create a new Outlook message and populate HTMLbody with my result. All of this works fine.
Recently my client sent me a new sample message, asking me if I could change this E-mail to the new layout as he sent me. So I saved the E-mail as html (from Outlook, generating a huge HTML file full of code that is hardly understandable for a non-HTML guru like me)), replaced the static data with my placeholders, and saved it as the new template.
This worked fine except for one thing - the new layout included some local pictures (not available from a public web resource). When original sample mail from outlook, two things were created - a htm file with the html code, named "Subject" AND a folder called "Subject_files", containing the picture files plus a filelist.xml and an mso file.
The generated HTML refers to the pictures as src="subject_files/image001.jpg"> etc.
When I send the HTML to Outlook, it obviously has no idea where the images are - but how do I tell him?
What I tried:
copying the "subject_files" folder to "My Documents"
hardcoding the links to the pictures in several formats (i.e.
src="C:/test/subject_files/image001.jpg",
src="C://test/subject_files/image001.jpg",
src="C:\test\subject_files\image001.jpg",
and the like
but both don't work... so my question is really, how do I tell Outlook where to look for image files when programatically creating an HTML message?
Thanks in advance!
Once you the email, the recipient does not have access to your computer, So the images won't show in the email. to send images in an email you can:
Use aboslute links
<img src="http://www.example.com/images/header.jpg">
Use embedded images
Convert your images to BASE64, you can find many web sites online to that like
https://www.base64-image.de/ then
<img src="data:image/jpg;base64,/*base64-generated-string*/" />
As mentioned by Remy, the images can be added as regular attachments. You will then need to set the PR_ATTACH_CONTENT_ID property (DASL name http://schemas.microsoft.com/mapi/proptag/0x3712001F) using Attachment.PropertyAccessor.SetProperty. The HTML body must use the matching value for the cid attribute - <img src="cid:xyz">, where "xyz" is the value of the PR_ATTACH_CONTENT_ID property.

XSL to Outlook: Picture is red X

Background:
I am sending HTML emails from Oracle using XSLT.
XML is transformed using XSL and sent to stored procedure from .NET 4. The XSL processor is System.Xml.Xsl.XslCompiledTransform.
My custom Oracle Stored Procedure SEND_MAIL_HTML calls UTL_SMTP
Outlook receives e-mail and the picture is Red X. <-- problem
Using View Source, copy all the content into a file and save.
Double-click on the file and the picture shows up fine.
The img tag in my HTML has a src="data:image/jpeg;base64, LotsOfAlphanumerics" element.
Please ask me how to clarify the problem and I will.
I tried hard to make the cid method work, per the URL's below, but I ended up spending too much time on it.
In the end, I ended up posting the image to a public URL.
See also:
http://www.oracle-base.com/articles/misc/html-with-embedded-images-from-plsql.php
embedding image in html email
Send a base64 image in HTML email

HTML Signature - Embedding a website

I'm trying to use an html email signature that pulls the html from another site. So, imagine I have the html hosted at blahblah.com/blah.html, and blah.html is:
<html>
<body>
Jon Jones
jon#blahblah.com
</body>
</html
And then my html signature would be something like <embed src="blahblah.com/blah.html/> that way I can manipulate the signature without having to constantly change the actual signature in Outlook (which I use to check my email).
I can't figure out any html that will do what I'm trying to do. The embed tag that I posted above doesn't do the trick. What simple line of html can I use to say "display what you find at blahblah.com/blah.html"
I would venture a guess and say this isn't the best way to do this.
From a security standpoint, I wouldn't want to be viewing any email sent by you that also brings in somesite.com/signature.htm. Even if it did, it would invoke a "click to view linked elements in this email" banner, and hide it until I did so (but chances are I'm not clicking).
From a recipient stand point, some spam filters block emails with externally-linked content (your intended recipient may not even get your email, or (best-case) see it with [spam] in the subject line.)
If you want an easy up-keep, you could place the signature in your my documents/some other folder and link to it via outlook's settings, but that about the least intense method (while also not causing concerns or issues to anyone viewing your email.)
It looks like instructions for what you want are here: http://www.emailaddressmanager.com/tips/html-email.html
Under "How to add HTML links in Outlook HTML emails," point to blahblah.com/blah.html
On the other hand, HTML in emails is generally not a great thing because it often isn't very secure (you could send me a page with HTML that would load a virus), so many clients won't be able to recieve it or will flag it as spam.

Outlook HTML Mail - changing linked items to embedded

I'm attempting to send HTML formatted emails using C# 3 via Outlook.MailItem
Outlook.MailItem objMail = (Outlook.MailItem)olkApp.CreateItem(Outlook.OlItemType.olMailItem);
objMail.To = to;
objMail.Subject = subject;
objMail.HTMLBody = htmlBody;
The email is generated externally by saving from an RTF control (TX Text Control), which yields HTML with links to images stored in a <<FileName>>_files subdirectory. Example:
<img border="0" src="file:///C:/Documents%20and%20Settings/ItsMe/Local%20Settings/Temp/2/zbt4dmvs_Images/zbt4dmvs_1.png" width="94" height="94" alt="[image]">
Sending the email this way generates a mail with broken links.
Using Outlook 2007 as the email client with Word as the email editor, switching to RTF (Options tab, Format tab group) preserves the layout and inlines the images.
Programmatically doing this via:
var oldFormat = objMail.BodyFormat;
objMail.BodyFormat = Outlook.OlBodyFormat.olFormatRichText;
objMail.BodyFormat = oldFormat;
loses the formatting and mangles the images (the image becomes a [image] link marker on screen which is clickable but no longer shows the image). This isn't a surprise given that the documentation for MailItem.BodyFormat Property says "All text formatting will be lost when the BodyFormat property is switched from RTF to HTML and vice-versa".
Sadly there doesnt seem to be an easy way to change the Type of each Attachment in the MailItem.Attachements to OlAttachmentType.olByValue, as it's a read-only property that's set when you create the Attachment.
An approach that comes to mind is to walk the HTML, replacing the <img> tags with markers and programatically walking the MailItem text, inserting an Outlook.Attachment of Type OlAttachmentType.olByValue.
Another option is to convert the <img> links to use src="cid:uniqueIdN" and add the images as attachments with the referenced identities.
So, to the question... Is there a way to get the linked images converted to embedded images, ideally without getting into third party tools like Redemption? Converting to RTF happens to yield the outcome, but doing it that way is by no means a pre-requisite, and obviously may lose fidelity - I Just Want It to Just Work :D Neither of my existing ideas sound Clean to me.
Since you are using .net > 2.0, you may want to look into the System.Net.Mail namespace for the creation of mail messages. I have found that its quite customizable and was very easy to use for a task similar to yours. The only problems that I had was making sure I was using the right encoding, and I had to use HTML tables for layouts (css would not work right). Here are some links to show you how this works...
Basic
With multiple views (Plain Text and HTML)
If that's not an option, then I would recommend going the Content ID route and embedding the images as attachments. Your other option is to host the images publicly on a website, and change the image links in the html to the public images.
Something that you should be cognizant about is that HTML emails can easily look like spam and can be treated as such by email servers and clients. Even ones that are just for in-house usage (its happened to me) can end up in Outlook's Junk Mail folder..
DOH!, actually sending the email in Outlook 2007 forces the images to become embedded.
The Sent Item size of 8K is a lot smaller than the draft size of 60K (RTF) I was seeing vs the draft size of 1K (HTML that hadn't been converted to RTF and back again).
So it was Doing What I Mean all the time. Grr.
I'll leave the Q and the A up here in case it helps someone of a similarly confused state of mind.
BTW some useful links I found on my journey:
Sending emails example
General Q&A site with other examples of varying quality