I tried to start a smtp server with python with the following code:
import smtplib
smtp_server = smtplib.SMTP('localhost')
And I get the following error:
File "test.py", line 2, in <module>
smtp_server = smtplib.SMTP('localhost')
File "/usr/local/lib/python2.7/smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "/usr/local/lib/python2.7/smtplib.py", line 302, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/local/lib/python2.7/smtplib.py", line 277, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/local/lib/python2.7/socket.py", line 571, in create_connection
raise err
I tried to telnet localhost 25 and telnet localhost but they both give the following error:
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
telnet: Unable to connect to remote host: Connection refused
The same python code runs perfectly well on one of my other machines.
Any advice ?
Thanks.
UPDATE:
The machine I'm having problem with is running Python 2.7.2, while the machine that the code works well is running Python 2.6.2. I don't know if this is one of the reasons.
smtplib is SMTP protocol client. What you are looking for is smtpd.
The problem is that smtplib does not provide an SMTP server implementation, it implements SMTP client. Most likely, the code tries to connect to localhost and fails. See http://docs.python.org/2/library/smtplib.html
Is the other machine you write about already running an SMTP server?
And what are you trying to do?
Related
I have written a script to send email using python through "smtp.office365.com" and its working fine on my Desktop machine but when I an running same script on VM its giving below error:
<class 'socket.error'>
[Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
[('C:\\Users\\Administrator\\.jenkins\\workspace\\xxxxx\\Libraries\\NewEmailUtility.py', 190, 'sendEmail', 'server.connect(SMTPEMAILHOST,SMTPEMAILPORT)'), ('C:\\Python27\\lib\\smtplib.py', 317, 'connect', 'self.sock = self._get_socket(host, port, self.timeout)'), ('C:\\Python27\\lib\\smtplib.py', 292, '_get_socket', 'return socket.create_connection((host, port), timeout)'), ('C:\\Python27\\lib\\socket.py', 575, 'create_connection', 'raise err')]
Traceback (most recent call last):
File "C:\Users\Administrator\.jenkins\workspace\xxxxx\Libraries\SendEmail.py", line 25, in <module>
emailUtil.sendEmail(mailSubject,mailbody)
File "C:\Users\Administrator\.jenkins\workspace\xxxxx\Libraries\NewEmailUtility.py", line 198, in sendEmail
raise Exception("Unable to connect to EmailHost: ", SMTPEMAILHOST)
Exception: ('Unable to connect to EmailHost: ', 'smtp.office365.com')
I am using below code to send email:
SMTPEMAILHOST = "smtp.office365.com"
SMTPEMAILPORT = 587
server = smtplib.SMTP()
server.connect(SMTPEMAILHOST,SMTPEMAILPORT)
server.ehlo()
server.starttls()
server.login(SMTPUSERNAME,SMTPPASSWORD)
server.sendmail(self.From, msg["To"].split(", "), msg.as_string())
Please note i have tried various things mentioned on this link
Example:
Changing the Command prompt runas administrator and then try to
execute the script.
Changing the port to 25 or 995 or 465(tried SSL
also) as mentioned on different forums.
Checked their is not Firewall setting present and antivirus is also not running on VM.
In my python file I am sending mail from gmail:
I used
server = smtplib.SMTP("smtp.gmail.com", 587)
I am successfully sending mails from my local system,
I executed same code in the web server I got this error:
File "/usr/lib/python2.4/smtplib.py", line 244, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.4/smtplib.py", line 306, in connect
raise socket.error, msg
socket.error: (97, 'Address family not supported by protocol')
How to send mails using gmail in web server using python?
Try SMTP_SSL, based on the below it'll probably help:
https://docs.python.org/2/library/smtplib.html
I am using simple python lib for the SMTP
But i am getting this error:
import smtplib
smtpObj = smtplib.SMTP('localhost')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python2.7/smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.7/smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 111] Connection refused
Using python-2.7
Start a simple SMTP server with Python like so:
python -m smtpd -n -c DebuggingServer localhost:1025
or you can also try gmail smtp setting
server = smtplib.SMTP(host='smtp.gmail.com', port=587)
if you:
python -m smtpd -n -c DebuggingServer localhost:1025
as suggested by Allen Thomas, then make sure you init:
server = smtplib.SMTP(host='localhost', port=1025)
with matching port number, here: 1025.
You need to Enable the Mode of Protocol you are using.Then it will create Bidirectional interaction virtual connection between your device and script.
For Example the following script will require Telnet Mode to be Enabled so that it can call to another Device he is requesting for.
telA = connect(a)
telA.write("xcom Dial Number:43062#10.47.8.108\n")
time.sleep(10)
quit_telnet(telA)
print telA.read_all()
I've been trying to connect to my Gmail account using python. imap is enabled.
import imaplib
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
# also tried imap_server = imaplib.IMAP4_SSL("imap.gmail.com"), doesnt work.
Traceback is :
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imap_server = imaplib.IMAP4_SSL("imap.gmail.com",993)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1202, in __init__
IMAP4.__init__(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 172, in __init__
self.open(host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1217, in open
IMAP4.open(self, host, port)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 248, in open
self.sock = self._create_socket()
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 1205, in _create_socket
sock = IMAP4._create_socket(self)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/imaplib.py", line 238, in _create_socket
return socket.create_connection((self.host, self.port))
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 435, in create_connection
raise err
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/socket.py", line 426, in create_connection
sock.connect(sa)
OSError: [Errno 65] No route to host
What OSError: [Errno 65] No route to host means is what it say: you can't get to that machine from your machine.
You can test that from outside of Python by opening up a terminal/DOS prompt and typing this:
ping imap.gmail.com
It's possible that this is actually a name lookup error, and you're somehow getting a bad address for imap.gmail.com. So, just to be sure, check by IP address too:
ping 74.125.129.108
ping 74.125.129.109
If ping works, you can check whether your router is for some reason just blocking TCP access to the host, e.g., with:
telnet imap.gmail.com
If it's working, this should either hang for a long time, or give you a connection-refused error; if it gives you a no-route-to-host error, it's the same problem you're seeing.
It's also possible that your router is specifically blocking port 993. You can test this too:
telnet imap.gmail.com 993
If it doesn't come back with something like "Connected to gmail-imap.l.google.com", same problem here too.
At any rate, once you've verified that this is a system or network configuration problem, not a programming problem, go ask for help with your system on the appropriate site.
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 = '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
the connection refused error is my problem. i've looked online but i can't figure out how to connect it.
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..
Do you have a smtp server running in your computer? Localhost refers to your own computer.