TKe*_*err 10 python email ssl office365
我正在尝试使用python从我的office365公司帐户发送电子邮件.我是python的新手.此代码以前在使用我的hotmail帐户时有效,但是现在我需要发送机密信息,我必须使用我的公司电子邮件.
我尝试了几件事.
535 5.7.3 Authentication unsuccessful我不清楚证书部分,但我的步骤包括,在线查找如何导出证书.使用chrome浏览器,microsoftonline.com有一个链证书.我可以导出root和根目录下面的级别,但不能导出最后一级.我不知道如何传递这两个文件,所以我只是传递了根证书.此时我收到错误:ssl.SSLError: [SSL] PEM lib (_ssl.c:3309)
我陷入了困境.任何帮助表示赞赏.代码如下
import smtplib
mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls(certfile='office365.cer')
mailserver.ehlo()
mailserver.login('user@company.co', 'password')
mailserver.sendmail('user@company.co','user@company.co','python email')
mailserver.quit()
Run Code Online (Sandbox Code Playgroud)
Gal*_*man 15
好吧,你快到了.以下代码将解决这个问题:
import smtplib
mailserver = smtplib.SMTP('smtp.office365.com',587)
mailserver.ehlo()
mailserver.starttls()
mailserver.login('user@company.co', 'password')
mailserver.sendmail('user@company.co','user@company.co','python email')
mailserver.quit()
Run Code Online (Sandbox Code Playgroud)
使用以下链接获取更多信息:
http://www.aventistech.com/2016/03/07/python-send-email-via-office-365-tls/
https://docs.python.org/3/library/smtplib.html
https://gist.github.com/jasonjoh/3ec367594c3fa662ee983a617bdc7deb
小智 11
我找到了一个适合我的图书馆:
https://github.com/Narcolapser/python-o365
https://pypi.python.org/pypi/O365
使用PIP安装它,然后:
from O365 import Message
o365_auth = ('YourAccount@office365.com','YourPassword')
m = Message(auth=o365_auth)
m.setRecipients('reciving@office365.com')
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()
Run Code Online (Sandbox Code Playgroud)
对我来说,@Prometheus 提供的答案是“RuntimeError:未找到身份验证令牌。需要身份验证流程”,如评论中所述。可能是因为我的公司电子邮件启用了 2fa。所以我必须遵循https://github.com/janscas/pyo365#authentication和https://pypi.org/project/O365/#authentication中提供的步骤
要使用 oauth,您首先需要在 Microsoft 应用程序注册门户上注册您的应用程序。
from O365 import Account
scopes = ["IMAP.AccessAsUser.All", "POP.AccessAsUser.All", "SMTP.Send", "Mail.Send", "offline_access"]
account = Account(credentials=('client_id_of_azureapp', 'client_secret_of_azureapp'))
result = account.authenticate(scopes=scopes) # request a token for this scopes
Run Code Online (Sandbox Code Playgroud)
from O365 import Account
from O365.utils.token import FileSystemTokenBackend
tk = FileSystemTokenBackend(token_path=".", token_filename="o365_token.txt")
credentials = ('client_id', 'client_secret')
account = Account(credentials, auth_flow_type = 'public',token_backend=tk)
m = account.new_message()
m.to.add('user@company.com')
m.subject = 'Testing!'
m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
m.send()
Run Code Online (Sandbox Code Playgroud)
注意:如果您获得管理员同意或者您是管理员或天蓝色帐户,您可以跳过步骤 4、5、6 并直接使用以下代码。
from O365 import Account
from O365.utils.token import FileSystemTokenBackend
tk = FileSystemTokenBackend(token_path=".", token_filename="o365_token.txt")
credentials = ('client_id', 'client_secret') # from step 2,3
account = Account(credentials, auth_flow_type = 'credentials', tenant_id="your_app_tenant_id") # tenant_id (required) available just below client_id in azure
if account.authenticate():
print('Authenticated!')
mailbox = account.mailbox("user@company.com") # Your email (required) from which you want to send email (your app should have permission to this email)
m = mailbox.new_message()
m.to.add('touser@company.com')
m.subject = 'Testing!'
m.body = "George Best quote: I've stopped drinking, but only while I'm asleep."
m.send()
Run Code Online (Sandbox Code Playgroud)