Below is the program which shows how to read the Office 365 Outlook Inbox emails received from specific email-id in python. I have read all the email id from which I have got emails in the inbox folder. Also, I have all the emails text body from received from specific email-id 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
sender = "specifiy any email id here for which you have to read all the respective emails"
# sender = sender.lower()
for message in messages:
if sender:
# This message was send by sender
print(message.body)
Comments
Post a Comment