通过 Python 将图像添加到 Outlook MailItem
Adding an image to an Outlook MailItem via Python
我的目标是使用 Python.
将图像(从本地 .png 文件)添加到 Outlook MailItem
我已经在 SO 上阅读了类似问题的答案(主要使用 VBA),但我遇到的问题是邮件在发送之前(以及当我发送时)看起来很好(我可以看到图像)查看我的已发送邮件文件夹),但是当另一端收到时,图像已损坏。
如果我在邮件中手动插入图像(使用 Outlook 菜单中的插入/图片/来自此设备),电子邮件会正常到达,并显示图像。
这是我正在使用的基本代码(主要基于 VBA 个示例):
import win32com.client as wc
def main():
outlook = wc.Dispatch("Outlook.Application")
#Create a new mail item
msg = outlook.CreateItem(0)
#Set some properties
msg.BodyFormat = 2
msg.Subject = 'Here is my chart'
msg.Recipients.Add('xxx@yyy.com')
#Add an attachment, with position 0
imageFile = 'C:\Temp\myplot.png'
ats = msg.Attachments
att = ats.Add(imageFile,1,0)
#Set the HTMLBody
msg.HTMLBody = '<img src="cid:{0:}" width=200 height=200>'.format(att.FileName)
#Send
msg.Send()
main()
这是我在“已发送邮件”文件夹中看到的内容:
我发送的项目(使用查看源代码)中的 HTML 是这样的:
<img src="cid:myplot.png" width=200 height=200>
但结果是这样的:
目的地正文中的HTML是这样的:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<img src="cid:AD1B7014D243624CA83F873A11053F18@GBRP123.PROD.OUTLOOK.COM" width="200" height="200">
</body>
</html>
当我手动插入图片时,接收到的HTML要长得多(所以我不会在这里全部包含),但是'img'标签在这里(这符合在“已发送邮件”文件夹中):
<img width="614" height="461" style="width:6.3958in;height:4.802in" id="Picture_x0020_1" src="cid:image001.png@01D736CD.72206440" alt="Chart, line chart Description automatically generated">
如果对此有任何见解,我将不胜感激! (遗憾的是 Outlook 不允许我录制宏以查看手动过程在做什么)。我见过一个示例(来自 C#),其中在 MailItem 上使用 SetProperty() 方法来添加不在 Outlook 对象模型(架构、上下文 ID)中的其他项目,以及新创建的 Guid,但这真的是有必要吗?
使用 Microsoft 365,Outlook 版本 2105,Beta 频道
创建附件并使用 Attachment.PropertyAccessor.SetProperty
.
设置 PR_ATTACH_CONTENT_ID
属性(DASL 名称 "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
)
您的 HTML 正文 (MailItem.HTMLBody
属性) 然后需要通过 cid 引用该图像附件:
img src="cid:xyz"
其中 xyz 是 PR_ATTACH_CONTENT_ID
属性 的值。
查看带有 OutlookSpy 的现有消息(我是其作者)- 单击 IMessage 按钮。
attachment = mailitem.Attachments.Add("c:\temp\MyPicture.jpg");
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1");
mailitem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>";
我的目标是使用 Python.
将图像(从本地 .png 文件)添加到 Outlook MailItem我已经在 SO 上阅读了类似问题的答案(主要使用 VBA),但我遇到的问题是邮件在发送之前(以及当我发送时)看起来很好(我可以看到图像)查看我的已发送邮件文件夹),但是当另一端收到时,图像已损坏。
如果我在邮件中手动插入图像(使用 Outlook 菜单中的插入/图片/来自此设备),电子邮件会正常到达,并显示图像。
这是我正在使用的基本代码(主要基于 VBA 个示例):
import win32com.client as wc
def main():
outlook = wc.Dispatch("Outlook.Application")
#Create a new mail item
msg = outlook.CreateItem(0)
#Set some properties
msg.BodyFormat = 2
msg.Subject = 'Here is my chart'
msg.Recipients.Add('xxx@yyy.com')
#Add an attachment, with position 0
imageFile = 'C:\Temp\myplot.png'
ats = msg.Attachments
att = ats.Add(imageFile,1,0)
#Set the HTMLBody
msg.HTMLBody = '<img src="cid:{0:}" width=200 height=200>'.format(att.FileName)
#Send
msg.Send()
main()
这是我在“已发送邮件”文件夹中看到的内容:
我发送的项目(使用查看源代码)中的 HTML 是这样的:
<img src="cid:myplot.png" width=200 height=200>
但结果是这样的:
目的地正文中的HTML是这样的:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
</head>
<body>
<img src="cid:AD1B7014D243624CA83F873A11053F18@GBRP123.PROD.OUTLOOK.COM" width="200" height="200">
</body>
</html>
当我手动插入图片时,接收到的HTML要长得多(所以我不会在这里全部包含),但是'img'标签在这里(这符合在“已发送邮件”文件夹中):
<img width="614" height="461" style="width:6.3958in;height:4.802in" id="Picture_x0020_1" src="cid:image001.png@01D736CD.72206440" alt="Chart, line chart Description automatically generated">
如果对此有任何见解,我将不胜感激! (遗憾的是 Outlook 不允许我录制宏以查看手动过程在做什么)。我见过一个示例(来自 C#),其中在 MailItem 上使用 SetProperty() 方法来添加不在 Outlook 对象模型(架构、上下文 ID)中的其他项目,以及新创建的 Guid,但这真的是有必要吗?
使用 Microsoft 365,Outlook 版本 2105,Beta 频道
创建附件并使用 Attachment.PropertyAccessor.SetProperty
.
PR_ATTACH_CONTENT_ID
属性(DASL 名称 "http://schemas.microsoft.com/mapi/proptag/0x3712001F"
)
您的 HTML 正文 (MailItem.HTMLBody
属性) 然后需要通过 cid 引用该图像附件:
img src="cid:xyz"
其中 xyz 是 PR_ATTACH_CONTENT_ID
属性 的值。
查看带有 OutlookSpy 的现有消息(我是其作者)- 单击 IMessage 按钮。
attachment = mailitem.Attachments.Add("c:\temp\MyPicture.jpg");
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1");
mailitem.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>";