smtp協議的基本命令包括:
HELO 向服務器標識用戶身份
MAIL 初始化郵件傳輸 mail from:
RCPT 標識單個的郵件接收人;常在MAIL命令后面,可有多個rcpt to:
DATA 在單個或多個RCPT命令后,表示所有的郵件接收人已標識,并初始化數據傳輸,以.結束
VRFY 用于驗證指定的用戶/郵箱是否存在;由于安全方面的原因,服務器常禁止此命令
EXPN 驗證給定的郵箱列表是否存在,擴充郵箱列表,也常被禁用
HELP 查詢服務器支持什么命令
NOOP 無操作,服務器應響應OK
QUIT 結束會話
RSET 重置會話,當前傳輸被取消
MAIL FROM 指定發送者地址
RCPT TO 指明的接收者地址
一般smtp會話有兩種方式,一種是郵件直接投遞,就是說,比如你要發郵件給zzz@163.com,那就直接連接163.com的郵件服務器,把信投給zzz@163.com; 另一種是驗證過后的發信,它的過程是,比如你要發郵件給zzz@163.com,你不是直接投到163.com,而是通過自己在sina.com的另一個郵箱來發。這樣就要先連接sina.com的smtp服務器,然后認證,之后在把要發到163.com的信件投到sina.com上,sina.com會幫你把信投遞到163.com。
第一種方式的命令流程基本是這樣:
1. helo
2. mail from
3. rcpt to
4. data
5. quit
但是第一種發送方式一般有限制的,就是rcpt to指定的這個郵件接收者必須在這個服務器上存在,否則是不會接收的。 先看看代碼:
代碼如下:
#-*- encoding: gb2312 -*-import os, sys, stringimport smtplib # 郵件服務器地址mailserver = "smtp.163.com"# smtp會話過程中的mail from地址from_addr = "asfgysg@zxsdf.com"# smtp會話過程中的rcpt to地址to_addr = "zhaoweikid@163.com"# 信件內容msg = "test mail" svr = smtplib.SMTP(mailserver)# 設置為調試模式,就是在會話過程中會有
注意的是,163.com是有反垃圾郵件功能的,想上面的這種投遞郵件的方法不一定能通過反垃圾郵件系統的檢測的。所以一般不推薦個人這樣發送。
第二種有點不一樣:
1.ehlo
2.auth login
3.mail from
4.rcpt to
5.data
6.quit
相對于第一種來說,多了一個認證過程,就是auth login這個過程。
代碼如下:
#-*- encoding: gb2312 -*-import os, sys, stringimport smtplibimport base64 # 郵件服務器地址mailserver = "smtp.163.com"# 郵件用戶名username = "xxxxxx@163.com"# 密碼password = "xxxxxxx"# smtp會話過程中的mail from地址from_addr = "xxxxxx@163.com"# smtp會話過程中的rcpt to地址to_addr = "yyyyyy@163.com"# 信件內容msg = "my test mail" svr = smtplib.SMTP(mailserver)# 設置為調試模式,就是在會話過程中會有
上面說的是最普通的情況,但是不能忽略的是現在好多企業郵件是支持安全郵件的,就是通過SSL發送的郵件,這個怎么發呢?SMTP對SSL安全郵件的支持有兩種方案,一種老的是專門開啟一個465端口來接收ssl郵件,另一種更新的做法是在標準的25端口的smtp上增加一個starttls的命令來支持。
看看第一種怎么辦:
代碼如下:
#-*- encoding: gb2312 -*-import os, sys, string, socketimport smtplib class SMTP_SSL (smtplib.SMTP): def __init__(self, host='', port=465, local_hostname=None, key=None, cert=None): self.cert = cert self.key = key smtplib.SMTP.__init__(self, host, port, local_hostname) def connect(self, host='localhost', port=465): if not port and (host.find(':') == host.rfind(':')): i = host.rfind(':') if i >= 0: host, port = host[:i], host[i+1:] try: port = int(port) except ValueError: raise socket.error, "nonnumeric port" if not port: port = 654 if self.debuglevel > 0: print>>stderr, 'connect:', (host, port) msg = "getaddrinfo returns an empty list" self.sock = None for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM): af, socktype, proto, canonname, sa = res try: self.sock = socket.socket(af, socktype, proto) if self.debuglevel > 0: print>>stderr, 'connect:', (host, port) self.sock.connect(sa) # 新增加的創建ssl連接 sslobj = socket.ssl(self.sock, self.key, self.cert) except socket.error, msg: if self.debuglevel > 0: print>>stderr, 'connect fail:', (host, port) if self.sock: self.sock.close() self.sock = None continue break if not self.sock: raise socket.error, msg # 設置ssl self.sock = smtplib.SSLFakeSocket(self.sock, sslobj) self.file = smtplib.SSLFakeFile(sslobj); (code, msg) = self.getreply() if self.debuglevel > 0: print>>stderr, "connect:", msg return (code, msg) if __name__ == '__main__': smtp = SMTP_SSL('192.168.2.10') smtp.set_debuglevel(1) smtp.sendmail("zzz@xxx.com", "zhaowei@zhaowei.com", "xxxxxxxxxxxxxxxxx") smtp.quit()
這里我是從原來的smtplib.SMTP派生出了新的SMTP_SSL類,它專門來處理ssl連接。我這里測試的192.168.2.10是我自己的測試服務器.
第二種是新增加了starttls的命令,這個很簡單,smtplib里就有這個方法,叫smtplib.starttls()。當然,不是所有的郵件系統都支持安全郵件的,這個需要從ehlo的返回值里來確認,如果里面有starttls,才表示支持。相對于發送普通郵件的第二種方法來說,只需要新增加一行代碼就可以了:
代碼如下:
#-*- encoding: gb2312 -*-import os, sys, stringimport smtplibimport base64 # 郵件服務器地址mailserver = "smtp.163.com"# 郵件用戶名username = "xxxxxx@163.com"# 密碼password = "xxxxxxx"# smtp會話過程中的mail from地址from_addr = "xxxxxx@163.com"# smtp會話過程中的rcpt to地址to_addr = "yyyyyy@163.com"# 信件內容msg = "my test mail" svr = smtplib.SMTP(mailserver)# 設置為調試模式,就是在會話過程中會有
注意: 以上的代碼為了方便我都沒有判斷返回值,嚴格說來,是應該判斷一下返回的代碼的,在smtp協議中,只有返回代碼是2xx或者3xx才能繼續下一步,返回4xx或5xx的,都是出錯了。
【相關推薦】
1. 詳細介紹Python使用SMTP發送郵件實例
2. Python 使用SMTP發送郵件的代碼小結
3. c#調用qq郵箱smtp發送郵件修改版代碼
4. Python使用SMTP發送郵件
5. php smtp發送郵件
6. Python SMTP郵件模塊詳解
7. 分享Python實現SMTP發送郵件圖文實例
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com