Send Email via Outlook and SMTP

Microsoft's email service, Outlook (formerly Hotmail), allows application developers to send emails through the SMTP protocol. This is what you need to know about Outlook's SMTP server:

  • Domain: smtp-mail.outlook.com

  • Port: 587

  • Security protocol: TLS

With this information and the help of the standard email and smtplib modules, we can use the following code to send an email from an Outlook account (it also accepts the old @hotmail.com, @live.com, etc.):

from email.message import EmailMessage
import smtplib
sender = "sender@outlook.com"
recipient = "recipient@example.com"
message = "Hello world!"
email = EmailMessage()
email["From"] = sender
email["To"] = recipient
email["Subject"] = "Test Email"
email.set_content(message)
smtp = smtplib.SMTP("smtp-mail.outlook.com", port=587)
smtp.starttls()
smtp.login(sender, "my_outlook_password_123")
smtp.sendmail(sender, recipient, email.as_string())
smtp.quit()

You'll need to replace "sender@outlook.com" and "my_outlook_password_123" with the credentials of the Outlook account which you want to send the email from.

For a general explanation of the structure of this code and how to include HTML code and/or file attachments, see our post on the email and smtplib modules: Send HTML Email With Attachments via SMTP.