最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】python中smtp发送gmail邮箱

Python crifan 7469浏览 0评论

折腾了:

【未解决】python中smtp发送QQ邮件出错:smtplib.SMTPDataError 550 Error content rejected mail.qq.com rejectedmail.html

【已解决】Python中smtp如何发送多个收件人地址且带名字的且可以被格式化

【基本解决】python中用smtp发送QQ邮件提示成功但是收件人收不到邮件

【已解决】python中smtp发送qq邮件出错:smtplib.SMTPAuthenticationError 530 Must issue a STARTTLS command first

【已解决】python中smtp用qq发送邮件出错:smtplib.SMTPAuthenticationError 530 Error A secure connection is required

但是之前调试时还是可以实现邮件发送的。

只不过是调试多了,被当垃圾邮件,所以不给发送了而已。

现在去试试换用gmail能否发送邮件。

python smtp gmail

How to Send Emails with Gmail using Python

Tutorial: How to send an email with Python – Nael Shiab

Send mail from your Gmail account using Python – GeeksforGeeks

端口都说是587

先去登录自己的gmail账户,去看看smtp方面的设置是什么

https://mail.google.com/mail/

没有看到对应配置,点击 配置说明:

在使用 POP 的其他电子邮件客户端上阅读 Gmail 邮件 – Gmail帮助

更改 SMTP 设置以便在其他客户端上查看 Gmail – Gmail帮助

接收邮件 (IMAP) 服务器
要求 SSL:是
端口:993
发送邮件 (SMTP) 服务器
要求 SSL:是
要求 TLS:是(如适用)
使用身份验证:是
SSL 端口:465
TLS/STARTTLS 端口:587
完整名称或显示名称
您的姓名
帐号名、用户名或电子邮件地址
您的完整电子邮件地址
密码
您的 Gmail 密码
接收邮件 (POP) 服务器
要求 SSL:是
端口:995
发送邮件 (SMTP) 服务器
要求 SSL:是
要求 TLS:是(如适用)
使用身份验证:是
SSL 端口:465
TLS/STARTTLS 端口:587
如果您是通过单位或学校帐号使用 Gmail,请向您的管理员咨询正确的 SMTP 配置。
服务器超时
大于 1 分钟(推荐 5 分钟)
完整名称或显示名称
您的姓名
帐号名、用户名或电子邮件地址
您的电子邮件地址
密码
您的 Gmail 密码

所以就是:

smtp用SSL端口用465或587

去试试

465的话:

smtpObj = smtplib.SMTP_SSL(smtpServer, smtpPort)

就死掉了。

换成587,问题依旧。

死在了:

new_socket = socket.create_connection((host, port), timeout)

全局翻墙试试,问题依旧。

重启PyCharm,问题依旧。

换成SMTP不带SSL,加上587试试

至少是可以继续运行了。

结果出错:

    smtpObj.login(sender, senderPassword)

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”, line 585, in login

    raise SMTPException(“SMTP AUTH extension not supported by server.”)

smtplib.SMTPException: SMTP AUTH extension not supported by server.

所以再去加上:

# start TLS for security

s.starttls()

试试

结果非SSL的587的SMTP竟然也始终死掉没反应了。

等了好多分钟后,以为超时了,但是不是,是另外的错误:

    smtpObj = smtplib.SMTP(smtpServer, smtpPort)

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”, line 256, in __init__

    (code, msg) = self.connect(host, port)

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”, line 316, in connect

    self.sock = self._get_socket(host, port, self.timeout)

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”, line 291, in _get_socket

    return socket.create_connection((host, port), timeout)

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”, line 575, in create_connection

    raise err

socket.error: [Errno 65] No route to host

python smtp gmail socket.error Errno 65 No route to host

python – Errno 65 No route to host when trying to send an email – Stack Overflow

ERROR: [Errno 65] No route to host – Google Groups

How to add gmail address to smtp? · Issue #57 · FedericoCeratto/bottle-cork

全局翻墙,等了会,好像可以继续了。

结果代码:

    smtpObj = smtplib.SMTP(smtpServer, smtpPort)

    # start TLS for security

    smtpObj.starttls()

smtpObj.set_debuglevel(1)

smtpObj.login(sender, senderPassword)

出错:

    smtpObj.login(sender, senderPassword)

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”, line 622, in login

    raise SMTPAuthenticationError(code, resp)

smtplib.SMTPAuthenticationError: (534, ‘5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvp\n5.7.14 dWrY1pMC55G0VTYo8kNQkGzMZBundjFyUtwLM2MUImj2xBQYNR-grpmxTsZEmaJiwoEUVD\n5.7.14 rhyax-mMiehkvLGR5Cc0BnHkxqCZQFBvczOptJaWZtI5a2x4z_WyNszwI0Y5qQzUQRHkuM\n5.7.14 CC_3WDT_ICX3oafsvkFo6YYd4EqLOUFm6fQq3-uNs0c1vFy63cuBUTQdAaQNhnIhjDLZ2C\n5.7.14 mQRk2xmePvogt3bg39Wn9fjhT4HuA> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 q69sm3842075pfa.161 – gsmtp’)

https://myaccount.google.com/?pli=1

如果是465加上

smtpObj = smtplib.SMTP_SSL(smtpServer, smtpPort)

是可以执行的,但是login出错:

    smtpObj.login(sender, senderPassword)

  File “/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py”, line 622, in login

    raise SMTPAuthenticationError(code, resp)

smtplib.SMTPAuthenticationError: (534, ‘5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsw\n5.7.14 ugHy4KXxOcqHBpUMQWKeosK3EWkda4AfLec_JE88g_JBA-66d2LuXKzCLgpdqIRMpzCQlo\n5.7.14 M-JZblWWXkfvc480u9xo-8cM9Dx6ZHQfU13HSXGwfIkW0scLPgkAYj1MqLy63qO-BT3Z8f\n5.7.14 ASQzQH8WPkLpTC0kgexfwsZCgl625nIJx7Ed9KqKy0O-qGnXHH-Z6NJVCFO_qKU0G52-IE\n5.7.14 g8cEYsGbY3oFupLazulC88t_0FxLM> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14  Learn more at\n5.7.14  https://support.google.com/mail/answer/78754 62sm3491676pgh.31 – gsmtp’)

https://support.google.com/mail/answer/78754

->

更改 SMTP 设置以便在其他客户端上查看 Gmail – Gmail帮助

后来发现收到邮件通知了:

点击: 

allowing access to less secure apps

Less secure apps

点击Allow less secure apps,从OFF变成ON

Some apps and devices use less secure sign-in technology, which makes your account more vulnerable. You can turn off access for these apps, which we recommend, or turn on access if you want to use them despite the risks. Learn more

Let less secure apps use your account – Google Account Help

然后再去调试,用的是465端口,smtplib.SMTP_SSL:

if useSSL:

    smtpObj = smtplib.SMTP_SSL(smtpServer, smtpPort)

else:

    smtpObj = smtplib.SMTP(smtpServer, smtpPort)

    # start TLS for security

    # smtpObj.starttls()

smtpObj.set_debuglevel(1)

smtpObj.login(sender, senderPassword)

# smtpObj.sendmail(sender, receiversAddr, msgStr)

smtpObj.sendmail(sender, receiverList, msgStr)

logging.info(“Successfully sent email: message=%s”, msgStr)

就可以收到邮件了:

对应log:

send: ‘ehlo licrifandeMacBook-Pro.local\r\n’

reply: ‘250-smtp.gmail.com at your service, [180.117.243.76]\r\n’

reply: ‘250-SIZE 35882577\r\n’

reply: ‘250-8BITMIME\r\n’

reply: ‘250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH\r\n’

reply: ‘250-ENHANCEDSTATUSCODES\r\n’

reply: ‘250-PIPELINING\r\n’

reply: ‘250-CHUNKING\r\n’

reply: ‘250 SMTPUTF8\r\n’

reply: retcode (250); Msg: smtp.gmail.com at your service, [180.117.243.76]

SIZE 35882577

8BITMIME

AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH

ENHANCEDSTATUSCODES

PIPELINING

CHUNKING

SMTPUTF8

send: ‘AUTH PLAIN AGNyaWZhbjIwMDNAZ21haWwuY29tACoxbGltYW9saW4xKg==\r\n’

reply: ‘235 2.7.0 Accepted\r\n’

reply: retcode (235); Msg: 2.7.0 Accepted

send: u’mail FROM:<[email protected]> size=645\r\n’

reply: ‘250 2.1.0 OK k130sm7993000pfc.100 – gsmtp\r\n’

reply: retcode (250); Msg: 2.1.0 OK k130sm7993000pfc.100 – gsmtp

send: u’rcpt TO:<[email protected]>\r\n’

reply: ‘250 2.1.5 OK k130sm7993000pfc.100 – gsmtp\r\n’

reply: retcode (250); Msg: 2.1.5 OK k130sm7993000pfc.100 – gsmtp

send: ‘data\r\n’

reply: ‘354  Go ahead k130sm7993000pfc.100 – gsmtp\r\n’

reply: retcode (354); Msg: Go ahead k130sm7993000pfc.100 – gsmtp

data: (354, ‘Go ahead k130sm7993000pfc.100 – gsmtp’)

send: ‘Content-Type: text/plain; charset=”utf-8″\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: base64\r\nFrom: =?utf-8?b?56eR55Ge5YiGR21haWwgPGNyaWZhbjIwMDNAZ21haWwuY29tPg==?=\r\nTo: =?utf-8?b?56S86LKMUVEgPDg0NDgzNDIzQHFxLmNvbT4=?=\r\nSubject: =?utf-8?b?W+mrmOS7t+agvF0gdGl0bGXkuK3mlocgTWFkZGVuIE5GTCAxNiBmb3IgWGJv?=\r\n =?utf-8?q?x_One?=\r\n\r\nW+mrmOS7t+agvF0gdGl0bGXkuK3mlocgTWFkZGVuIE5GTCAxNiBmb3IgWGJveCBPbmUKICAgICAg\r\nICBOb3QgYnV5IGh0dHBzOi8vd3d3Lm1pY3Jvc29mdC5jb20vZW4tdXMvc3RvcmUvZC9tYWRkZW4t\r\nbmZsLTE2LWZvci14Ym94LW9uZS85M3M1dzZiZGcyNTAvbGdrcyB0aXRsZeS4reaWhyBNYWRkZW4g\r\nTkZMIDE2IGZvciBYYm94IE9uZSBmb3IgY3VycmVudCBwcmljZSAkMjk5LjAwIGV4cGVjdGVkIHBy\r\naWNlICQxOTkuMDAK\r\n.\r\n’

reply: ‘250 2.0.0 OK 1513251721 k130sm7993000pfc.100 – gsmtp\r\n’

reply: retcode (250); Msg: 2.0.0 OK 1513251721 k130sm7993000pfc.100 – gsmtp

data: (250, ‘2.0.0 OK 1513251721 k130sm7993000pfc.100 – gsmtp’)

2017/12/14 07:42:03 LINE 753  INFO    Successfully sent email: message=Content-Type: text/plain; charset=”utf-8″

MIME-Version: 1.0

Content-Transfer-Encoding: base64

From: =?utf-8?b?56eR55Ge5YiGR21haWwgPGNyaWZhbjIwMDNAZ21haWwuY29tPg==?=

To: =?utf-8?b?56S86LKMUVEgPDg0NDgzNDIzQHFxLmNvbT4=?=

Subject: =?utf-8?b?W+mrmOS7t+agvF0gdGl0bGXkuK3mlocgTWFkZGVuIE5GTCAxNiBmb3IgWGJv?=

=?utf-8?q?x_One?=

W+mrmOS7t+agvF0gdGl0bGXkuK3mlocgTWFkZGVuIE5GTCAxNiBmb3IgWGJveCBPbmUKICAgICAg

ICBOb3QgYnV5IGh0dHBzOi8vd3d3Lm1pY3Jvc29mdC5jb20vZW4tdXMvc3RvcmUvZC9tYWRkZW4t

bmZsLTE2LWZvci14Ym94LW9uZS85M3M1dzZiZGcyNTAvbGdrcyB0aXRsZeS4reaWhyBNYWRkZW4g

TkZMIDE2IGZvciBYYm94IE9uZSBmb3IgY3VycmVudCBwcmljZSAkMjk5LjAwIGV4cGVjdGVkIHBy

aWNlICQxOTkuMDAK

完整代码请参考:

https://github.com/crifan/crifanLib/blob/master/python/crifanLib.py

转载请注明:在路上 » 【记录】python中smtp发送gmail邮箱

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.191 seconds, using 22.21MB memory