Python Smtplib WinError 100022 - python

I'm trying to send an email using Python and I used the material I found on the official site (https://docs.python.org/3/library/email.examples.html) but I'm getting an error when sending an email. Using the port and server works when I use Java but when using the same details in Python the code doesn't work.
The code that I am testing:
import smtplib
from email.message import EmailMessage
port = 25
server = "172.xxx.xxx.xxx"
from_add = "test#fakemail.com"
to_add = "test#fakemail.com"
message = "This is a test Email"
subject = "Test Email"
msg = EmailMessage()
msg.set_content(message)
msg["Subject"] = subject
msg["From"] = from_add
msg["To"] = to_add
#Send the message via our own SMTP server
s = smtplib.SMTP(server)
s.send_message(msg)
s.quit()
When I run my code I get the below error. I have been searching the net and all I am finding are errors concerning sockets (I tried adding which I haven't seen in any examples. My firewall admin tells me he doesn't see any traffic being blocked his end. Does anyone know if what I am coding is wrong?
Original
Traceback (most recent call last):
File "X:/test/test_email.py", line 21, in <module>
s = smtplib.SMTP(server)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 336, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "X:\Enviroments_For_Python\PROD\lib\smtplib.py", line 307, in _get_socket
self.source_address)
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 728, in create_connection
raise err
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 711, in create_connection
sock = socket(af, socktype, proto)
File "X:\Enviroments_For_Python\PROD\lib\socket.py", line 151, in __init__
_socket.socket.__init__(self, family, type, proto, fileno)
OSError: [WinError 10022] An invalid argument was supplied
Process finished with exit code 1
Socket change (didn't work)
s.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sock.bind(server, port)

I found the problem. I am using Anaconda and had created a different environment. When I switched to the root environment my code worked without a problem.

Related

TimeoutError when using python to send email

I'm trying to write a python script to send an email. And here is the code:
def main():
import smtplib
import email.mime.text
import email.mime.application
from email.mime.multipart import MIMEMultipart
from_email = 'xxx#gmail.com'
from_email_pwd = '123456'
to_email = 'yyy#gmail.com'
msg = MIMEMultipart()
msg['Subject'] = 'Subject'
msg['From'] = from_email
msg['To'] = to_email
body = email.mime.text.MIMEText("Body")
msg.attach(body)
s = smtplib.SMTP('smtp.gmail.com')
s.starttls()
s.login(from_email, from_email_pwd)
s.sendmail(from_email, to_email, msg.as_string())
s.quit()
return
main()
While I can successfully run this code on my personal laptop, I came across problems when I try to run it on the company's desktop (working on PyCharm IDE with interpreter Python 3.5.1). Here is the error message:
Traceback (most recent call last):
File "C:/PATH/stack.py", line 29, in <module>
main()
File "C:/PATH/stack.py", line 21, in main
s = smtplib.SMTP('smtp.gmail.com')
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 251, in __init__
(code, msg) = self.connect(host, port)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 335, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 306, in _get_socket
self.source_address)
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 711, in create_connection
raise err
File "C:\Username\AppData\Local\Programs\Python\Python35-32\lib\socket.py", line 702, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Process finished with exit code 1
Any thoughts what's going on?

[PYTHON]Email and file connection error

I tried to find some topic related, found some, tried some suggested solution, but seems not to work.
I'm trying to figure out where I did it wrong, I'm basically listing a directory and putting the output into a text file that I want to send via smtp to dest address.
#!/usr/bin/python
import os, sys
import smtplib
from email.mime.text import MIMEText
# GRABE LISTED DIRECTORY AND CREATE THE FILE #
#open folder and list it
Winpath = "C:\"
dirs = os.listdir(Winpath)
#Put it into file
fo = open("activitygraber.txt", "w+")
for file in dirs:
fo.write(file + "\n")
# EMAIL IT TO GIVEN ADDRESS #
msg = MIMEText(fo.read())
fo.close()
msg["Subject"] = "Test ActivityGrab"
msg["From"] = "test#test.me"
msg["To"] = "dest#dest.com"
s = smtplib.SMTP("localhost")
s.sendmail("test#test.me", "dest#dest.com", msg.as_string())
s.quit()
That's simple code mostly from Python documentation (I'm in the learning curve).
The file is being created and ready to send I guess, but somehow the connection is not being created. I guess it's because my smtp port in not open ? should I tried to connect to a mail service ? then send it from there ?
Here the message I get when I try to send it:
Traceback (most recent call last):
File "C:\Users\PC\Desktop\testfile.py", line 33, in <module>
s = smtplib.SMTP("localhost")
File "C:\Python27\lib\smtplib.py", line 250, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 310, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 285, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 571, in create_connection
raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
Any help of hint would really be appreciated thanks !
Meanwhile I'm gonna continue to search the cause.
Cheers,
s = smtplib.SMTP("localhost") isn't going to work unless you have a running SMTP server on your local machine (which you probably don't...).
If you do, make sure it's listening on port 25.
If you don't, you will have to use a remote SMTP server. You can use google's if you have a gmail account, or find a free one that is set up as a mail relay (that means you can use it anonymously), but these are very rare these days.

error sending mail in python

pwd = "mypassword"
import smtplib
server = smtplib.SMTP('smtp.gmail.com')
server.ehlo()
server.starttls()
server.login("gvpcse113#gmail.com",pwd)
msg = "YOUR MESSAGE!"
server.sendmail("gvpcse113#gmail.com", "sender#xyz.com", msg)
server.quit()
I have tried sending a mail through python...
Error :
Traceback (most recent call last):
File "H:/my projects/PythonCourse/test_cont/mail_test4.py", line 4, in <module>
server = smtplib.SMTP('smtp.gmail.com')
File "C:\Python27\lib\smtplib.py", line 256, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 316, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 291, in _get_socket
return socket.create_connection((host, port), timeout)
File "C:\Python27\lib\socket.py", line 557, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
I am connecting through a proxy connection
I set the proxy through cmd in windows.
Please help me with this.
Update :
I am sure with the internet connection :
import urllib2
def internet_on():
try:
response=urllib2.urlopen('https://www.google.co.in',timeout=1)
return True
except urllib2.URLError as err: pass
return False
print internet_on()
Output is True
Your code works fine for me, so it's probably the connection settings.
Try changing server to:
server = smtplib.SMTP('64.233.184.108')
(that's the IP address of smtp.gmail.com, to bypass DNS resolution)
Try updating the server = smtplib.SMTP('smtp.gmail.com') to include a port. The port for gmail is 587, so add that as a second parameter: server = smtplib.SMTP('smtp.gmail.com', 587)
If this does not work, make sure that you have used this site to make sure that the account will let you in:
https://www.google.com/settings/security/lesssecureapps

Error Sending Email using Gmail with SMTP (Python 3)

I am trying to use SMTP in Python 3.4 to send emails but I am receiving the following error when I run my code:
Traceback (most recent call last):
File "C:/Users/Anna Hughes/Documents/Python Projects/2015.10.16/Mail Application.py", line 9, in <module>
server.login(user_email, user_password)
File "C:\Python34\lib\smtplib.py", line 613, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
Here is my code:
import smtplib
server = smtplib.SMTP("smtp.gmail.com:587")
user_email = "user#gmail.com"
user_password = "password"
recipient_email = "recipient#gmail.com"
msg = "Test."
server.login(user_email, user_password)
server.ehlo()
server.starttls()
server.sendmail(user_email, recipient_email, msg)
server.quit()
Thanks.
EDIT:
I have changed the second line to server = smtplib.SMTP("smpt.gmail.com, 587") as suggested. That seemed to fix the error, but I am now getting a new one:
Traceback (most recent call last):
File "C:/Users/Anna Hughes/Documents/Python Projects/2015.10.16/Mail Application.py", line 2, in <module>
server = smtplib.SMTP("smtp.gmail.com,587")
File "C:\Python34\lib\smtplib.py", line 242, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python34\lib\smtplib.py", line 321, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python34\lib\smtplib.py", line 292, in _get_socket
self.source_address)
File "C:\Python34\lib\socket.py", line 494, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
You can also try yagmail:
import yagmail
yag = yagmail.SMTP("user#gmail.com", "password")
yag.send("recipient#gmail.com", subject="sub", contents="Test.")
Install using pip install yagmail.
Lots of more tricks can be found on the github page, such as passwordless script.
I can help.
See here and turn on.
Then
server = smtplib.SMTP("smtp.gmail.com, 587")
server.starttls()
server.login("username","p455w0rd")
I think perhaps your problem lies with this: server = smtplib.SMTP("smtp.gmail.com:587")
I've got a few programs that send e-mails, and I use the following format instead:
server = smtplib.SMTP("smtp.gmail.com, 587")
If you can't see the difference at first, the colon has been changed to a comma.
Hope this fixes it!
Try this
import smtplib
Server=smtplib.SMTP('smtp.gmail.com',587)
Server.starttls()
Server.login("EMAIL_YOUR","YOUR_PASSWORD")
msg="Hello, Its the Message!"
Server.sendmail("whom_2_send#gmail.com","your_email#gmail.com",msg)
print("email send succesfully") #Just for confirmation
Server.quit()
try this:
server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(email, password, message)
server.quit()

Send email from server python

I am trying to send an email form my server with python, I asked my server provider what port to use for this and they said "choose SMTP Ports 465 (Secure SSL/TLS outgoing server) , 25 ( Non-SSL outgoing server)." Not sure what this exactly means but currently I am using 25, here is my code
#! /usr/bin/python
import smtplib
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 25)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
I filled in my username (which is my email address right) and password,a dn the target but it is not working, when I try to navigate to the url where my py script is, it just doesn't load. I have an internet connection cause I am loving other things, and go to other pages on my server. I have also tried running with cron jobs but that also doesn't work.
The permissions on the script are 0755, is there a problem with the script?
When i ran with cron jobs here is the traceback
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 6, in <module>
server = smtplib.SMTP('smtp.gmail.com', 25)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
Thanks for the help in advance.
EDIT
here is updated error log with the port at 587
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 7, in <module>
server = smtplib.SMTP('smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
raise error, msg
socket.error: [Errno 101] Network is unreachable
EDIT 2
When I had server = smtplib.SMTP('telnet smtp.gmail.com', 587)
I got this error
Traceback (most recent call last):
File "/home/spencerf/public_html/cgi-bin/send_email.py", line 8, in <module>
server = smtplib.SMTP('telnet smtp.gmail.com', 587)
File "/usr/lib64/python2.6/smtplib.py", line 239, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib64/python2.6/smtplib.py", line 295, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib64/python2.6/smtplib.py", line 273, in _get_socket
return socket.create_connection((port, host), timeout)
File "/usr/lib64/python2.6/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
BUG:
First of all you are using gmail server not your company server to send mail .For gmail server the output port is 587
The code:
Due to security issues gmail blocks accessing mail via code or program
But still you can use gmail to send mail via code if you do the following things
What i have done in code :
1.Added a error object to get the error message
import smtplib
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
#Next, log in to the server
server.login("youremailusername", "password")
#Send the mail
msg = "\nHello!" # The /n separates the message from the headers
server.sendmail("you#gmail.com", "target#example.com", msg)
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
print "Error: unable to send email"
If u ran this code u would see a error message like this stating that google is not allowing u to login via code
Things to change in gmail:
1.Login to gmail
2.Go to this link https://www.google.com/settings/security/lesssecureapps
3.Click enable then retry the code
Hopes it help :)
But there are security threats if u enable it
Updated
import smtplib
try:
content = 'test'
mail = smtplib.SMTP('smtp.gmail.com',587)
mail.ehlo()
mail.starttls()
mail.login("ABC#gmail.com", "password")
mail.sendmail("ABC#gmail.com", "recivermailaddress", content)
mail.quit
print "Successfully sent email"
except smtplib.SMTPException,error:
print str(error)
In python, please download yagmail (disclaimer: I'm the developer):
pip install yagmail
It is then simply a matter of:
import yagmail
yag = yagmail.SMTP("you#gmail.com", "password")
yag.send("target#example.com", 'This is the subject', 'Hello!')
I guess it mostly just has to do with the fact that you gave a wrong port (smtp.gmail.com is not at 25).
On the github you can also see a common list of errors.

Categories

Resources