2

i've looked online but every time i try to connect to the localhost it says connection refused. Do i need to sign into a valid email address to send email? Here is my EXACT code.

>>> import smtplib >>> sender = '[email protected]' #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 = ['[email protected]'] #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 

the connection refused error is my problem. i've looked online but i can't figure out how to connect it.

2
  • 2
    Do you have an SMTP server listening on your localhost? Commented Dec 18, 2011 at 15:48
  • Are you really sure that you run a local SMTP server? Otherwise you have either run one or use some other SMTP-server like your ISP:s or something... If you just want to send some e-mails I suggest you get yourself a gmail account and use a code like this: nixtutor.com/linux/send-mail-through-gmail-with-python Commented Dec 18, 2011 at 15:49

2 Answers 2

2

If you want to use gmail to send your message there is some code at: http://www.nixtutor.com/linux/send-mail-through-gmail-with-python/ that you can use. It should be pretty self explaining..

Sign up to request clarification or add additional context in comments.

3 Comments

thank you so much it worked. so do i have to login to an account everytime i use an email address that is not my local host (i.e. is it possible to send an email without signing into an email account)?
Glad it worked. You have to login everytime you send via Gmail smtp-servers (but not for every mail you send). But otherwise, there are SMTP servers that you could use to send without logging in. But your ISP would probably block them (except maybe there own) to prevent spam.
Or to be more specific, your ISP will probably block outgoing tcp-connections to port 25, which is the old school standard SMTP port. A connection to a smtp-server which listen on port 25 will in most cases be unencrypted and without login. That's (simplified) why gmail use port 587 as you see in the code you use (numbers after a domainname and a colon is often a port).
2

Do you have a smtp server running in your computer? Localhost refers to your own computer.

3 Comments

i dont think so, but i don't know enough about this to say if i do or dont. If i don't have a server running on my computer does that mean it's impossible to send an email, or is there another way (eventually i want to be able to send to a valid gmail address like [email protected])?
btw, by saying localhost refers to my own computer does that mean i'm supposed to use something other than s = smtplib.SMTP('localhost')? Because that is exactly my code in my program, localhost is not a variable it is what i actually put in (my code is EXACTLY s = smtplib.SMTP('localhost')
Just type in your ISP's smtp server address instead of localhost. Right now it tries to connect to your own computer but because you haven't set up a smtp server it fails and that's why you get this error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.