Reputation: 4636
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:
Could someone;
1. Give a link to the location of clear documentation (if it exists)
2. Expand on the below
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.
Upvotes: 79
Views: 102509
Reputation: 594
How to use Microsoft Documents to Guide win32com
Code:
1. Start with the Namespace Interface:
Start by importing win32com
and creating an outlook
object, which represents the ‘Outlook Namespace Interface’(the link has all properties and methods available to call). This object serves as your starting point for accessing everything in the Outlook app:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
2. Navigate the Documentation:
Navigation of the documentation is best done by starting at the ‘Outlook Namespace Interface’ and click on hyperlinks for the method or properties you want to call to see return types.
Alternatively:
You can check the 'Class' property, which returns an integer. Refer to this table to determine the associated Interface. Then search for pages labeled '____ Interface,' where ____ represents the object you are interested in.
Example Navigating Email Accounts, Folders, and Emails:
level 1 is email accounts
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
folders = outlook.Folders
for i, folder in enumerate(folders):
print(i, folder.Name)
# 0 [email protected]
# 1 [email protected]
# 2 [email protected]
level 2 is first level of folders
account = folders[1] # use index to select account; in this case [email protected]
account_folders = account.Folders
for i, folder in enumerate(account_folders):
print(i, folder.Name)
# 0 Deleted Items
# 1 Inbox
# 2 Outbox
# 3 Sent Items
# 4 Notes
# 5 Journal
# … continues on
If you have folders inside folders you can continue this pattern.
Accessing emails:
inbox_folder = account_folders[1] # select inbox
print("number of email", inbox_folder.Items.Count) # number of email 231
for i, email in enumerate(inbox_folder.Items):
print(i, email.Subject)
# 0 Meeting Time
# 1 Meeting follow-up
# 2 Scheduling another meeting
# 3 Can't make it to today's meeting
# … continues on
Accessing emails properties:
email = inbox_folder.Items[3]
print(email.SenderName) # Jane Smith
print(email.SentOn) # 2023-08-31 18:05:23+00:00
print(email.Body) # Hi John, I can't make today's meeting because ...
Complete list of property for an email.
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 411
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
Upvotes: 3
Reputation: 529
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.
Upvotes: 38
Reputation: 71
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.
Upvotes: 7
Reputation: 1236
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.
Upvotes: 38