python发送邮件 大全汇总

参考菜鸟教程

发送只有文字的邮件

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()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值