从 Outlook 电子邮件中提取嵌入图像

发布于 2024-07-12 06:31:45 字数 686 浏览 10 评论 0原文

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

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

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

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”。 在电子邮件源中,我看到像这样的图像源链接:

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

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...

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()

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">

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-小熊_ 2024-07-19 06:31:45

OS/Outlook/CDO 版本的差异可能会造成混乱,因此以下是使其在 WinXP/Outlook 2007/CDO 1.21 上运行的步骤:

  • 安装 CDO 1.21
  • 安装 win32com.client
  • 转到 C:\Python25\Lib\site -packages\win32com\client\ 目录运行以下命令:
python makepy.py
  • 从列表中选择“Microsoft CDO 1.21 Library (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,将为您提供了解哪些类、方法、属性和常量可用。

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

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),相同的通用方法也将起作用。

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:

  • install CDO 1.21
  • install win32com.client
  • goto C:\Python25\Lib\site-packages\win32com\client\ directory run the following:
python makepy.py
  • from the list select "Microsoft CDO 1.21 Library (1.21)", click ok
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
  • Examining file 3FA7DEA7-6438-101B-ACC1-00AA00423326x0x1x33.py that's just been generated, will give you an idea of what classes, methods, properties and constants are available.

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()

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文