SMTP (Simple Mail Transfer Protocol)
郵件傳送代理 (Mail Transfer Agent,MTA) 程式使用SMTP協議來發送電郵到接收者的郵件伺服器。SMTP協議只能用來發送郵件,不能用來內送郵件。大多數的郵件發送伺服器 (Outgoing Mail Server) 都是使用SMTP協議。SMTP協議的預設TCP連接埠號碼是25。
SMTP協議的一個重要特點是它能夠接力傳送郵件。它工作在兩種情況下:一是電子郵件從客戶機傳輸到伺服器;二是從某一個伺服器傳輸到另一個伺服器。
smtplib模組還提供了SMTP_SSL類和LMTP類,對它們的操作與SMTP基本一致。
SMTP.set_debuglevel(level)
設定是否為偵錯模式。預設為False,即非偵錯模式,表示不輸出任何調試資訊。
SMTP.connect([host[, port]])
串連到指定的SMTP伺服器。參數分別表示SMTP主機和連接埠。注意: 也可以在host參數中指定連接埠號碼(如:smtp.163.com:25),這樣就沒必要給出port參數。
SMTP.login(user, password)
登陸到SMTP伺服器。現在幾乎所有的SMTP伺服器,都必須在驗證使用者資訊合法之後才允許發送郵件。
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options])
例
| 代碼如下 |
複製代碼 |
|
#!/usr/bin/env python
# -*- coding: gbk -*-
#匯入smtplib和MIMEText
import smtplib
from email.mime.text import MIMEText
#############
#要發給誰,這裡發給2個人
mailto_list=["aaa@juyimeng.com","bbb@juyimeng.com"]
#####################
#設定伺服器,使用者名稱、口令以及郵箱的尾碼
mail_host="smtp.126.com"
mail_user="xxx"
mail_pass="yyy"
mail_postfix="126.com"
######################
def send_mail(to_list,sub,content):
'''
to_list:發給誰
sub:主題
content:內容
send_mail("aaa@126.com","sub","content")
'''
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mailto_list,"subject","content"):
print "發送成功"
else:
print "發送失敗"
|
POP3 (Post Office Protocol) & IMAP (Internet Message Access Protocol)
POP協議和IMAP協議是用於郵件接收的最常見的兩種協議。幾乎所有的郵件用戶端和伺服器都支援這兩種協議。
POP3協議為使用者提供了一種簡單、標準的方式來訪問郵箱和擷取電郵。使用POP3協議的電郵用戶端通常的工作過程是:串連伺服器、擷取所有資訊並儲存在使用者主機、從伺服器刪除這些訊息然後中斷連線。POP3協議的預設TCP連接埠號碼是110。
IMAP協議也提供了方便的郵件下載服務,讓使用者能進行離線閱讀。使用IMAP協議的電郵用戶端通常把資訊保留在伺服器上直到使用者顯式刪除。這種特性使得多個用戶端可以同時管理一個郵箱。IMAP協議提供了摘要瀏覽功能,可以讓使用者在閱讀完所有的郵件到達時間、主題、寄件者、大小等資訊後再決定是否下載。IMAP協議的預設TCP連接埠號碼是143。
| 代碼如下 |
複製代碼 |
|
#!/usr/bin/env python3
#coding: utf-8
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
sender = '***'
receiver = '***'
subject = 'python email test'
smtpserver = 'smtp.163.com'
username = '***'
password = '***'
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
# Create the body of the message (a plain-text and an HTML version).
text = "Hi!nHow are you?nHere is the link you wanted:nhttp://www.python.org"
html = """
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
#構造附件
att = MIMEText(open('h:\python\1.jpg', 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="1.jpg"'
msg.attach(att)
smtp = smtplib.SMTP()
smtp.connect('smtp.163.com')
smtp.login(username, password)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit() |