我看过网上但每次尝试连接到localhost时都说连接被拒绝了。我是否需要登录有效的电子邮件地址才能发送电子邮件? 这是我的精确代码。
>>> import smtplib
>>> sender = 'from@fromdomain.com'
#this is my exact sender name bc i don't know if i need to use a valid email address or if i can just make up one since i dont need a password and username
>>> receiver = ['to@todomain.com']
#again, i dont know what to use for the receiver email address
>>> message = 'this is a test'
>>> s = smtplib.SMTP('localhost')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
s = smtplib.SMTP('localhost')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtplib.py", line 302, 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 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
raise err
error: [Errno 61] Connection refused
连接拒绝错误是我的问题。我看过网上但我无法弄清楚如何连接它。
答案 0 :(得分:2)
您的计算机上是否运行了smtp服务器? Localhost是指您自己的计算机。
答案 1 :(得分:2)
如果您想使用gmail发送消息,可以使用以下代码:http://www.nixtutor.com/linux/send-mail-through-gmail-with-python/。它应该是非常自我解释..