一、发送纯文本邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import smtplib
from email.mime.text import MIMEText
from email.header import Header
 
subject = "邮件标题"      # 邮件的主题
content = "邮件内容"      # 邮件的内容
sender = "[email protected]"         # 发件人
password = "qnrwndesjxmmijce"       # 刚才我们在QQ邮箱里设置的授权密码
receiver = "[email protected]"         # 收件人
 
message = MIMEText(content, "plain", "utf-8"# 发送的内容,内容的格式类型(plain与html),内容的编码方式
message["From"] = sender          # 发送邮箱
message["To"] = receiver          # 接收邮箱
message["Subject"] = subject      # 邮件标题
# message['Subject'] = Header(subject, 'utf-8')  # subject
# message = '\n'.join(['From: {}'.format(sender), 'To: {}'.format(receiver), 'Subject: {}'.format(subject), '', content])
 
# smtp = smtplib.SMTP("smtp.qq.com", 25)       # SMTP:普通的邮件发送形式
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)    # SMTP_SSL:QQ邮箱的SMTP服务器(端口465或587)
smtp.set_debuglevel(1)                         # 用set_debuglevel(1)就可以打印出和SMTP服务器交互的所有信息
smtp.login(sender, password)                   # 登录SMTP服务器,输入发送邮箱和密码
smtp.sendmail(sender, receiver, message.as_string())
smtp.quit()
smtp.close()

二、发送HTML格式的邮件

Python发送HTML格式的邮件与发送纯文本消息的邮件不同之处就是将MIMEText中_subtype设置为html。

1
2
content = '<h1>Hello</h1>' + '<p>send by <a href="http://www.python.org">Python</a>...</p>'
message = MIMEText(content, "html", "utf-8")

三、发送文本中带图片的邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
 
subject = "图片邮件测试"
content = """<p>Python 邮件发送测试...</p><p><img src="cid:image1"></p>"""
sender = "[email protected]"     # 发送人,换成自己的!
password = "qnrwndesjxmmijce"   # 授权码,换成自己的!
receiver = "[email protected]"    # 收件人
 
message = MIMEMultipart("related")   # 构造一个MIMEMultipart对象代表邮件本身。related 表示使用内嵌资源的形式 将邮件发送给对方
message["From"] = sender
message["To"] = receiver
message["Subject"] = subject
 
# msgAlternative = MIMEMultipart('alternative')               # 接收者的别名
# msgAlternative.attach(MIMEText(content, 'html', 'utf-8'))   # 添加文本
# message.attach(msgAlternative)
message.attach(MIMEText(content, 'html', 'utf-8'))   # 一步到位,不用上面三行代码
 
# ---------------发送图片的第一种方式-----------------------
with open(r'C:\Users\bt.cn\Desktop\004.jpg', 'rb') as fp:   # 二进制模式读取图片
    msgImage = MIMEImage(fp.read())
msgImage.add_header("Content-ID", "<image1>"# 定义图片ID,在HTML文本中引用
message.attach(msgImage)                       # 添加图片到邮箱信息中去
 
# ---------------发送图片的第二种方式-----------------------
# msgImage = MIMEText(open(r'C:\Users\bt.cn\Desktop\004.jpg', 'rb').read(), 'base64', 'utf-8')
# msgImage['Content-disposition'] = 'attachment;filename="happy.png"'
# message.attach(msgImage)
 
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtp.set_debuglevel(1)
smtp.login(sender, password)
try:
    smtp.sendmail(sender, receiver, message.as_string())
    smtp.quit()
    smtp.close()
except Exception as e:
    print("邮件发送失败,错误原因[{0}]".format(str(e)))
print("邮件发送成功")

四、发送带附件的邮件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
 
subject = "附件邮件测试"
content = "Python 邮件发送测试..."
sender = "[email protected]"     # 发送人,换成自己的!
password = "qnrwndesjxmmijce"   # 授权码,换成自己的!
receiver = "[email protected]"   # 收件人
 
message = MIMEMultipart("related")   # 构造一个MIMEMultipart对象代表邮件本身。related 表示使用内嵌资源的形式 将邮件发送给对方
message["From"] = sender
message["To"] = receiver
message["Subject"] = Header(subject, 'utf-8')
message.attach(MIMEText(content, 'html', 'utf-8'))   # 发送文本内容
 
# ---------------构造附件(文本或图片都行,文本用MIMEText,图片用MIMEText与MIMEImage都行)-----------------------
# att1 = MIMEImage(open(r'C:\Users\bt.cn\Desktop\004.jpg', 'rb').read())
att1 = MIMEText(open(r'C:\Users\bt.cn\Desktop\004.jpg', 'rb').read(), 'base64', 'utf-8')
att1["Content-Type"] = 'application/octet-stream'
att1['Content-disposition'] = 'attachment; filename="happy.png"'  # 这里的filename可以任意写,写什么名字,邮件中显示什么名字
message.attach(att1)
 
smtp = smtplib.SMTP_SSL("smtp.qq.com", 465)
smtp.set_debuglevel(1)
smtp.login(sender, password)
try:
    smtp.sendmail(sender, receiver, message.as_string())
    smtp.quit()
    smtp.close()
except Exception as e:
    print("邮件发送失败,错误原因[{0}]".format(str(e)))
print("邮件发送成功")

五、补充MIMEMultipart的可选类型

1
2
3
MIMEMultipart(‘mixed’)       # 如果一封邮件中含有附件
MIMEMultipart(‘alternative’)  # 可以传送超文本内容,但出于兼容性的考虑,一般在发送超文本格式内容的同时会同时发送一个纯文本内容的副本
MIMEMultipart(‘related’)      # 除了可以携带各种附件外,还可以将其它内容以内嵌资源的方式存储在邮件中

https://www.jianshu.com/p/66ef0ee329bd

https://www.cnblogs.com/pythonywy/p/11861090.html

https://blog.mimvp.com/article/27043.html

https://blog.csdn.net/qq_32252957/article/details/82593927

https://blog.csdn.net/zhangpan19910604/article/details/45065629

02-11 21:07