如何使用 tls/ssl 使用 python 为 office365 发送 SMTP 电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46160886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:31:02  来源:igfitidea点击:

How to send SMTP email for office365 with python using tls/ssl

pythonemailssloffice365

提问by TKerr

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.

我正在尝试使用 python 从我的 office365 公司帐户发送电子邮件。我是python的新手。此代码以前在使用我的 hotmail 帐户时有效,但是现在我需要发送机密信息,我必须使用我的公司电子邮件。

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')
  • 验证我的用户名和密码是否正确。
  • 使用了python2和python3。两者都给出相同的错误:535 5.7.3 Authentication unsuccessful
  • 我之前在使用mailserver.starttls()时出现上述错误,然后经过一番研究,我尝试通过
    证书。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.comhas 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)

我不清楚证书部分,但我的步骤包括,在线查找以了解如何导出证书。使用 chrome 浏览器,microsoftonline.com具有链证书。我可以导出根和根正下方的级别,但不能导出最后一级。这两个文件我都不知道怎么通过,所以就简单的通过了根证书。此时我收到错误: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()

回答by Gal Silberman

Well, you are almost there. The following code will do the trick:

嗯,你快到了。以下代码可以解决问题:

import smtplib

mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls()
mailserver.login('[email protected]', 'password')
mailserver.sendmail('[email protected]','[email protected]','python email')
mailserver.quit()

Use the following links for more information:

使用以下链接了解更多信息:

http://www.aventistech.com/2016/03/07/python-send-email-via-office-365-tls/

http://www.aventistech.com/2016/03/07/python-send-email-via-office-365-tls/

https://docs.python.org/3/library/smtplib.html

https://docs.python.org/3/library/smtplib.html

https://gist.github.com/jasonjoh/3ec367594c3fa662ee983a617bdc7deb

https://gist.github.com/jasonjoh/3ec367594c3fa662ee983a617bdc7deb

回答by Nacho Parra

I found a library that it's working for me:

我找到了一个对我有用的库:

https://github.com/Narcolapser/python-o365

https://github.com/Narcolapser/python-o365

https://pypi.python.org/pypi/O365

https://pypi.python.org/pypi/O365

Install it using PIP and then:

使用 PIP 安装它,然后:

from O365 import Message
o365_auth = ('[email protected]','YourPassword')
m = Message(auth=o365_auth)
m.setRecipients('[email protected]')
m.setSubject('I made an email script.')
m.setBody('Talk to the computer, cause the human does not want to hear it any more.')
m.sendMessage()

回答by Prometheus

The code has slightly changed. The above code won't work. Please use the below code. Reference

代码略有改动。上面的代码不起作用。请使用以下代码。参考

from O365 import Account

credentials = ('client_id', 'client_secret')

account = Account(credentials)
m = account.new_message()
m.to.add('[email protected]')
m.subject = 'Testing!'
m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
m.send()