python发送邮件的模块_Python使用smtplib模块发送电子邮件的流程详解

本文详细讲述了如何使用Python的smtplib库通过SMTP服务器登录163邮箱,包括遇到的SMTPAuthenticationError问题及解决方法。重点介绍了设置客户端密码并正确构造MIMEText对象以发送带有发件人和主题的邮件,还提供了完整的代码示例。
摘要由CSDN通过智能技术生成

1、登录SMTP服务器

首先使用网上的方法(这里使用163邮箱,smtp.163.com是smtp服务器地址,25为端口号):

?

1

2

3

4

5

6

7

8

9

import smtplib

server= smtplib.SMTP('smtp.163.com',25)

server.login('j_hao104@163.com','password')

Traceback (most recent call last):

File "C:/python/t.py", line192,in

server.login('j_hao104@163.com','password')

File "C:\Python27\lib\smtplib.py", line622,in login

raise SMTPAuthenticationError(code, resp)

smtplib.SMTPAuthenticationError: (535,'Error: authentication failed')

发现返回:

?

1

smtplib.SMTPAuthenticationError: (535, 'Error: authentication failed')

,提示验证失败。

有说python不支持SMTP服务,或是服务没开启之类的。但是我想起上次我用foxmail登录我的163邮箱的时候,邮箱密码都输对了还是提示我密码错误,最后的解决办法是:像QQ和163邮箱现在都有个客户端密码,用第三方登录时需用客户端密码登录才行,python也是如此,因此去设置好客户端密码,再用客户端密码登录。

11111a0G-0.png

?

1

2

3

import smtplib

server= smtplib.SMTP('smtp.163.com',25)

server.login('j_hao104@163.com','clientPassword')

此时便返回登录成功提示:

?

1

(235, 'Authentication successful')

2、发送邮件

首先使用网上给出的代码:

?

1

2

3

4

5

6

import smtplib

from email.mime.textimport MIMEText

server= smtplib.SMTP('smtp.163.com',25)

server.login('j_hao104@163.com','clientPassword')

msg= MIMEText('hello, send by Python...','plain','utf-8')

server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())

构造MIMEText对象时,第一个参数是邮件正文,第二个参数是MIME的subtype,最后个是编码方式。

sendmail是发邮件方法,第一个参数是发件邮箱,第二个参数是收件人邮箱,是一个列表,代表可以同时发给多个人,as_string是把MIMEText对象变成str。

但是执行结果并不能得到网上说的结果:

1111191R6-1.jpg

而是:

?

1

2

3

4

5

6

Traceback (most recent call last):

File "C:/python/t.py", line 195, in

server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())

File "C:\Python27\lib\smtplib.py", line 746, in sendmail

raise SMTPDataError(code, resp)

smtplib.SMTPDataError: (554, 'DT:SPM 163 smtp11,D8CowEDpDkE427JW_wQIAA--.4996S2 1454562105,please see http://mail.163.com/help/help_spam_16.htm?ip=171.221.144.51&hostid=smtp11&time=1454562105')

网上一查才知道:smtplib.SMTPDataError: (554, 'DT:SPM 163 smtp11……的错误是因为信封发件人和信头发件人不匹配。可以看出看出图片中并没有发件人和主题,所以需要对代码做如下修改:

?

1

2

3

4

5

6

7

8

9

10

import smtplib

from email.headerimport Header

from email.mime.textimport MIMEText

server= smtplib.SMTP('smtp.163.com',25)

server.login('j_hao104@163.com','clientPassword')

msg= MIMEText('hello, send by Python...','plain','utf-8')

msg['From']= 'j_hao104@163.com '

msg['Subject']= Header(u'text','utf8').encode()

msg['To']= u'飞轮海 '

server.sendmail('j_hao104@163.com', ['946150454@qq.com'], msg.as_string())

这样就能成功发出邮件啦

msg里的具体信息可以用一般发邮件方式发封邮件测试下

1111195263-2.png

3、参考示例

?

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

import smtplib

from email.mime.textimport MIMEText

to_list= ['123@123.com','456@456.com']

server_host= 'smtp.163.com'

username= '你的邮箱账号'

password= '你的邮箱密码'

def send(to_list, sub, content):

'''

:param to_list: 收件人邮箱

:param sub: 邮件标题

:param content: 内容

'''

me= "manager" + "<" + username+ ">"

# _subtype 可以设为html,默认是plain

msg= MIMEText(content, _subtype='html')

msg['Subject']= sub

msg['From']= me

msg['To']= ';'.join(to_list)

try:

server= smtplib.SMTP()

server.connect(server_host)

server.login(username, password)

server.sendmail(me, to_list, msg.as_string())

server.close()

except Exception as e:

print str(e)

if __name__== '__main__':

send(to_list,"这个是一个邮件","

Hello, It's test email.

")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值