SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式
python的smtplib提供了一种很方便的途径发送电子邮件。它对smtp协议进行了简单的封装。
Python创建 SMTP 对象语法:
import smtplib
smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
参数说明:
host: SMTP 服务器主机。 你可以指定主机的ip地址或者域名如: runoob.com,这个是可选参数。
port: 如果你提供了 host 参数, 你需要指定 SMTP 服务使用的端口号,一般情况下 SMTP 端口号为25。
local_hostname: 如果 SMTP 在你的本机上,你只需要指定服务器地址为 localhost 即可。
Python SMTP 对象使用 sendmail 方法发送邮件,语法如下:
SMTP.sendmail(from_addr, to_addrs, msg[, mail_options, rcpt_options]
from_addr: 邮件发送者地址。
to_addrs: 字符串列表,邮件发送地址。
msg: 发送消息
# 使用163邮箱给自己发邮件信息
import smtplib
from email.mime.text import MIMEText
from email.header import Header
mail_host = "smtp.163.com"
mail_user = "[email protected]"
mail_pass = "aaaaaa"
sender = "[email protected]"
receivers = ["[email protected]"]
message = MIMEText("python 邮件测试","plain","utf-8")
message["From"] = Header("ceshi","utf-8")
message["To"] = Header("CESHI","UTF-8")
subject = "python semp email ceshi ...."
message["Subject"] = Header(subject,"utf-8")
try:
smtpOBJ = smtplib.SMTP()
smtpOBJ.connect(mail_host, 25)
smtpOBJ.login(mail_user,mail_pass)
smtpOBJ.sendmail(sender,receivers,message.as_string())
except smtplib.SMTPException:
print("error")
# 使用163邮箱发送信息,带附件
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
mail_host = "smtp.163.com"
mail_user = "[email protected]"
mail_pass = "aaaaa"
sender = "[email protected]"
receivers = ["[email protected]"]
message = MIMEMultipart()
message["From"] = Header("wo","utf-8")
message["To"] = Header("ni","utf-8")
subject = "python smtp text, 附件"
message["Subject"] = Header("subject","utf-8")
message.attach(MIMEText('这是菜鸟教程Python 邮件发送测试……', 'plain', 'utf-8'))
att1 = MIMEText(open("tt.yaml","r").read(),"base64","utf-8")
att1["Content-Type"] = "application/octet-stream"
att1["Content-Disposition"] = 'attachment; filename="tt.yaml"'
message.attach(att1)
try:
smtpOBJ = smtplib.SMTP()
smtpOBJ.connect(mail_host, 25)
smtpOBJ.login(mail_user,mail_pass)
smtpOBJ.sendmail(sender,receivers,message.as_string())
except smtplib.SMTPException:
print("error")
# 使用163邮箱发送信息,带附件和html
在 HTML 文本中添加图片
邮件的 HTML 文本中一般邮件服务商添加外链是无效的,正确添加突破的实例如下所示:
import smtplib
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
mail_host = "smtp.163.com"
mail_user = "[email protected]"
mail_pass = "abc115698"
sender = "[email protected]"
receivers = ["[email protected]"]
msgRoot = MIMEMultipart("related")
msgRoot["From"] = Header("wo")
msgRoot["To"] = Header("ni")
subject = "Python SMTP 邮件测试,html + 图片"
msgRoot["Subject"] = Header(subject,"utf-8")
msgAlternative = MIMEMultipart("alternative")
msgRoot.attach(msgAlternative)
mail_msg = """
<p>Python 邮件发送测试...</p>
<p><a href="http://www.runoob.com">菜鸟教程链接</a></p>
<p>图片演示:</p>
<p><img src="cid:image1"></p>
"""
msgAlternative.attach(MIMEText(mail_msg, 'html', 'utf-8'))
fb = open("1111.jpg","rb")
msgImage = MIMEImage(fb.read())
fb.close()
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)
try:
smtpOBJ = smtplib.SMTP()
smtpOBJ.connect(mail_host, 25)
smtpOBJ.login(mail_user,mail_pass)
smtpOBJ.sendmail(sender,receivers,msgRoot.as_string())
except smtplib.SMTPException:
print("error")
# 验证开通邮箱的smtp协议的授权码,并验证登录

验证代码:
import smtplib
from email.mime.text import MIMEText
server = smtplib.SMTP("smtp.163.com",25)
server.login("[email protected]","aaaaaa")
MSG = MIMEText("hello,send by python..","plain","utf-8")
server.sendmail("[email protected]",["[email protected]"],MSG.as_string())
参考链接: http://www.runoob.com