Python发邮件的代码如下:
只需要填写好加粗字体,即可正常使用。
from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody def Email(to, subject, body):
creds = Credentials(
username='xxxxxx',
password='xxxxxx'
)
account = Account(
primary_smtp_address='xxx@xxx.com',
credentials=creds,
autodiscover=True,
access_type=DELEGATE
)
m = Message(
account=account,
subject=subject,
body=HTMLBody(body),
to_recipients = [Mailbox(email_address=to)]
)
m.send() Email("xxx@xxx.com", "abc", "def")
但是如果Python环境安装有瑕疵,则报错如下:
$python3 ab.py
Traceback (most recent call last):
File "ab.py", line , in <module>
Email("xxx@xxx.com", "abc", "def")
File "ab.py", line , in Email
access_type=DELEGATE
File "/usr/local/lib/python3.5/site-packages/exchangelib/account.py", line , in __init__
credentials=credentials)
File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line , in discover
email=email)
File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line , in _try_autodiscover
return _try_autodiscover(e.server, credentials, email)
File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line , in _try_autodiscover
raise_from(AutoDiscoverFailed('All steps in the autodiscover protocol failed'), None)
File "/usr/local/lib/python3.5/site-packages/future/utils/__init__.py", line , in raise_from
exec(execstr, myglobals, mylocals)
File "<string>", line , in <module>
exchangelib.errors.AutoDiscoverFailed: All steps in the autodiscover protocol failed
至今仍无解。
好在我有台机器安装Python3.5.2正常,执行上述完全没有问题。
今天终于搞定!!! 2017-12-11 exchange的版本不对, 我本机版本:
pip3 search exchangelib
exchangelib (1.10.) - Client for Microsoft Exchange Web Services (EWS)
INSTALLED: 1.9.
LATEST: 1.10. 服务器版本为:
$pip3 search exchangelib
exchangelib (1.10.) - Client for Microsoft Exchange Web Services (EWS)
INSTALLED: 1.10.
LATEST: 1.10. 要做的就是把服务器上的版本降低到1.9.4,就