smtplib 是一个用于python2.7和python3.x的内置包,所以有不需要安装。您可以导入它,而无需安装任何其他软件包。
参考:https://docs.python.org/3/library/smtplib.html?highlight=smtplib
如果你安装,会出现以下错误:
$ pip3 install smtplib
Collecting smtplib
ERROR: Could not find a version that satisfies the requirement smtplib (from versions: none)
ERROR: No matching distribution found for smtplib
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
# 第三方 SMTP 服务
mail_host = "smtp.163.com" # SMTP服务器
mail_port = 465
mail_user = "[email protected]" # 用户名
mail_pass = "esefskjhurhsjeisjie" # 授权密码,非登录密码 (这个不是网页版的登陆密码,需要使用"应用程序专用密码")
sender = '[email protected]' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ '[email protected]' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
content = '我用Python学习过程!'
title = 'Python学习文档' # 邮件主题
def sendEmail():
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
163.com 发送邮件常见错误:http://help.163.com/09/1224/17/5RAJ4LMH00753VB8.html
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
# 第三方 SMTP 服务
mail_host = "smtp.gmail.com" # SMTP服务器
mail_port = 465
mail_user = "[email protected]" # 用户名
mail_pass = "esefskjhurhsjeisjie" # 授权密码,非登录密码 (goole 账号设置了2级密码,需要使用"应用程序专用密码")
sender = '[email protected]' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ '[email protected]' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
content = '我用Python学习过程!'
title = 'Python学习文档' # 邮件主题
def sendEmail():
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
gmail 发送邮件常见错误:https://support.google.com/mail/?p=BadCredentials
QQ邮箱服务器:smtp.qq.com,如果是腾讯企业邮箱,其服务器为smtp.exmail.qq.com,方式给上面的163.com和gmail一样。
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import email.mime.text
# 第三方 SMTP 服务
mail_host = "smtp.gmail.com" # SMTP服务器
mail_port = 465
mail_user = "[email protected]" # 用户名
mail_pass = "eskeskefksekjsefsek" # 授权密码,非登录密码 (goole 账号设置了2级密码,需要使用"应用程序专用密码")
sender = '[email protected]' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ '[email protected]' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
content = '我用Python学习过程!'
title = 'Python学习文档' # 邮件主题
def sendEmail():
message = MIMEMultipart()
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
# 添加邮件正文
txt = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message.attach(txt)
# 添加附件,从本地路径读取。如果添加多个附件,可以定义part_2,part_3等,然后使用part_2.add_header()和msg.attach(part_2)即可。
part = MIMEApplication(open('/Users/my-user/PycharmProjects/py-flask/templates/list.html', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename="list.html") # 给附件重命名,一般和原文件名一样,改错了可能无法打开.
message.attach(part)
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
基本跟上面一样,修改的地方有2个:
1. 内容可以是html格式的文本,如:
content = '
我用Python学习过程!
baidu.com'
* a 中的href 需要带删http/https,在QQ邮箱不带协议,就会打不开链接。
2. 邮件正文,使用html格式:MIMEText(content, 'html', 'utf-8')
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
# 第三方 SMTP 服务
mail_host = "smtp.gmail.com" # SMTP服务器
mail_port = 465
mail_user = "[email protected]" # 用户名
mail_pass = "esefskjhurhsjeisjie" # 授权密码,非登录密码 (goole 账号设置了2级密码,需要使用"应用程序专用密码")
sender = '[email protected]' # 发件人邮箱(最好写全, 不然会失败)
receivers = [ '[email protected]' ] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
content = '我用Python学习过程!
baidu.com'
title = 'Python学习文档' # 邮件主题
def sendEmail():
message = MIMEText(content, 'html', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, mail_port) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.ehlo()
smtpObj.sendmail(sender, receivers, message.as_string()) # 发送
smtpObj.close()
print("mail has been send successfully.")
except smtplib.SMTPException as e :
print(e)
if __name__ == '__main__':
sendEmail()
参考:
https://www.runoob.com/python/python-email.html
https://stackabuse.com/how-to-send-emails-with-gmail-using-python/
例子:
https://www.runoob.com/python/python-email.html