Send email python outlook 365

Here is an example of how to send an email using Python with Outlook 365:

Prerequisites:

Code:

import win32com.client

# Set up the Outlook application
outlook = win32com.client.Dispatch('Outlook.Application')

# Set up the email message
email = outlook.CreateItem(0)  # 0 = MailItem

# Set the sender and recipient
email.From = '[email protected]'
email.To = '[email protected]'

# Set the subject and body
email.Subject = 'Test email from Python'
email.Body = 'This is a test email sent from Python using Outlook 365'

# Add attachments (optional)
# email.Attachments.Add('path/to/attachment1.pdf')
# email.Attachments.Add('path/to/attachment2.docx')

# Send the email
email.Send()

# Clean up
outlook.Quit()

Note:

Security considerations: