如何在 Outlook 2010 中将嵌入图像添加到 HTML 邮件中

发布于 2024-11-11 18:06:33 字数 403 浏览 4 评论 0 原文

我有 Office 2003 VBA 代码,它使用此处描述的技术将图像嵌入到使用未记录的 MAPI 属性和 CDO 1.21 的 HTML 消息。

不再支持 CDO 1.21,但根据 MSDN,大多数其功能现已合并到 Outlook 2010 对象模型中。

在哪里可以找到使用 Outlook 2010 对象模型在 Outlook 2010 邮件中嵌入图像的示例?

I have Office 2003 VBA code that uses the technique described here to embed an image in an HTML message using undocumented MAPI properties and CDO 1.21.

CDO 1.21 is no longer supported, but according to MSDN, most of its functionality is now incorporated into the Outlook 2010 object model.

Where can I find a sample that embeds images in an Outlook 2010 message using the Outlook 2010 object model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挖个坑埋了你 2024-11-18 18:06:33

此处找到了答案。

关键是:

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" 

...

Set colAttach = l_Msg.Attachments        
For x = 1 To Items.Count            
    Set l_Attach = colAttach.Add(Items.Item(x))            
    Set oPA = l_Attach.PropertyAccessor            
    oPA.SetProperty PR_ATTACH_MIME_TAG, ItemTypes.Item(x)            
    oPA.SetProperty PR_ATTACH_CONTENT_ID, "item" & x            
    oPA.SetProperty PR_ATTACHMENT_HIDDEN, True        
Next

Found the answer here.

The key bits being:

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" 

...

Set colAttach = l_Msg.Attachments        
For x = 1 To Items.Count            
    Set l_Attach = colAttach.Add(Items.Item(x))            
    Set oPA = l_Attach.PropertyAccessor            
    oPA.SetProperty PR_ATTACH_MIME_TAG, ItemTypes.Item(x)            
    oPA.SetProperty PR_ATTACH_CONTENT_ID, "item" & x            
    oPA.SetProperty PR_ATTACHMENT_HIDDEN, True        
Next
千紇 2024-11-18 18:06:33

这是其他示例:

Option Explicit
'Add reference to MS Outlook x.x Object Library
'Picture to be added as an attachment and modified src location for each embedded picture.
Private Sub Command1_Click()

    Dim oApp As Outlook.Application
    Dim oEmail As MailItem
    Dim colAttach As Outlook.Attachments
    Dim oAttach As Outlook.Attachment

    'create new Outlook MailItem
    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(olMailItem)
    'add graphic as attachment to Outlook message
    'change path to graphic as needed
    Set colAttach = oEmail.Attachments
    Set oAttach = colAttach.Add("D:\my documents\[color=red]MyPic.jpg[/color]")
    oEmail.Close olSave
    'change the src property to 'cid:your picture filename'
    'it will be changed to the correct cid when its sent.
    oEmail.HTMLBody = "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _
    "<IMG alt='' hspace=0 src='[color=red]cid:MyPic.jpg[/color]' align=baseline border=0> </BODY>"
    oEmail.Save
    oEmail.Display 'fill in the To, Subject, and Send. Or program it in.
    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing

End Sub

您可以在以下位置找到它:

http://www.vbforums.com/showthread.php?278600-VB-Embedd-an-image-into-an-Outlook-email-message-body

Here other example:

Option Explicit
'Add reference to MS Outlook x.x Object Library
'Picture to be added as an attachment and modified src location for each embedded picture.
Private Sub Command1_Click()

    Dim oApp As Outlook.Application
    Dim oEmail As MailItem
    Dim colAttach As Outlook.Attachments
    Dim oAttach As Outlook.Attachment

    'create new Outlook MailItem
    Set oApp = CreateObject("Outlook.Application")
    Set oEmail = oApp.CreateItem(olMailItem)
    'add graphic as attachment to Outlook message
    'change path to graphic as needed
    Set colAttach = oEmail.Attachments
    Set oAttach = colAttach.Add("D:\my documents\[color=red]MyPic.jpg[/color]")
    oEmail.Close olSave
    'change the src property to 'cid:your picture filename'
    'it will be changed to the correct cid when its sent.
    oEmail.HTMLBody = "<BODY><FONT face=Arial color=#000080 size=2></FONT>" & _
    "<IMG alt='' hspace=0 src='[color=red]cid:MyPic.jpg[/color]' align=baseline border=0> </BODY>"
    oEmail.Save
    oEmail.Display 'fill in the To, Subject, and Send. Or program it in.
    Set oEmail = Nothing
    Set colAttach = Nothing
    Set oAttach = Nothing
    Set oApp = Nothing

End Sub

you can find it on:

http://www.vbforums.com/showthread.php?278600-VB-Embedd-an-image-into-an-Outlook-email-message-body

撩起发的微风 2024-11-18 18:06:33

这是 Dmitry Streblechenko (MVP) 的 vb 代码片段。这对我来说效果很好。

Set objOutlook = CreateObject("Outlook.Application")
Set Ns = objOutlook.GetNamespace("MAPI")
Ns.Logon
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
Set objOutlookRecip = objOutlookMsg.Recipients.Add("[email protected]")
objOutlookRecip.Type = olTo
objOutlookMsg.Subject = "test"
' add graphic as attachment to Outlook message
Set colAttach = objOutlookMsg.Attachments
Set l_Attach = colAttach.Add("z:\Temp\8\1.jpg ")
l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"  '
Change From 0x370eE001E
l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident"
'Changed from 0x3712001E
objOutlookMsg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True
'
Set body format to HTML
objOutlookMsg.BodyFormat = olFormatHTML
objOutlookMsg.HTMLBody = "html tags goes here< img align="
baseline " order="
1 " hspace="
0 " src="
cid: myident " width="
" 600="
" > </img> end html tags"
objOutlookMsg.Save
objOutlookMsg.Send

This is a vb code snippet by Dmitry Streblechenko (MVP). It worked fine for me.

Set objOutlook = CreateObject("Outlook.Application")
Set Ns = objOutlook.GetNamespace("MAPI")
Ns.Logon
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
Set objOutlookRecip = objOutlookMsg.Recipients.Add("[email protected]")
objOutlookRecip.Type = olTo
objOutlookMsg.Subject = "test"
' add graphic as attachment to Outlook message
Set colAttach = objOutlookMsg.Attachments
Set l_Attach = colAttach.Add("z:\Temp\8\1.jpg ")
l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x370E001F", "image/jpeg"  '
Change From 0x370eE001E
l_Attach.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/proptag/0x3712001F", "myident"
'Changed from 0x3712001E
objOutlookMsg.PropertyAccessor.SetProperty "http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8514000B", True
'
Set body format to HTML
objOutlookMsg.BodyFormat = olFormatHTML
objOutlookMsg.HTMLBody = "html tags goes here< img align="
baseline " order="
1 " hspace="
0 " src="
cid: myident " width="
" 600="
" > </img> end html tags"
objOutlookMsg.Save
objOutlookMsg.Send
舟遥客 2024-11-18 18:06:33

这是我发现最适合我的方法。我曾经只链接到子文件夹,但发现一些国际用户遇到了巨大的延迟,甚至在收件箱中崩溃了。我切换到下面的内容,以便将图像嵌入到电子邮件中,并解决了滞后问题,因为文件实际上嵌入到了电子邮件中。

Sub sendKeyMailer()


Dim emlMsg As Object
    Set emlMsg = CreateObject("CDO.Message")

    strBody = "<html> yadda yadda tricks are for kids. <br><br>"
    strBody = strBody & "<img src=""cid:myimage.jpg""><BR><BR>"
    strBody = strBody & "but tricks make the world go round.</html>"

    emlMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    emlMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<<yourserver>>"
    emlMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    emlMsg.Configuration.Fields.Update
    
    emlMsg.AddRelatedBodyPart "C:\temp\The Big Eskimo Roll.jpg", "myimage.jpg", cdoRefTypeId
    emlMsg.Fields.Item("urn:schemas:mailheader:Content-ID") = "<myimage.jpg>"
    emlMsg.Fields.Update
        
    
    With emlMsg
            .To = "[email protected]"
            .From = "[email protected]"
            .Subject = "Humpty dumpty had a great fall"  '''I have kids....;^D.....
            .HTMLbody = strBody
            '.AddAttachment "C:\temp\The Big Eskimo Roll.jpg"
            .Send
    End With

    Set emlMsg = Nothing

End Sub

Here is the method I've found works the best for me. I used to just link to the subfolder but found some international users experienced huge lag and even crashed there inbox. I switched to the below so that the image was imbedded in the email and it solved the lag issues because the file is actually embedded in the email.

Sub sendKeyMailer()


Dim emlMsg As Object
    Set emlMsg = CreateObject("CDO.Message")

    strBody = "<html> yadda yadda tricks are for kids. <br><br>"
    strBody = strBody & "<img src=""cid:myimage.jpg""><BR><BR>"
    strBody = strBody & "but tricks make the world go round.</html>"

    emlMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    emlMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<<yourserver>>"
    emlMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    emlMsg.Configuration.Fields.Update
    
    emlMsg.AddRelatedBodyPart "C:\temp\The Big Eskimo Roll.jpg", "myimage.jpg", cdoRefTypeId
    emlMsg.Fields.Item("urn:schemas:mailheader:Content-ID") = "<myimage.jpg>"
    emlMsg.Fields.Update
        
    
    With emlMsg
            .To = "[email protected]"
            .From = "[email protected]"
            .Subject = "Humpty dumpty had a great fall"  '''I have kids....;^D.....
            .HTMLbody = strBody
            '.AddAttachment "C:\temp\The Big Eskimo Roll.jpg"
            .Send
    End With

    Set emlMsg = Nothing

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文