从Python Script发送电子邮件

时间:2016-06-29 22:11:53

标签: python automation

我正在尝试从python脚本发送电子邮件。我正在使用这个例子,但不知何故代码继续运行并确实回馈任何东西。

import smtplib

to = 'receiver'
gmail_user = 'username'
gmail_pwd = 'password'
smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo()
smtpserver.login(gmail_user, gmail_pwd)
header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
print header
msg = header + '\n this is test msg from mkyong.com \n\n'
smtpserver.sendmail(gmail_user, to, msg)
print 'done!'
smtpserver.close()

我还启用了Google帐户中允许不太安全的应用。我在中断此代码时遇到的错误是:

Traceback (most recent call last):
  File "/Users/kgandh003c/PycharmProjects/kandarp_exercise/kandarp.py", line 6, in <module>
    smtpserver = smtplib.SMTP("smtp.gmail.com", 587)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 256, in __init__
    (code, msg) = self.connect(host, port)
  File "/Library/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 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 291, in _get_socket
    return socket.create_connection((host, port), timeout)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 60] Operation timed out

如果有人遇到这个问题,请告诉我这可能有什么问题。

由于

0 个答案:

没有答案