Python - 无法发送电子邮件

时间:2017-01-04 16:56:27

标签: python-3.x email smtplib

我在Windows 7企业版64位上运行Python 3.4.2。

我有以下脚本(write_email.py)在运行时收到错误:

# smtplib module send mail

import smtplib

TO = 'address@somedomain.com'
SUBJECT = 'TEST MAIL'
TEXT = 'Here is a message from python.'

# Gmail Sign In
gmail_sender = 'address@gmail.com'
gmail_passwd = ''   #this is left blank on purpose

server = smtplib.SMTP('smtp-relay.gmail.com', 25)
server.ehlo()
server.starttls()
server.login(gmail_sender, gmail_passwd)

BODY = '\r\n'.join(['To: %s' % TO,
                'From: %s' % gmail_sender,
                'Subject: %s' % SUBJECT,
                '', TEXT])

try:
    server.sendmail(gmail_sender, [TO], BODY)
print ('email sent')
except:
    print ('error sending mail')

server.quit()

错误是:

Traceback (most recent call last): 
File "email_test.py", line 17, in <module>
server.login(gmail_sender, gmail_passwd)
File "C:\Python34\lib\smtplib.py", line 652, in login
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (501, b'5.5.2 Cannot Decode response       13sm147030
88ith.4 - gsmtp')

我已经做了很多搜索来解决这个问题,但到目前为止还没有运气。

0 个答案:

没有答案