繁体   English   中英

如何使用python发送电子邮件?

[英]how do i send an email in python?

我已经上网了,但是每次我尝试连接到本地主机时都说连接被拒绝。 我需要登录有效的电子邮件地址来发送电子邮件吗? 这是我的确切代码。

>>> 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

连接被拒绝的错误是我的问题。 我已经看过网上了,但我不知道如何连接它。

您的计算机中是否有运行的SMTP服务器? Localhost是指您自己的计算机。

如果要使用gmail发送消息,则可以使用位于http://www.nixtutor.com/linux/send-mail-through-gmail-with-python/的一些代码。 它应该是很自我解释的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM