使用 python win32com Outlook 清楚地记录了电子邮件功能的阅读

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22813814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:45:25  来源:igfitidea点击:

Clearly documented reading of emails functionality with python win32com outlook

pythonemailpython-3.xoutlook

提问by Phoenix

I'm trying to understand outlook interaction through win32com better. I've been unable to find clear documentation that allows me to utilise win32com to read emails effectively, from my current investigation it seems like a fairly regular sentiment by users. Thus comes the following information and request:

我正在尝试通过 win32com 更好地了解 Outlook 交互。我一直无法找到明确的文件,让我有效利用win32com阅读电子邮件,从我目前的调查,好像用户一个相当常规的情绪。因此出现以下信息和请求:

Could someone;

有人可以吗?

1. Give a link to the location of clear documentation (if it exists)

1. 提供指向清晰文档位置的链接(如果存在)

2. Expand on the below

2.在下面展开

Below is the current functionality I've found based on reading other peoples code.

以下是我根据阅读其他人的代码发现的当前功能。

Take the below code:

取下面的代码:

import win32com

outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox=outlook.GetDefaultFolder(6)

messages=inbox.Items

for message in messages:
    attachments = message.attachments

    for attachment in attachments:
        pass

The objects used above have the following functionality that I'm aware of:

上面使用的对象具有以下我所知道的功能:

inbox -

收件箱——

.Folders
.Items

messages -

留言——

.GetFirst()
.GetLast()
.GetNext()
.GetPrevious()
.Attachments

message -

信息 -

.Subject
.Body
.To
.Recipients
.Sender
.Sender.Address

attachments -

附件——

.item()
.Count

attachment -

依恋 -

.filename

If you know of any more functionality then please add to this in your answers.

如果您知道更多功能,请在您的答案中添加。

采纳答案by Genome

The visual basic for applications reference is your friend here. Try starting with this link...

应用程序参考的视觉基础是您的朋友。尝试从这个链接开始...

Interop Outlook Mailitem Properties

互操作 Outlook 邮件项属性

For instance I can see that message will probably have additional properties than what you listed above. For example.

例如,我可以看到该消息可能具有比您上面列出的其他属性。例如。

  • message.CC
  • message.Importance
  • message.LastModificationTime
  • 消息抄送
  • 消息.重要性
  • message.LastModificationTime

回答by Dmitry Streblechenko

You can see all live Outlook objects and their data in OutlookSpy.

您可以在OutlookSpy 中查看所有实时 Outlook 对象及其数据。

MailItemobject properties, methods and events are fully documented at https://msdn.microsoft.com/en-us/library/office/ff861332.aspx

MailItem对象属性、方法和事件在https://msdn.microsoft.com/en-us/library/office/ff861332.aspx 中有完整记录

回答by John Cook

I thought I'd add something on navigating through folders too - this is all derived from the Microsoft documentation above, but might be helpful to have here, particularly if you're trying to go anywhere in the Outlook folder structure except the inbox.

我想我也会添加一些关于文件夹导航的内容 - 这一切都来自上面的 Microsoft 文档,但在这里可能会有所帮助,特别是如果您尝试访问 Outlook 文件夹结构中除收件箱之外的任何位置。

You can navigate through the folders collection using folders- note in this case, there's no GetDefaultFolderafter the GetNamespace(otherwise you'll likely end up with the inbox).

您可以使用folders-浏览文件夹集合,注意在这种情况下,没有GetDefaultFolder后面GetNamespace(否则您最终可能会看到收件箱)。

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace('MAPI')
folder = outlook.Folders[1]

The number is the index of the folder you want to access. To find out how many sub-folders are in there:

该数字是您要访问的文件夹的索引。要了解其中有多少个子文件夹:

folder.Count

If there more sub-folders you can use another Foldersto go deeper:

如果有更多子文件夹,您可以使用另一个子文件夹Folders更深入:

folder.Folders[2]

Foldersreturns a list of sub-folders, so to get the names of all the folders in the current directory, you can use a quick loop.

Folders返回子文件夹列表,因此要获取当前目录中所有文件夹的名称,可以使用快速循环。

for i in range(folder.Count):
    print (folder[i].Name)

Each of the sub-folders has a .Itemsmethod to get a list of the emails.

每个子文件夹都有一个.Items方法来获取电子邮件列表。

回答by pajonk

For everyone wondering how to reach any default folder not just "Inbox" here's the list:

对于想知道如何访问任何默认文件夹而不仅仅是“收件箱”的每个人,这里是列表:

3  Deleted Items
4  Outbox
5  Sent Items
6  Inbox
9  Calendar
10 Contacts
11 Journal
12 Notes
13 Tasks
14 Drafts

There are more (Reminders, Sync errors etc.); you can get whole list with this code (inspired by John Cook's solutionto Folders):

还有更多(提醒、同步错误等);您可以使用此代码获取整个列表(受John Cook 的文件夹解决方案启发):

import win32com
outlook=win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
for i in range(50):
    try:
        box = outlook.GetDefaultFolder(i)
        name = box.Name
        print(i, name)
    except:
        pass

I'm not pasting the whole list here, because mine is in Polish and wouldn't be really helpful.

我不会在这里粘贴整个列表,因为我的列表是波兰语,并不会真正有帮助。

回答by Mika72

For attachments https://docs.microsoft.com/en-us/office/vba/api/outlook.attachment(see Properities)

附件 https://docs.microsoft.com/en-us/office/vba/api/outlook.attachment(见属性)

attachment.FileName
attachment.Type
attachment.Position
attachment.BlockLevel
attachment.Class
attachment.DisplayName
attachment.Parent
attachment.Session
attachment.Size
attachment.Index
attachment.Application