Search code examples
python-2.7smtpsmtplib

send SMTP email for office365 with python using tls/ssl


i'm trying to create a python script that sends emails with some messages. my setup is based on a smtp.office365.com. i created some script to do the job.

#!/usr/bin/python

print ("HI! let's start")  
import smtplib  
import getpass

port=587                                                                                                                    
smtp_server="smtp.office365.com"  
sender_email='mymail@something.com'  
receiver_email='mymail@something.com'  
print ("defining server")  
server = smtplib.SMTP(smtp_server,port)

password=getpass.getpass("enter password")

message='''
Hi,
THIS IS A TEST MESSAGE.
'''

print ("logging in")  
server.login(sender_email,password)  
print ("sending mail now")  
server_sendmail(sender_email,receiver_email,message)  
print ("all must be done by now")

But due to some reasons, it gets stuck after printing "defining server". and never reaches to asking for password. apparently the smtplib.SMTP command hangs. can you tell why this happens and how to get rid of it? my setup is as following :

Server: smtp.office365.com
/sjmail.com
security : STARTTLS
Port : 587

Solution

  • Vishesh Arya!

    SMTP AUTH extension not supported by Office 365 account. Search for Microsoft Graph to do this.