参考菜鸟教程
发送只有文字的邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'xxxxxx@xxx.com'#发件人的邮件地址
password='xxxxxx'#发件人的客户端密码
host='smtp.xxx.com'#发件人用的邮件服务器
receivers = ['xxxxxx@xxx.com','xxxxxxx@xx.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText('文本内容~', 'plain', 'utf-8')
message['From'] = Header("发件人哦~", 'utf-8')#内容中显示的发件人
message['To'] = Header("收件人哦~", 'utf-8')#内容中显示的收件人
message['Subject'] = Header('邮件内容的标题~', 'utf-8')#邮件的题目
try:
smtpObj = smtplib.SMTP_SSL()#这个点要注意
smtpObj.connect(host)
smtpObj.login(sender,password) #邮箱登录
smtpObj.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException as e:
print ("Error: 发送邮件产生错误")
print(e)
smtpObj.close()
发送网页邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'xxxxxx@xx.com'#发件人的邮件地址
password='xxxxx'#发件人的客户端密码
host='smtp.xxx.com'#发件人用的邮件服务器
receivers = ['xxxxxx@xxx.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
meg_text='''
<h1>这个是大标题</h1>
<a href=https://xwmdream.cn>这个是链接</a>
'''
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEText(meg_text, 'html', 'utf-8')
message['From'] = Header("发件人哦~", 'utf-8')#内容中显示的发件人
message['To'] = Header("收件人哦~", 'utf-8')#内容中显示的收件人
message['Subject'] = Header('邮件内容的标题~', 'utf-8')#邮件的题目
try:
smtpObj = smtplib.SMTP_SSL()#这个点要注意
smtpObj.connect(host)
smtpObj.login(sender,password) #邮箱登录
smtpObj.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException as e:
print ("Error: 发送邮件产生错误")
print(e)
smtpObj.close()
发送带有附件的邮件:
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender = 'xxxxxx@xx.com'#发件人的邮件地址
password='xxxxx'#发件人的客户端密码
host='smtp.xxx.com'#发件人用的邮件服务器
receivers = ['xxxxxx@xxx.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
meg_text='''
<h1>这个是大标题</h1>
<a href=https://xwmdream.cn>这个是链接</a>
<p>这个邮件有附件</p>
'''
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEMultipart()
#邮件正文内容
message.attach(MIMEText(meg_text, 'html', 'utf-8'))
message['From'] = Header("发件人哦~", 'utf-8')#内容中显示的发件人
message['To'] = Header("收件人哦~", 'utf-8')#内容中显示的收件人
message['Subject'] = Header('邮件内容的标题~', 'utf-8')#邮件的题目
# 构造附件1,传送当前目录下的 test.txt 文件
att1 = MIMEText(open('test/test.txt', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
# 这里的filename可以任意写,写什么名字,邮件中显示什么名字
att1["Content-Disposition"] = 'attachment; filename="first.txt"'
message.attach(att1)
# 构造附件2,传送当前目录下的 runoob.txt 文件
att2 = MIMEText(open('test/test.mp3', 'rb').read(), 'base64', 'utf-8')
att2["Content-Type"] = 'application/octet-stream'
att2["Content-Disposition"] = 'attachment; filename="two.mp3"'
message.attach(att2)
try:
smtpObj = smtplib.SMTP_SSL()#这个点要注意
smtpObj.connect(host)
smtpObj.login(sender,password) #邮箱登录
smtpObj.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException as e:
print ("Error: 发送邮件产生错误")
print(e)
smtpObj.close()
发送网页中带有图片的邮件
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
sender = 'xxxxx@xxxx.com'#发件人的邮件地址
password='xxxxxx'#发件人的客户端密码
host='smtp.xxx.com'#发件人用的邮件服务器
receivers = ['xxxxxxxx@xx.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
meg_text='''
<h1>这个是大标题</h1>
<a href=https://xwmdream.cn>这个是链接</a>
<p>这个邮件有附件</p>
<img src="cid:image1">
'''
# 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码
message = MIMEMultipart()
#邮件正文内容
message.attach(MIMEText(meg_text, 'html', 'utf-8'))
message['From'] = Header("发件人哦~", 'utf-8')#内容中显示的发件人
message['To'] = Header("收件人哦~", 'utf-8')#内容中显示的收件人
message['Subject'] = Header('邮件内容的标题~', 'utf-8')#邮件的题目
# 指定图片为当前目录
fp = open('test/test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()
# 定义图片 ID,在 HTML 文本中引用
msgImage.add_header('Content-ID', '<image1>')
message.attach(msgImage)
try:
smtpObj = smtplib.SMTP_SSL()#这个点要注意
smtpObj.connect(host)
smtpObj.login(sender,password) #邮箱登录
smtpObj.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException as e:
print ("Error: 发送邮件产生错误")
print(e)
smtpObj.close()