Python3.5 執行發郵件腳本失敗【惑】==>【搞定】


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 22, in <module>
    Email("xxx@xxx.com", "abc", "def")
  File "ab.py", line 12, in Email
    access_type=DELEGATE
  File "/usr/local/lib/python3.5/site-packages/exchangelib/account.py", line 66, in __init__
    credentials=credentials)
  File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line 214, in discover
    email=email)
  File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line 236, in _try_autodiscover
    return _try_autodiscover(e.server, credentials, email)
  File "/usr/local/lib/python3.5/site-packages/exchangelib/autodiscover.py", line 262, 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 398, in raise_from
    exec(execstr, myglobals, mylocals)
  File "<string>", line 1, 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.6)  - Client for Microsoft Exchange Web Services (EWS)
  INSTALLED: 1.9.4
  LATEST:    1.10.6


服務器版本為:
$pip3 search exchangelib
exchangelib (1.10.6)  - Client for Microsoft Exchange Web Services (EWS)
  INSTALLED: 1.10.4
  LATEST:    1.10.6 要做的就是把服務器上的版本降低到1.9.4,就🆗了。

pip3 install exchangelib==1.9.4

再次執行發郵件的操作,bingo,搞定!

多謝大管家【jason.wu】

 

如果想發送郵件的時候帶附件,請參考:(2018-06-11添加)

myfile = FileAttachment(name='report.pdf', content=open('/data/graph/report.pdf','rb').read())
m.attach(myfile)

謝謝!

 

求大俠們指點。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM