473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

sending of mail (smtp) - connection refused - but smtp server isrunning!

I am using the smtp module to send emails via a local SMTP server on our network.
I am failing with "connection refused" error, even though we definitely have an
smtp server running on port 25!

the code is like this:
me = 'a*******@companyname.com'
you = 's*********@companyname.com'
msg['Subject'] = '*** alert'
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP('192.168.1.105')
s.connect()
s.sendmail(me, [you], msg.as_string())
s.close()

print "Email sent."

When I run this code, I get:
Traceback (most recent call last):
File "C:\Python23\check.py", line 58, in -toplevel-
s.connect()
File "C:\Python23\lib\smtplib.py", line 302, in connect
raise socket.error, msg
error: (10061, 'Connection refused')
here's me verifying that SMTP server is indeed alive at port 25 on my local
network:

$ telnet 192.168.1.105 25
Trying 192.168.1.105...
Connected to 192.168.1.105.
Escape character is '^]'.
220 grain.companyname.com ESMTP Server FTGate
helo
250 grain.companyname.com
any ideas what the problem could be? this usually happens when someone is not
aware they have to run an SMTP server, but we do have one running, as can be
seen above!

thanks
alex

Jul 18 '05 #1
4 25356
Alex Hunsley wrote:
I am failing with "connection refused" error, even though we definitely
have an smtp server running on port 25! .... any ideas what the problem could be? this usually happens when someone
is not aware they have to run an SMTP server, but we do have one
running, as can be seen above!


Not sure, but please try this at the interactive prompt and report
(or analyze) what it returns:
from socket import *
getaddrinfo('192.168.1.105', 25, 0, SOCK_STREAM)

This is something that smtplib.py is actually doing to get the
host:port combination that it uses to connect. It seems that
the only possible source for the failure you see is if it
does not return the expected values.

-Peter
Jul 18 '05 #2
Peter Hansen wrote:
Alex Hunsley wrote:
I am failing with "connection refused" error, even though we
definitely have an smtp server running on port 25!


...
any ideas what the problem could be? this usually happens when someone
is not aware they have to run an SMTP server, but we do have one
running, as can be seen above!

Not sure, but please try this at the interactive prompt and report
(or analyze) what it returns:
>>> from socket import *
>>> getaddrinfo('192.168.1.105', 25, 0, SOCK_STREAM)

This is something that smtplib.py is actually doing to get the
host:port combination that it uses to connect. It seems that
the only possible source for the failure you see is if it
does not return the expected values.

-Peter


Hi Peter
when I run the above interactively, I get:

[(2, 1, 6, '', ('192.168.1.105', 25))]

I've just discovered something interesting.
If I completely miss out connect() and close(), it works!
i.e. my code now reads:

s = smtplib.SMTP('192.168.1.105')
#s.connect()
s.sendmail(me, [you], msg.as_string())
#s.close()
and this works! Is it a bad state of affairs to leave things in?
thanks
alex

Jul 18 '05 #3
Alex Hunsley wrote:
I've just discovered something interesting.
If I completely miss out connect() and close(), it works!
i.e. my code now reads:

s = smtplib.SMTP('192.168.1.105')
#s.connect()
s.sendmail(me, [you], msg.as_string())
#s.close()
and this works! Is it a bad state of affairs to leave things in?


Oh, silly me! I should have seen this the first time.
This is exactly what you should expect, since the call
to the SMTP() constructor calls its self.connect() method
automatically if you specify the host argument!

The docs for smtplib say these things near the top:

'''If the optional host and port parameters are given, the
SMTP connect() method is called with those parameters during
initialization. '''

'''For normal use, you should only require the
initialization/connect, sendmail(), and quit() methods.
An example is included below.'''

To clean things up, you should be calling .quit() instead
of .close(), although in actual fact they basically do
the same thing. (Check the source in smtplib.py to learn
more.)

-Peter
Jul 18 '05 #4
On Tue, 29 Jun 2004 15:26:41 +0100, Alex Hunsley
<la**@tardis.ed.ac.molar.uk> wrote:

I am using the smtp module to send emails via a local SMTP server on our network.
I am failing with "connection refused" error, even though we definitely have an
smtp server running on port 25!

the code is like this:

me = 'a*******@companyname.com'
you = 's*********@companyname.com'
msg['Subject'] = '*** alert'
msg['From'] = me
msg['To'] = you

s = smtplib.SMTP('192.168.1.105')
s.connect()
s.sendmail(me, [you], msg.as_string())
s.close()

print "Email sent."

When I run this code, I get:

Traceback (most recent call last):
File "C:\Python23\check.py", line 58, in -toplevel-
s.connect()
File "C:\Python23\lib\smtplib.py", line 302, in connect
raise socket.error, msg
error: (10061, 'Connection refused')


Try this

s = smtplib.SMTP('192.168.1.105')
failed = s.sendmail(me, [you], msg.as_string())
s.quit()

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Josh Close | last post by:
I'm using the smtplib module and I keep getting this error: (111, 'Connection refused') What could be causing this? I've tried it from a different computer and it seems to work, but not from this...
9
by: B-Dog | last post by:
I've built a small app that sends mail through our ISP's SMTP server but when I try to send through my local exchange server I get CDO error. Does webmail use SMTP or does it strictly rely on...
4
by: bgs | last post by:
i'm trying to send mail from asp server in two ways, the first uses CDONTS.Newmail: <% Dim objCDOMail Set objCDOMail = Server.CreateObject("CDONTS.NewMail") objCDOMail.BodyFormat = 1 ...
2
by: satnamsarai | last post by:
Using System.Net.Mail: Sometimes I get error 'failure sending mail. Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host.' Not sure how...
2
by: Danny | last post by:
Hi all, Trying to send mail with System.Net.SmtpClient, using very simple code just for testing: SmtpClient smtp = new SmtpClient("mail.server.com", 25); smtp.Credentials = new...
3
by: raj200809 | last post by:
when i m sending mail i received error from symantec Antivirus" Your email message was unable to be sent because your mail server rejected the message 550-5.7.1 the ip you’re using to send mail is...
7
by: undbund | last post by:
Hi I am creating a newsletter system. The software should run from desktop computer (localhost) but be able to send email to anyone on the internet. Can you guys give me some ideas on how to...
4
by: Kapil Choubisa | last post by:
Hello all I am using smtp for sending mail. it ask the smtp host name. that is smtp.Host = "smtp.gmail.com"; what is for yahoo/rediff/hotmail is there any one for globle mail systems.
1
by: lekaasree | last post by:
hai, how to send a mail through a client machine to the server database , the client is a stand alone application which is designed in c sharp
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.