I am trying to send an email from my office365 corporate account using python. I am new to python. This code previously worked when using my hotmail account, however now that I have a need to send confidential information, I must use my corporate email.
I have tried a couple things.
- Verified that my username and password is correct.
- Used both python2 and python3. Both give the same error:
535 5.7.3 Authentication unsuccessful
- I previously was using mailserver.starttls() when I got the
above error, then after some research, I tried to pass a
certificate.mailserver.starttls(certfile='office365.cer')
I am unclear on the certificate part, but my steps include, looking online to find out how to export a certificate. Using chrome browser, microsoftonline.com has a chain certificate. I can export the root and the level just below the root but not the last level. I dont know how to pass both of these files, so I have simply passed the root certificate. At this point I get the error: ssl.SSLError: [SSL] PEM lib (_ssl.c:3309)
i got stuck at this point. Any help is appreciated. Code used below
import smtplib
mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls(certfile='office365.cer')
mailserver.ehlo()
mailserver.login('[email protected]', 'password')
mailserver.sendmail('[email protected]','[email protected]','python email')
mailserver.quit()