我无法使用python在电子邮件正文中添加图片,我可以添加图片作为附件,但我想要代码在邮件正文中添加图片 [英] I am not able to add an image in email body using python , I am able to add a picture as a attachment but i want a code to add image in mailbody
问题描述
Right now I am using below code:
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'to address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>'# this field is optional
**mail.Attachments.Add('C:\Users\MA299445\Downloads\screenshot.png')**
mail.Send()
我可以附加图片,但是我想将此图片粘贴到电子邮件正文中.
I am able to attach a picture but I want to paste this picture in e-mail body.
预先感谢
推荐答案
创建附件并使用Attachment.PropertyAccessor
设置PR_ATTACH_CONTENT_ID属性(DASL名称"http://schemas.microsoft.com/mapi/proptag/0x3712001F
).
Create an attachment and set the PR_ATTACH_CONTENT_ID property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x3712001F
") using Attachment.PropertyAccessor
.
您的HTML正文(MailItem.HTMLBody
属性)将需要通过cid引用该图像附件:
Your HTML body (MailItem.HTMLBody
property) would then need to reference that image attachment through the cid:
img src="cid:xyz"
其中xyz是PR_ATTACH_CONTENT_ID
属性的值.
where xyz is the value of the PR_ATTACH_CONTENT_ID
property.
使用 OutlookSpy 查看现有邮件(单击IMessage按钮).
Look at an existing message with OutlookSpy (click IMessage button).
attachment = mail.Attachments.Add("C:\Users\MA299445\Downloads\screenshot.png")
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001F", "MyId1")
mail.HTMLBody = "<html><body>Test image <img src=""cid:MyId1""></body></html>"
这篇关于我无法使用python在电子邮件正文中添加图片,我可以添加图片作为附件,但我想要代码在邮件正文中添加图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!