python发送邮件 大全汇总

https://blog.csdn.net/bmxwm/article/details/79007871

参考菜鸟教程
发送只有文字的邮件

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
    
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()
发送网页邮件

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
    
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()
发送带有附件的邮件:

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

43

44

45

46

47

48
    
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()
发送网页中带有图片的邮件

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

43

44

45
    
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()
---------------------  
作者:bmxwm  
来源:CSDN  
原文:https://blog.csdn.net/bmxwm/article/details/79007871  
版权声明:本文为博主原创文章,转载请附上博文链接!

data = "
mail_host = ""
mail_user = "" # 用户名
mail_pass = "" # 授权密码

title = ''
sender = '' # 发送邮箱
receivers = [''] # 接收邮箱

message = MIMEText(data, 'plain', 'utf-8')
message['From'] = "{}".format(sender)
message['To'] = ",".join(receivers)
message['Subject'] = title
try:
smtpobj = smtplib.SMTP_SSL(mail_host, 465)
smtpobj.login(mail_user, mail_pass)
smtpobj.sendmail(sender, receivers, message.as_string())
except smtplib.SMTPException as e:
print(e)
原文地址:https://www.cnblogs.com/fengff/p/10234696.html