1

My issue is that when I run the following code, I get a WinError 10061, and from all of my searching it looks like this is a result of the foreign machine not being set up properly, but I assume that Google has that taken care of for gmail, so the error lies on my side. All of the other examples I could find were using localhost and getting this error, and it was because they did not have a local mail server set up. Would that still be the case with this problem? I am sure I am missing something obvious. Also, the error code is in full. Thank you in advance!

import smtplib

fromaddr = 'email@email.com'
toaddrs  = 'otheremail@email.com'
msg = 'Random stuff!'


username = 'username'
password = 'pass'

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

Error:

Traceback (most recent call last):
  File "C:/Users/Brett/PycharmProjects/Texting/sendMessage.py", line 13, in <module>
    server = smtplib.SMTP('smtp.gmail.com')
  File "C:\Python33\lib\smtplib.py", line 238, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python33\lib\smtplib.py", line 317, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python33\lib\smtplib.py", line 288, in _get_socket
    self.source_address)
  File "C:\Python33\lib\socket.py", line 424, in create_connection
    raise err
  File "C:\Python33\lib\socket.py", line 415, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
4

1 に答える 1

0

過去に Gmail SMTP サーバーを使用したことがありますが、SMTP ポートの番号が実際にはデフォルトで Gmail に適切な番号になっていることだけを指摘できます。そのため、ポート引数 (サーバー名にコロンを付けることもできます) は省略します。このことを考えると、おそらく別のマシンを試して、ネットワーク エラーなどが発生していないことを確認する必要があります。また、ポート 587 では ID チェックを行う必要があります。これは、Gmail がスパムやその他のナンセンスであると判断する可能性があることを意味します。ポート番号が入力されていない場合のデフォルトであるポート 25 では、このようなチェックは実行されません。

于 2013-08-09T00:06:13.007 に答える