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> </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:
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> </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:
I have no idea why this is happening, can someone please help?
Please note, I am using Windows 7, Excel 2010, and Outlook 2010.
答案 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> </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> </BODY>"