Below is the program which shows how to read the Office 365 Outlook Inbox emails in python. I have read all the email id from which I have got emails in the inbox folder. Also, I have read the last/latest email body in python.
For this you need to install the pywin32 library in your program using following command.
pip install pywin32
The program is:
from win32com.client import Dispatch
import win32com
outlook = Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder("6")
all_inbox = inbox.Items
folders = inbox.Folders
for msg in all_inbox:
if msg.Class == 43:
if msg.SenderEmailType == 'EX':
print(msg.Sender.GetExchangeUser().PrimarySmtpAddress)
else:
print(msg.SenderEmailAddress)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
messages = inbox.Items
message = messages.Getlast
body_content = message.body
print(body_content)
Comments
Post a Comment