从 Outlook 电子邮件中提取嵌入的图像 [英] Extracting Embedded Images From Outlook Email

查看:43
本文介绍了从 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
bar');
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?

推荐答案

OS/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:

  • 安装 CDO 1.21
  • 安装 win32com.client
  • 转到 C:Python25Libsite-packageswin32comclient 目录,运行以下命令:
python makepy.py

  • 从列表中选择Microsoft CDO 1.21 Library (1.21)",点击确定
  • C:Python25Libsite-packageswin32comclient>python makepy.py
    Generating to C:Python25libsite-packageswin32comgen_py3FA7DEA7-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 电子邮件中提取嵌入的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆