Sending Outlook Email with embedded image using VBS

匿名 (未验证) 提交于 2019-12-03 01:05:01

问题:

I am currently using the following VBS script to send an email and it works fine, however the image is sent as an attachment. I would instead like to embed the image into the email. I understand that I must reference the attachment in the HTML body of the email but I am struggling to do this.

Any suggestions?

Dim ToAddress Dim FromAddress Dim MessageSubject Dim MyTime Dim MessageBody Dim MessageAttachment Dim ol, ns, newMail MyTime = Now  ToAddress = "email@address.com" MessageSubject = "Auto Stats " & MyTime MessageBody = "Stats Attached" & vbCrLf & "Produced at " & MyTime MessageAttachment = "P:\stats.png" Set ol = WScript.CreateObject("Outlook.Application") Set ns = ol.getNamespace("MAPI") Set newMail = ol.CreateItem(olMailItem) newMail.Subject = MessageSubject newMail.Body = MessageBody newMail.RecipIents.Add(ToAddress) newMail.Attachments.Add(MessageAttachment) newMail.Send 

回答1:

use the code below

Const PR_ATTACH_MIME_TAG = "http://schemas.microsoft.com/mapi/proptag/0x370E001E" Const PR_ATTACH_CONTENT_ID = "http://schemas.microsoft.com/mapi/proptag/0x3712001E" Const PR_ATTACHMENT_HIDDEN = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"   Sub testing2() Dim ToAddress Dim FromAddress Dim MessageSubject Dim MyTime Dim MessageBody Dim MessageAttachment Dim ol, ns, newMail Dim realAttachment MyTime = Now  ToAddress = "testing@address.com" MessageSubject = "Auto Stats " & MyTime MessageBody = "Stats Attached" & vbCrLf & "Produced at " & MyTime MessageAttachment = "C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg" Set ns = Outlook.GetNamespace("MAPI") Set newMail = Outlook.CreateItem(olMailItem) newMail.Subject = MessageSubject newMail.Body = MessageBody newMail.Recipients.Add (ToAddress) Set realAttachment = newMail.Attachments.Add(MessageAttachment) Set oPA = realAttachment.PropertyAccessor oPA.SetProperty PR_ATTACH_MIME_TAG, "image/jpeg" oPA.SetProperty PR_ATTACH_CONTENT_ID, "myident" 'change myident for another other image newMail.HTMLBody = newMail.HTMLBody & "" 'need to match the "myident" above newMail.Send End Sub 

Hope it helps



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!