Hi,
I have a internet connection at home. I have installed Python2.6.1 on my laptop in C:\Python26 directory. I have written a script named "TestUtil_" and my intension is to send a mail to the given mail id.

But when am running the script I am getting an error: "[Errno 10061] No connection could be made because the target machine actively refused it". Kindly help me resolve this issue.

TestUtil_SendMail.py:
import smtplib

print "EMAIL UTILITY!!!!!!!!!!!!!!!!!"
SERVER = "localhost"

FROM = ""
TO = [""] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

# Send the mail

server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, message)
server.quit()

print "Mail has been sent... Thanks"

--------------------------------------------------------------------

But while running I am getting the following Error:

>>>
EMAIL UTILITY!!!!!!!!!!!!!!!!!
Traceback (most recent call last):
File "C:/Python26/TestUtil_", line 25, in <module>
server = smtplib.SMTP(SERVER)
File "C:\Python26\lib\", line 239, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python26\lib\", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python26\lib\", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python26\lib\", line 512, in create_connection
raise error, msg
error: [Errno 10061] No connection could be made because the target machine actively refused it
>>>

------------------------------------------------------------------------------

Thanks,
Shakila.

Dani AI

Generated

the 10061 error is Windows saying “connection refused.” Your code points to SERVER="localhost", so Python tries to talk to an SMTP service on port 25 on your laptop. Because nothing is actually listening there (or a security tool blocked it), the OS refuses the TCP connection. is spot on: you either need a local SMTP server running, or you must connect to a real SMTP service. See Microsoft’s description of WSAECONNREFUSED for the exact wording. (learn.microsoft.com)

Fastest path: use an SMTP provider instead of localhost. For Gmail (similar idea for your ISP or your company’s Domino/Notes SMTP host), connect over TLS on port 587 and authenticate. Modern Gmail requires either OAuth2 or a 16‑digit App Password if you have 2‑Step Verification; regular account passwords are often blocked. Example with smtplib STARTTLS (works in both old Python 2.x and Python 3.x):

import smtplib

FROM = "you@gmail.com"
TO = ["you@gmail.com"]
msg = "Subject: Hello\r\n\r\nThis was sent with Python."

s = smtplib.SMTP("smtp.gmail.com", 587, timeout=30)
s.ehlo(); s.starttls(); s.ehlo()
s.login(FROM, "APP_PASSWORD")  # or OAuth2 flow
s.sendmail(FROM, TO, msg)
s.quit()

Gmail’s SMTP ports and TLS/SSL requirements are documented here; STARTTLS and login are documented in Python’s smtplib. App Passwords info here. (developers.google.com)

If you want to keep localhost for dev only, run a local catch‑all SMTP receiver like Papercut SMTP so your original code has something to connect to. Also, ’s point about AV suites is real: “email scanning” features can block SMTP. Finally, be aware many networks block outbound port 25; prefer 587 or 465. (Example: Azure blocks 25 by default, recommending authenticated submission on 587.) (papercut-smtp.com)

Recommended Answers

All 6 Replies

Does your laptop also have an open mail server listening on the default port? smptlib provides methods to connect to an smtp mail server, but if your laptop is not currently running one, then you cannot send mail to it.

No, I do not have any mail server listening in my laptop. Actually I woulld like to develop a small python utility which should automatically send mail to the given to address. I will be using this utility when I am in office or at home. So when I am in office I have a lotus notes client running in my system.

Could you please guide me on how to proceed. Do you want me to install any open mail server? If yes, please guide me how to do that.

Thanks,
Shakila.

By the way, Thanks a lot for your reply.

I've never set up a mail server. I suggest google.com

If you have mc Afee there is chance that it can block the port that SMTP uses.

Hi there, check this out, by the way the code that i explain in there it only works for gmail and googlemail, i think.
Also check this, and give us some feed back to the email address reffered.

Dan08.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.