Python通过SMTP发送邮件总是验证失败。


以下是我的代码及返回信息,163和qq的都尝试过但都是验证错误,密码什么的我肯定是没有输错的。
代码:


 #-*- encoding: utf-8 -*-
import os, sys
import smtplib
from smtplib import SMTP_SSL
from email.header import Header
from email.mime.text import MIMEText

mailInfo = {
    "from": "[email protected]",
    "to": "[email protected]",
    "hostname": "smtp.qq.com",
    "username": "[email protected]",
    "password": "*********",
    "mailsubject": "this is test",
    "mailtext": "hello, this is send mail test.",
    "mailencoding": "utf-8"
}

if __name__ == '__main__':
    smtp = SMTP_SSL(mailInfo["hostname"])
    smtp.set_debuglevel(1)
    smtp.ehlo(mailInfo["hostname"])
    smtp.login(mailInfo["username"],mailInfo["password"])

    msg = MIMEText(mailInfo["mailtext"],"text",mailInfo["mailencoding"])
    msg["Subject"] = Header(mailInfo["mailsubject"],mailInfo["mailencoding"])
    msg["from"] = mailInfo["from"]
    msg["to"] = mailInfo["to"]
    smtp.sendmail(mailInfo["from"], mailInfo["to"], msg.as_string())

    smtp.quit()

返回错误:


 send: 'ehlo smtp.qq.com\r\n'
reply: '250-smtp.qq.com\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 73400320\r\n'
reply: '250-AUTH LOGIN PLAIN\r\n'
reply: '250-AUTH=LOGIN\r\n'
reply: '250-MAILCOMPRESS\r\n'
reply: '250 8BITMIME\r\n'
reply: retcode (250); Msg: smtp.qq.com
PIPELINING
SIZE 73400320
AUTH LOGIN PLAIN
AUTH=LOGIN
MAILCOMPRESS
8BITMIME
send: 'AUTH PLAIN AGx************************************vZA==\r\n'
reply: '535 Authentication failed\r\n'
reply: retcode (535); Msg: Authentication failed
Traceback (most recent call last):
  File "C:\Users\Daniel\Desktop\mail1.py", line 23, in <module>
    smtp.login(mailInfo["username"],mailInfo["password"])
  File "C:\Python27\lib\smtplib.py", line 622, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (535, 'Authentication failed')

拜托大家帮忙解决下我的问题!!!

python smtp mail

卖萌so可耻 9 years, 3 months ago

谢谢 韦轩 的回答,差点把这个问题给忘了,当初一直出错是因为多年前就把qq smtp服务打开了,不知道后来qq跟163一样开启smtp服务也要设置独立密码。当关闭smtp服务再重新开启并设置密码就没问题了。

cc792 answered 9 years, 3 months ago

哥们,说的很清楚了,登陆验证失败,一般来说,是因为没有开启 SMTP 服务。

我关闭了服务,和你的报错一样的。

重新打开这个服务,就成功。具体的打开方法网上可以搜索到,一般是在账户里面设置。

附我成功的截图和log


 send: 'ehlo [222.212.253.207]\r\n'
reply: '250-smtp.qq.com\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 73400320\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH LOGIN PLAIN\r\n'
reply: '250-AUTH=LOGIN\r\n'
reply: '250-MAILCOMPRESS\r\n'
reply: '250 8BITMIME\r\n'
reply: retcode (250); Msg: smtp.qq.com
PIPELINING
SIZE 73400320
STARTTLS
AUTH LOGIN PLAIN
AUTH=LOGIN
MAILCOMPRESS
8BITMIME
send: 'AUTH PLAIN ADgwNTM2NjE4MEBxcS5jb20AMjAxNXdvaGVuaGFvPz8=\r\n'
reply: '235 Authentication successful\r\n'
reply: retcode (235); Msg: Authentication successful
send: 'mail FROM:<[email protected]> size=128\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'rcpt TO:<[email protected]>\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'data\r\n'
reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
send: 'Content-Type: text/plain; charset="utf-8"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\n\r\naGVsbG8sIHNlbmQgYnkgUHl0aG9uLi4u\r\n.\r\n'
reply: '250 Ok: queued as \r\n'
reply: retcode (250); Msg: Ok: queued as
data: (250, 'Ok: queued as')
send: 'quit\r\n'
reply: '221 Bye\r\n'
reply: retcode (221); Msg: Bye

犬走椛丶天羽 answered 9 years, 3 months ago

Your Answer