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
|
使用python调用第三方邮箱群发邮件
第三方邮件需要设置授权码这里是
163
邮箱,图上传不了。
设置
=
=
=
>邮箱安全设置
=
=
=
>客户端授权密码
代码部分:
#!/usr/bin/env python
#__*__coding:utf-8__*__
import
smtplib
from
email.mime.text
import
MIMEText as mimetext
from
email.mime.multipart
import
MIMEMultipart
#发件服务器地址
mail_host
=
'smtp.163.com'
#发件邮箱地址
sender_user
=
'xxxx_monitor@163.com'
#mail_pass = 'xxxxx'#登录密码
#邮箱授权码,不是登录密码
sender_pass
=
'xxxx'
#收件邮箱地址
receivers
=
[
'receiver1@163.com'
,
'receiver2@163.cn'
]
#邮件内容
message
=
mimetext(
'Python 邮件测试发送'
,
'plain'
,
'utf-8'
)
#发送邮箱地址
message[
'From'
]
=
sender_user
#群发邮件时会报错message['To']不支持列表,使用join函数把地址合成字符串
message[
'To'
]
=
","
.join(receivers)
#邮件主题
subject
=
'Python SMTP 邮件测试'
message[
'Subject'
]
=
subject
# try:
# smtpobj = smtplib.SMTP()
# smtpobj.connect(mail_host,25)
# smtpobj.login(mail_user,mail_pass)
# smtpobj.sendmail(sender,receivers,message)
# print '邮件发送成功'
# except:
# print "邮件发送失败"
smtpobj
=
smtplib.SMTP()
smtpobj.connect(mail_host,
25
)
smtpobj.login(sender_user,sender_pass)
smtpobj.sendmail(sender_user,receivers,message.as_string())
smtpobj.close()
|
本文转自 wangpengtai 51CTO博客,原文链接:http://blog.51cto.com/wangpengtai/1950887,如需转载请自行联系原作者