最新消息:

使用PYTHON的SMTP模块用GMAIL发邮件失败的解决

python admin 6351浏览 0评论

基于之前种种重要时间点CUL 中美速递都没有邮件提示这一事实,我推断发货的时候他们也不会邮件通知我的,而且事实证明,我的推断是正确的,我在自己的网站上有一个脚本在检查他们的发货信息页面的变化来自己检查他们的发货进度,当检测到发货时给我自己发送一封邮件,这也是弥补CUL的垃圾服务的一种方式吧。

脚本的主要部分是这样的:

smtpServer = "smtp.gmail.com:25"
fromAddress = "your_from_address@email_server_domain.com"
toAddress = "target_address@email_server_domain.com"

msgHead = ["From:" + fromAddress , "To:" + toAddress, 'Subject:Nexus 7 being delivered']
msgBody = ['Nexus 7 being delivered']
msg = 'rnrn'.join(['rn'.join(msgHead), 'rn'.join(msgBody)])

def notifyMe():
s = smtplib.SMTP(smtpServer)
s.starttls()
s.login("your_user_name", "your_password")
s.sendmail(fromAddress, toAddress, msg)
s.quit()

脚本在我自己的机器上测试时可以正常运行的,于是就直接放到了网站上cron定时执行,昨天晚上开始,收到了cron发送的脚本执行错误报告:

File "/home/gu0gu0/public_html/<wbr>fetch_web_page.py", line 34, in &lt;module&gt;
notifyMe()
File "/home/gu0gu0/public_html/<wbr>fetch_web_page.py", line 15, in notifyMe
s = smtplib.SMTP(smtpServer)
File "/usr/lib64/python2.6/smtplib.<wbr>py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.<wbr>py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.<wbr>py", line 273, in _get_socket
return socket.create_connection((<wbr>port, host), timeout)
File "/usr/lib64/python2.6/socket.<wbr>py", line 567, in create_connection
raise error, msg
socket.error: [Errno 97] Address family not supported by protocol

在建立和服务器连接的过程中出现了错误,于是找到live chat的人询问,起初认为是他们配置的问题,后来经过他们调查,他们建议我换25端口,不知道他们的依据是什么,我在gamil的帮助里面还没看到为什么使用25端口可以成功,之后会再尝试找到合理的解释。

几个google的帮助文档,供各位参考:

My client isn’t accepting my username and password

转载请注明:爱开源 » 使用PYTHON的SMTP模块用GMAIL发邮件失败的解决

您必须 登录 才能发表评论!