as indicated by the title I am having trouble sending an email via my gmail account through a python application.I have searched online for a solution but nothing seems to solve it and I thought I might ask here.
My code is the following:
FROMADDR = "myemail#gmail.com"
LOGIN = FROMADDR
PASSWORD = "mypass"
TOADDRS = "varis81#hotmail.com"
msg = "Test message"
server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()
print "E-mail succesfully sent"
I get the message:
socket.error: [Errno 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
I tried different ports but it doesn't work also.I also tried hotmail but it causes the same problem.I am using Python 2.7 (don't ask :) ) on a Windows 7 machine developing on Eclipse using PyDev.
Any help would be great!
Thank you in advance.
I'm using the same construct on one of my servers. My code is below.
The only difference is the extra .ehlo() after '.starttls()`. This should not be the issue; from the RFC:
5.2 Result of the STARTTLS Command
The client SHOULD send an EHLO command as the first
command after a successful TLS negotiation.
According to the RFC, the server should not sever a connection if the client does not send ehlo after starttls, but Google could be more restrictive on their SMTP server. I'd check that first. (I've seen providers tighten down on these kinds of conditions to reduce spam, see Mailinator's 2007 writeup for instance.)
It could also be filtered ports - try running the code in the REPL and confirm which line is exceptioning, if it's the connect() you'll know it's network. If it's after, it's likely your usage of smtplib.
Of note, I also experienced occasional unclean shutdowns, resulting in the try/except around .close().
import smtplib
s = smtplib.SMTP()
s.connect("smtp.gmail.com")
s.ehlo()
s.starttls()
s.ehlo()
s.login("from#gmail.com", "frompass")
s.sendmail("fromname#gmail.com", toAddr, bytes)
try:
s.close()
except: pass
Well, since I cant post comments yet I'll have to attempt an answer..
Judging by this: Python SMTP Errno 10060
Perhaps a timeout would help?
Related
I am trying to send an email with python, because I have to send many emails in bulk and would like to do so with python. The code that I have written is below. When I run this however, I get the following error
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
I think this might be a problem with the setup in Outlook, but I am unsure.
import smtplib
with smtplib.SMTP('smtp.outlook365.com',587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login('email#address.com','password')
subject = 'This is a test.'
body = 'wonder if this works!'
msg = f'Subject: {subject}\n\n{body}'
smtp.sendmail('sender#email.com','receiver#email.com',msg)
Check the address of the smtp server, referring to the Microsoft documentation I saw that the address is different.
SMTP server name smtp.office365.com
https://support.microsoft.com/en-us/office/pop-imap-and-smtp-settings-for-outlook-com-d088b986-291d-42b8-9564-9c414e2aa040
I am trying to send emails with gmail using the smtp module for an amazon price tracker. A few days ago, it was working perfectly but today it came up with this error:
File "C:\Users\61409\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 755, in starttls
raise SMTPNotSupportedError(
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
This is the code that originally worked but gives the error:
import smtplib
def send_email():
server = smtplib.SMTP('smtp.gmail.com', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login('xxxxxxxx#gmail.com', '*password*')
subject = f'The price of your product has fallen'
body = f'Check the amazon link - {URL}'
msg = f"Subject: {subject}\n\n{body}"
server.sendmail(
'xxxxxxx#gmail.com',
'xxxxxxx#gmail.com',
msg
)
print('The email has been sent')
server.quit()
I have tried making another gmail account and using SSL (importing ssl too) but the other gmail account comes up with the same problem and when using SSL, it comes up with a SMTP AUTH problem (unable to solve this too). I have allowed less secure apps but the same problem still occurs. I have also tried removing the first server.ehlo() and I have also tried using the server.set_debuglevel(1) but both do not work. I have also tried using with stmplib.SMTP(*Host*, *Port*) as server: and I have tried just using SMTP without using SSl and TLS at all but both answers do not work. Most of the answers removed the smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server. but other problems occured such as smtplib.SMTPServerDisconnected: Connection unexpectedly closed which happened numerous times with other answers. I tried researching on solutions to the connection unexpectedly closing but none of those answers worked too.
These are the answers I have tried:
SMTPException: STARTTLS extension not supported by server
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server
Python 2: SMTPServerDisconnected: Connection unexpectedly closed
Python3 SMTP 'Connection unexpectedly closed'
https://www.reddit.com/r/Python/comments/5grxy8/python_smtplib_connection_unexpectedly_closed/daumi2m/
How to send an email with Python?
How can I fix this problem?, am I missing something?? Will I need to use another module or library to send emails??? I apologise if this is a repeat question with a viable answer. I also apologise if one of the answers I mentioned as not working does actual work except I screwed up the code.
smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server.
The SMTP connection to the server does not seem to offer the STARTTLS extension, at least as seen from the perspective of the client. This extension is announced inside the response to the initial EHLO command of the client:
$ telnet smtp.gmail.com 587
...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP h9sm19557267wre.24 - gsmtp
EHLO mail.example.com
250-smtp.gmail.com at your service, ...
250-SIZE 35882577
250-8BITMIME
250-STARTTLS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
...
Given that smtp.gmail.com definitely supports this extension it is likely that there is some man in the middle somewhere in path of the connection, which rewrites the traffic to something like this:
220 smtp.gmail.com ESMTP h9sm19557267wre.24 - gsmtp
EHLO mail.example.com
250-smtp.gmail.com at your service, ...
250-SIZE 35882577
250-8BITMIME
250-XXXXXXXA <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
...
Here the response of the server is changed to no longer show support for STARTTLS, in the hope that the client continues without encryption and thus the mail can be analyzed be the man in the middle. Such man in the middle are typically corporate firewalls like Cisco ASA but can also be ISP or local antivirus products.
A few days ago, it was working perfectly but today it came up with this error:
Check if there were any changes regarding local security products (antivirus) or its settings or if there are changes to your environment (corporate firewall, different network you use ...).
I had the same problem: smtplib working properly and then returning this same error, without any change in the server (outlook).
It just so happens that I had refactored my code, and the function that sent the email was deeper in the modules.
If I used the old way, it worked!
When I turned off the anti-virus (AVG in my case), all worked just fine!
So, for some reason, the code refactoring made the sending procedure to be caught by anti-virus.
I want to try hiding my main IP and send email using Hotmail SMTP via Python SMTP Lib. I'm using the following code to send email but its sent from my IP (Originally it sends from Hotmail SMTP but it still shows my IP in the header if can see).
import smtplib
SMTP = "smtp.live.com"
s = smtplib.SMTP(SMTP,587)
s.ehlo()
s.starttls()
s.ehlo()
s.login("username#hotmail.com", "Password")
s.sendmail("username#hotmail.com", "recipient#hotmail.com","Hello World")
Let me know if there's any thing possible from which I can hide my IP and use the proxy IP to send the email. Furthermore, I think playing with socket/socks library might do the trick but still unsure.
Thank you very much.
I was trying to build a python script which could send emails via Gmail.
When I try to connect to the Gmail Server, I get an error Errno 10013.
This is what I'm trying to do:
gmail_message = smtplib.SMTP('smtp.gmail.com')
gmail_message.starttls()
gmail_message.login('xyz#gmail.com','xyzpassword')
gmail_message.sendmail('xyz#gmail.com',['xyz#gmail.com'], msg.as_string())
gmail_message.quit()
Error:
error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
What can I do to fix this?
You may need to specify the port by
gmail_message = smtplib.SMTP(host='smtp.gmail.com', port=587)
This is code that worked for me on Win7. It's pretty much identical to yours.
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
for comp in comps:
msg = "Subject:CommenceSweep:Comp%s-%s\n\n" % (comp,sweep)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Note: Did you correctly configure Gmail to allow SMTP?
I am attempting to connect to the Gmail SMTP mail server and perform tasks as outlined by the skeleton code given to me. Only the use of sockets is allowed (so not the smtplib). I need to: send HELO command, MAIL FROM, RCPT TO, and DATA.
There are many cases of similar problems posted, but they haven't received the proper answer. For example:
Implementing Transport Layer Security in Python - Simple Mail Client
The program is required to connect to smtp.gmail.com over port 587. I've taken two different approaches:
Using STARTTLS:
mailserver = 'smtp.gmail.com'
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 587))
recv = clientSocket.recv(1024)
print recv
if recv[:3] != '220':
print '220 reply not received from server.'
#Send HELO command and print server response
heloCommand = 'HELO Alice\r\n'
clientSocket.send(heloCommand)
recv1 = clientSocket.recv(1024)
print recv1
if recv1[:3] != '250':
print '250 reply not received from server.'
#Send MAIL FROM command and print server response.
command = "STARTTLS\r\n"
clientSocket.send(command)
recvdiscard = clientSocket.recv(1024)
print recvdiscard
clientSocket.send("MAIL From: email\r\n")
recv2 = clientSocket.recv(1024)
print recv2
if recv2[:3] != '250':
print '250 reply not received from server.'
Using SSL:
clientSocketSSL = ssl.wrap_socket(clientSocket)
Then clientSocketSSL replaces all instances of clientSocket. The STARTTLS lines are also removed and import ssl is added to the top.
When using the first method, the MAIL FROM: command isn't returning anything. I'm getting the following output:
250 mx.google.com at your service
220 2.0.0 Ready to start TLS
250 reply not received from server.
When using SSL, I'm getting the same as the linked post:
ssl.SSLError: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
Am I missing something here? I guess my best option is to use TLS but I have no idea how to go about that... is there something wrong with my MAIL FROM command?
When using SSL, you need to connect to port 465 instead of port 587. If you use STARTTLS, you still need to use ssl.wrap_socket, you just do it later - specifically, after receiving the 220 response to the STARTTLS command. After doing STARTTLS, you're supposed to do HELO again, since the server is supposed to forget anything that happened before the STARTTLS.
In either case, the servers at smtp.google.com ports 465 and 587 still won't return a 250 response to the MAIL command, since they require that you are authenticated before you send mail. You'll get a 530 response instead. You'll need to use the AUTH command with your gmail.com credentials to authenticate before you can use MAIL successfully on those servers.
If you don't want to authenticate, and depending on the details of what you need to do, you could try using port 25 of the server found in gmail.com's MX record. At the moment, the server is gmail-smtp-in.l.google.com and supports STARTTLS.
After STARTTLS, call
clientSocket = ssl.wrap_socket(clientSocket)