3

如下图,目前我已经实现了使用 Outlook 2010 和 python 发送 HTML 格式的邮件

def send_mail_via_com(self):
  o = win32com.client.Dispatch("Outlook.Application")
  Msg = o.CreateItem(0)
  Msg.To = self.myEmailAddress 
  Msg.BCC = self.myEmailAddress

  Msg.Subject = 'This is Subject'
  Msg.HTMLBody  = htmlEmailContent
  Msg.Send()

展望未来,我想在 HTML 格式的电子邮件中显示一些嵌入图像,我检查了 HTML 源代码,如下所示:

 <img border=0 width=117 height=20 id="Picture_x0020_2" src="cid:image001.png@01CE9386.AB9B5E70" alt="Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: Description: cid:786FB7C2-A5F6-462A-B0FD-EA865CD02221@xxx.example.com">

我尝试附加图像文件(使用上述 html 代码中引用的相同名称作为附件),但它只是附件,不会作为嵌入图像显示在文本正文中。我们的邮件服务器不支持 SMTP 协议,所以我不能Sending Multipart html emails which contains embedded images中那样使用 smtplib 。有人可以帮帮我吗?提前致谢!

    attachment1 = "G:\\image001.png"
    Msg.Attachments.Add(attachment1)

2013/8/8 更新,我通过http://social.msdn.microsoft.com/Forums/vstudio/en-US/6c063b27-7e8a-4963-ad5f-ce7e5ffb2c64/how-to-embed-获得了 C# 版本代码image-in-html-body-in-c-into-outlook-mail,谁能给我看一下这些的 Python 对应代码:

   Attachment attachment = newMail.Attachments.Add(
                      @"E:\Pictures\image001.jpg"
                      , OlAttachmentType.olEmbeddeditem
                      , null
                      , "Some image display name"
                     );

   string imageCid = "image001.jpg@123";

   attachment.PropertyAccessor.SetProperty(
                     "http://schemas.microsoft.com/mapi/proptag/0x3712001E"
                     , imageCid
                     );

    newMail.HTMLBody = String.Format(
                      "<body><img src=\"cid:{0}\"></body>"
                     , imageCid
                     );
4

3 回答 3

2
import win32com.client

o = win32com.client.Dispatch("Outlook.Application")
newMail = o.CreateItem(0)

attachment = newMail.Attachments.Add("E:\Pictures\image001.jpg", win32com.client.constants.olEmbeddeditem, 0, "Some image display name")
imageCid = "image001.jpg@123"
attachment.PropertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E", imageCid)
newMail.HTMLBody = "<body><img src=\"cid:{0}\"></body>".format(imageCid)

msg.Send()

https://msdn.microsoft.com/en-us/library/office/ff869553(v=office.15).aspx

于 2015-01-22T17:32:23.937 回答
1

在 python 中,您必须在属性 PR_ATTACH_CONTENT_ID_W 之前定义,因为它没有在 pywin32 mapitags (win32com.mapi.mapitags) 中定义

from win32com.mapi.mapitags import PROP_TAG, PT_UNICODE 

# defines the property PR_ATTACH_CONTENT_ID_W  not defined  in    pywin32 mapitags.
# number of PR_ATTACH_CONTENT_ID_W Property is in MSDN Library
# https://msdn.microsoft.com/en-us/library/cc765868.aspx
# all mapi tags are defined in https://msdn.microsoft.com/en-us/library/cc815492%28v=office.15%29.aspx
mapitags.PR_ATTACH_CONTENT_ID_W = PROP_TAG(PT_UNICODE, 0x3712)

# now you can use the property PR_ATTACH_CONTENT_ID_W like Dmitry explained you.
于 2015-02-06T18:01:58.813 回答
-1

获取一个 HTML 页面,其中包含您的文本内容和 CSS

抓取您想要发送的嵌入在您的电子邮件中的图像

使用 Base64 编码器将二进制照片转换为巨大的文本字符串(例如:http ://www.greywyvern.com/code/php/binary2base64 )

用该字符串替换您的普通图像源

保存文件并正常发送

于 2013-08-07T11:01:11.480 回答