Python 3(13)使用 smtplib 模块发送 SMTP 邮件

Python3 入门专栏

http://blog.csdn.net/column/details/19679.html


Python  发送 SMTP 邮件


python 的 smtplib 模块对 smtp 协议进行了简单的封装,可以用于发送 smtp 协议的电子邮件;

发送普通邮件

 
import smtplib
from email.header import Header
from email.mime.text import MIMEText
# 使用 163 邮箱的 SMTP 服务
mail_host = "smtp.163.com"              # smtp 服务器
mail_user = "[email protected]"  # smtp 服务器验证用户名
mail_password = "*************"         # smtp 服务器验证密码(smtp 授权码)
sender = "[email protected]"          # 发送地址
receivers = ["[email protected]"]   # 接收地址
# 信息对象(dict 对象)
message = MIMEText("Hello world", "plain", "utf-8")
message["From"] = Header("assad", "utf-8")
message["To"] = Header("Mr.lin", "utf-8")
message["Subject"] = Header("python smtp 邮件测试", "utf-8")
try:
    server = smtplib.SMTP()                  # 创建 SMTP 对象
    server.connect(mail_host, 25)            # 连接 smtp 服务器,默认端口25
    server.login(mail_user, mail_password)   # 登陆 smtp 服务器
    server.sendmail(sender, receivers, message.as_string())   # 发送邮件
    print("send email success")
except smtplib.SMTPException as err:
    print("send email fail")
    print("Error message:", err)

发送 HTML 文本邮件

 
import smtplib
from email.header import Header
from email.mime.text import MIMEText
# 使用 163 邮箱的 SMTP 服务
mail_host = "smtp.163.com"              # smtp 服务器
mail_user = "[email protected]"  # smtp 服务器验证用户名
mail_password = "*************"         # smtp 服务器验证密码(smtp 授权码)
sender = "[email protected]"          # 发送地址
receiver = "[email protected]"   # 接收地址
mail_msg = """

Hello World!

this is a test email from python smtp

"""
# 信息对象(dict 对象)
message = MIMEText(mail_msg, "html", "utf-8")
message["From"] = Header(sender, "utf-8")
message["To"] = Header(receiver, "utf-8")
message["Subject"] = Header("python smtp html 邮件测试", "utf-8")
try:
    server = smtplib.SMTP()                  # 创建 SMTP 对象
    server.connect(mail_host, 25)            # 连接 smtp 服务器,默认端口25
    server.login(mail_user, mail_password)   # 登陆 smtp 服务器
    server.sendmail(sender, receiver, message.as_string())   # 发送邮件
    print("send email success")
except smtplib.SMTPException as err:
    print("send email fail")
    print("Error message:", err)

发送带有附件的邮件

 
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
# 使用 163 邮箱的 SMTP 服务
mail_host = "smtp.163.com"              
mail_user = "[email protected]" 
mail_password = "*************"        
sender = "[email protected]"   
receiver = "[email protected]" 
 
# 邮件信息对象
message = MIMEMultipart()
message['From'] = Header(sender, 'utf-8')
message['To'] =  Header(receiver, 'utf-8')
message['Subject'] = Header('python smtp 邮件附件测试', 'utf-8')
 
# 邮件正文内容
message.attach(MIMEText('this is a test email from python smtp', 'plain', 'utf-8'))
 
# 附件1:当前目录下的 test1.txt 文件
att1 = MIMEText(open('test1.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1["Content-Disposition"] = 'attachment; filename="test1.txt"'
# 附件2:当前目录下的 test2.txt 文件
att2 = MIMEText(open('test2.txt', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="test2.txt"'
# 向邮件对象添加附件对象
message.attach(att1)
message.attach(att2)
 
try:
    server = smtplib.SMTP()                 
    server.connect(mail_host, 25)            
    server.login(mail_user, mail_password)  
    server.sendmail(sender, receiver, message.as_string())   
    print("send email success")
except smtplib.SMTPException as err:
    print("send email fail")
    print("Error message:", err)

你可能感兴趣的:(Python,3,python,3,基本使用教程)