Why does an image embed successfully in an email with .Display but not .Send?

时间:2015-10-06 08:34:12

标签: excel vba email outlook ms-office

This is something that I'm finding very weird and hard to solve, I have the following code:

Sub EmailImage()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment

Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\Users\User1\Documents\thumbs-up.jpg")
oEmail.Close olSave
oEmail.To = "abc@abc123.com"
oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:thumbs-up.jpg' align=baseline border=0>&nbsp;</BODY>"
oEmail.Display
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub

With the .Display code the image embeds successfully and once the image displays and you click send the reciever can see the image in the email, as below:

enter image description here

But the problem is I have a lot of emails to send, so it would be more appropriate to use .Send rather than .Display, once I do this however the image gets attached to the email but cannot be seen in it.

Here's the changed code:

Sub EmailImage()
Dim oApp As Outlook.Application
Dim oEmail As MailItem
Dim colAttach As Outlook.Attachments
Dim oAttach As Outlook.Attachment

Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\Users\User1\Documents\thumbs-up.jpg")
oEmail.Close olSave
oEmail.To = "abc@abc123.com"
oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:thumbs-up.jpg' align=baseline border=0>&nbsp;</BODY>"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub

And an image of what can be seen in the email:

enter image description here

I have no idea why this is happening, can someone please help?

Please note, I am using Windows 7, Excel 2010, and Outlook 2010.

2 个答案:

答案 0 :(得分:1)

这对我有用:

Sub EmailImage()
Dim oApp As Object
Dim oEmail As Object

Set oApp = CreateObject("Outlook.Application")
Set oEmail = oApp.CreateItem(olMailItem)
Set colAttach = oEmail.Attachments
Set oAttach = colAttach.Add("C:\Users\User1\Documents\thumbs-up.jpg")
oEmail.Close olSave
oEmail.To = "abc@abc123.com"
oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:thumbs-up.jpg' align=baseline border=0>&nbsp;</BODY>"
oEmail.Send
Set oEmail = Nothing
Set colAttach = Nothing
Set oAttach = Nothing
Set oApp = Nothing
End Sub

答案 1 :(得分:1)

您需要将附件上的PR_ATTACH_CONTENT_ID属性(DASL名称http://schemas.microsoft.com/mapi/proptag/0x3712001F)设置为与HTML正文中cid标记的img属性匹配的值。

Set oAttach = colAttach.Add("C:\Users\User1\Documents\thumbs-up.jpg")
oAttach.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyCid"
...
oEmail.HTMLBody = "<IMG alt='' hspace=0 src='cid:MyCid' align=baseline border=0>&nbsp;</BODY>"