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

我试图通过 win32com 更好地理解 Outlook 交互。我一直无法找到清晰的文档,使我能够利用 win32com 有效地阅读电子邮件,从我目前的调查看,这似乎是一个相当规律的情绪的用户。因此,提出了以下信息和要求:

会不会有人

给出一个清晰文档位置的链接(如果有的话)

2. 在下面展开

下面是我在阅读其他人代码的基础上发现的当前功能。

以下面的代码为例:

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

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

收件箱-

.Folders
.Items

讯息-

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

讯息-

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

附件-

.item()
.Count

附件-

.filename

如果您知道任何更多的功能,然后请添加到您的答案。

86633 次浏览

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

Interop Outlook Mailitem Properties

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

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.

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

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 Folders to go deeper:

folder.Folders[2]

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

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

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

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 solution to Folders):

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.

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

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

This page is definitely the most complete resource for pywin32!

Just one to add on:

 message.senton.date() # for received date only
message.senton.time() # for received time only
message.senton # for date and time

Also, just found out the case of the message properties like senton, attachment and all other properties shown in 'Interop Outlook Mailitem Properties' shared by Genome above can be write as fully lowercase or fully uppercase or mix.