Python: Send Email via Office 365

Please refer to the following Python Script to send Email via Office 365 with TLS 

Encrypted your password

import base64
print base64.b64encode("XXXXXX") #Your Password here
print base64.b64decode("UEBzc3cwcmR3dms0ODk3")

This is the actual script that will send email via Office 365 to my Gmail

import smtplib
import base64

sendto = 'kwyong510@gmail.com'
user= 'kwyong@aventistech.com'
password = base64.b64decode("UEBzc3cwcmR3dms0ODk3")
smtpsrv = "smtp.office365.com"
smtpserver = smtplib.SMTP(smtpsrv,587)

smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(user, password)
header = 'To:' + sendto + 'n' + 'From: ' + user + 'n' + 'Subject:testing n'
print header
msgbody = header + 'n This is a test Email send using Python nn'
smtpserver.sendmail(user, sendto, msgbody)
print 'done!'
smtpserver.close()

Leave a Comment