【问题标题】:Sending Outlook Email with embedded image using VBS使用 VBS 发送带有嵌入图像的 Outlook 电子邮件
【发布时间】:2012-12-08 21:11:47
【问题描述】:

我目前正在使用以下 VBS 脚本发送电子邮件,它工作正常,但是图像作为附件发送。相反,我想将图像嵌入到电子邮件中。我知道我必须在电子邮件的 HTML 正文中引用附件,但我很难做到这一点。

有什么建议吗?

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

【问题讨论】:

    标签: email vbscript outlook html-email outlook-2007


    【解决方案1】:

    使用下面的代码

    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 & "<IMG align=baseline border=0 hspace=0 src=cid:myident>" 'need to match the "myident" above
    newMail.Send
    End Sub
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      我在 Outlook 2003 和 2016 中对此进行了测试

          'I do have a solution, and that is to convert the Body to HTML
      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
      ' The trick is to convert all the message body into HTML
      ' Don't mix script text and HTML. Then simply add an HTML image reference.
      ' Remember if the recipient can't get to the image
      ' it won't appear in the email body, and will be blank. So don't use a local image. 
      ' Use an recipient reachable image.
      MessageBody = "<html>Stats Attached" & "<p>Produced at  " & MyTime & _
            "<p><img src=""http://somedomain.com/.../stats.png""></html><br>"
      'MessageAttachment = "P:\stats.png" ! Now Uneccessary 
      Set ol = CreateObject("Outlook.Application")
      Set ns = ol.getNamespace("MAPI")
      Set newMail = ol.CreateItem(olMailItem)
      newMail.Subject = MessageSubject
      newMail.htmlBody = MessageBody 'Changed the newmMail.Body to newMail.htmlBody
      newMail.RecipIents.Add(ToAddress)
      'newMail.Attachments.Add(MessageAttachment) !This was removed because it would just appear as email attachment
      newMail.Display
      

      【讨论】:

      • TH OP 想要一个嵌入式图像附件。您的代码引用了远程 Web 服务器上的图像。
      猜你喜欢
      • 2012-04-07
      • 2021-10-31
      • 1970-01-01
      • 2011-02-05
      • 2015-07-29
      • 2014-01-25
      • 1970-01-01
      • 2018-03-13
      • 1970-01-01
      相关资源
      最近更新 更多