python基於exchange函式傳送郵件過程詳解
1.Python hasattr() 函式
描述
hasattr() 函式用於判斷物件是否包含對應的屬性。
語法
hasattr 語法:
hasattr(object,name)
引數
- object -- 物件。
- name -- 字串,屬性名。
返回值
如果物件有該屬性返回 True,否則返回 False。
例項
以下例項展示了 hasattr 的使用方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Coordinate: x = 10 y = -5 z = 0 point1 = Coordinate() print(hasattr(point1,'x')) print(hasattr(point1,'y')) print(hasattr(point1,'z')) print(hasattr(point1,'no')) # 沒有該屬性
輸出結果:
True
True
True
False
打包.ui檔案:
C:\Python27\Lib\site-packages\PyQt4>pyuic4 E:\pyDemo\proTools.ui -o proTools.py
Qt 程式碼是面向物件的,並且簡單易學。所有我們新增的元件都是一個物件,並且都擁有自己的方法比如toPlainText()(用來讀取輸入框中的值)。這讓它使用起來非常方便。
程式碼打包:C:\Python27\Scripts>python pyinstaller-script.py -w E:\pyDemo\PyQt\testPyQt.py testPyQt.spec
-F 帶黑視窗 (打包成單個exe)
-W 不帶黑視窗(打包成檔案形式)
-i 加圖示
pyinstaller -i D:\ALIX_SH\2carReport\yx.ico -W D:\ALIX_SH\2carReport\upload2carReport.py
-F -w 打包成exe,但不帶黑視窗
pyinstaller -i D:\ALIX_SH\2carReport\yx.ico -F -w D:\ALIX_SH\2carReport\upload2carReport.py
下面用Pyinstaller生成一個可執行檔案試試。
在命令提示符中去到要打包的目錄;直接輸入"pyinstaller -F -w 檔名.py(F大寫);檔案目錄出現新的資料夾,裡面就是打包好的exe檔案了;
- -F,-onefile:打包成一個exe檔案
- -w,-windowed:使用視窗,無控制檯
- -c,-console:無視窗,使用控制檯
- -D,-onedir:建立一個目錄,包含exe檔案,但會依賴其他檔案
- --icon=圖示路徑
可以通過"Pyinstaller -h"來檢視
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='[email protected]',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("[email protected]","abc","def")
但是如果Python環境安裝有瑕疵,則報錯如下:
$python3 ab.py Traceback (most recent call last): File "ab.py",line 22,in <module> Email("[email protected]","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,搞定!
1、首先下載一個神器exchangelib,操作 pip install exchangelib
2、然後只需要將下面的資訊改成你的資訊就可以了
from exchangelib import Account,HTMLBody,ServiceAccount #忽略警告 import urllib3 urllib3.disable_warnings() #報錯處理:ConnectionError: HTTPConnectionPool(host='mail.xxx.com',port=443): Max retries exceeded with url: # import socket # socket.create_connection((host,port),timeout=10) def Email(to,body): creds = ServiceAccount(username='域\名',password='密碼') account = Account('傳送的郵箱',autodiscover=True) m = Message(account=account,to_recipients = [Mailbox(email_address=to)] ) m.send() Email("接收的郵箱","郵件標題","郵件內容")
安裝庫exchangelib
pip install exchangelib1
指令碼內容
# coding=utf-8 # # Created on 2018/2/ from exchangelib import DELEGATE,Configuration,NTLM,HTMLBody from exchangelib.protocol import BaseProtocol,NoVerifyHTTPAdapter #此句用來消除ssl證書錯誤,exchange使用自簽證書需加上 BaseProtocol.HTTP_ADAPTER_CLS = NoVerifyHTTPAdapter # 輸入你的域賬號如example\leo cred = Credentials(r'EXAMPLE\leo','輸入你的密碼') config = Configuration(server='輸入郵箱伺服器網頁地址',credentials=cred,auth_type=NTLM) a = Account( primary_smtp_address='輸入你要繫結的郵箱名([email protected])',config=config,autodiscover=False,access_type=DELEGATE ) # 此處為用來發送html格式郵件的檔案路徑 with open(r'C:\Users\leo\Desktop\1.html') as f: msg = f.read().decode('utf-8') m = Message( account=a,folder=a.sent,subject=u'測試郵件',body=HTMLBody(msg),to_recipients=[Mailbox(email_address='輸入你要繫結的郵箱名([email protected])')] ) m.send_and_save()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。