本文介绍了从Outlook电子邮件中提取嵌入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft的CDO(协作数据对象)以编程方式从Outlook邮箱读取邮件并保存嵌入式图像附件。我正在尝试使用Win32扩展从Python中执行此操作,但使用CDO的任何语言的示例都将是有帮助的。

I am using Microsoft's CDO (Collaboration Data Objects) to programmatically read mail from an Outlook mailbox and save embedded image attachments. I'm trying to do this from Python using the Win32 extensions, but samples in any language that uses CDO would be helpful.

到目前为止,我在这里...

So far, I am here...

以下Python代码将读取我邮箱中的最后一封电子邮件,打印附件的名称,并打印邮件正文:

The following Python code will read the last email in my mailbox, print the names of the attachments, and print the message body:

from win32com.client import Dispatch

session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\nbar');
inbox = session.Inbox
message = inbox.Messages.Item(inbox.Messages.Count)

for attachment in message.Attachments:
    print attachment

print message.Text

session.Logoff()

但是,附件名称如:zesjvqeqcb_chart_0。在电子邮件来源中,我看到像这样的图像源链接:
< IMG src =cid:zesjvqeqcb_chart_0>

However, the attachment names are things like: "zesjvqeqcb_chart_0". Inside the email source, I see image source links like this:<IMG src="cid:zesjvqeqcb_chart_0">

那么,是否可以使用此CID URL(或其他任何东西)提取实际图像并将其保存在本地?

So, is it possible to use this CID URL (or anything else) to extract the actual image and save it locally?

推荐答案

操作系统/ Outlook / CDO可能是混乱的根源,所以这里是使其在WinXP / Outlook 2007 / CDO 1.21上运行的步骤:

Difference in versions of OS/Outlook/CDO is what might be the source of confusion, so here are the steps to get it working on WinXP/Outlook 2007/CDO 1.21:


  • 安装

  • 安装win32com.client

  • goto C:\Python25\Lib\site-packages\win32com\client\\ \\目录运行以下内容:

python makepy.py




  • 从列表中选择Microsoft CDO 1.21库(1.21),单击确定

  • C:\Python25\Lib\site-packages\win32com\client>python makepy.py
    Generating to C:\Python25\lib\site-packages\win32com\gen_py\3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.py
    Building definitions from type library...
    Generating...
    Importing module




    • 检查刚刚生成的文件3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.py,可以了解可用的类,方法,属性和常量。

    • 现在我们完成了无聊的步骤,这里是有趣的部分:

      Now that we are done with the boring steps, here is the fun part:

      import win32com.client
      from win32com.client import Dispatch
      
      session = Dispatch('MAPI.session')
      session.Logon ('Outlook') # this is profile name
      inbox = session.Inbox
      messages = session.Inbox.Messages
      message = inbox.Messages.GetFirst()
      
      if(message):
          attachments = message.Attachments
          for i in range(attachments.Count):
              attachment = attachments.Item(i + 1) # yep, indexes are 1 based
      
              filename = "c:\\tmpfile" + str(i)
              attachment.WriteToFile(FileName=filename)
      session.Logoff()
      

      如果您使用较旧版本的CDO(win2k的CDO),则相同的一般方法也将起作用。

      Same general approach will also work if you have older version of CDO (CDO for win2k)

      这篇关于从Outlook电子邮件中提取嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 15:51