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?
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 was sending emails from flask-mail but since trying to use the mailservers at namecheap or bluehost, I'm getting the following error:
SSLError: [Errno 1] _ssl.c:510: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
So now I'm trying to send the email without flask-mail but I'm still getting the same error. Any fix?
My code is as follows:
from smtplib import SMTP
smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('xxxxxx', 26)
smtp.login('noreply#xxx.com', 'xxxxxxx')
from_addr = "xxx <noreply#xxx.com>"
to_addr = rec#xxx.com
subj = "hello"
date = datetime.datetime.now.strftime( "%d/%m/%Y %H:%M" )
message_text = "Hello\nThis is a mail from your server\n\nBye\n"
msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )
smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
My application is running on Ubuntu 14.04 on Amazon EC2.
Thanks.
The reason that this is giving you this error is because your mail server is not a SMTP server. Use Gmail or another smtp mail service to send the mail. Try sending it through a gmail account with the server being smtp.gmail.com and the port being 587. First though you will need to configure your account for it.
I have a server on app engine from which I'm trying to send a mail with an SMTP socket. The code goes something like this:
session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
session.starttls()
session.ehlo()
session.login(sender, sender_password)
session.sendmail(sender, recipient, msg.as_string())
session.quit()
and I get the error in the title on the line for "session.starttls()".
Any ideas on how to solve this?
Thanks
AppEngine does not permit apps to send mail directly. Use the AppEngine Mail Python API instead.
I use lib secure_smtpd for create SMTP server. When I generate certificate and use it for SSL connection I catch exception (for test I use Opera mail client and The Bat!):
SSLError: _ssl.c:489: The handshake operation timed out
When I test use python script everything is ok:
smtpObj = smtplib.SMTP_SSL('localhost',2000)
smtpObj.set_debuglevel(1)
smtpObj.login('testuser', '111111')
msg = MIMEMultipart('alternative')
msg['Subject'] = "my subj SSL"
msg['From'] = sender
msg['To'] = "username#site.com"
msg.attach(MIMEText("Hello world!",'html'))
smtpObj.sendmail(sender, [toemail], msg.as_string())
Can somebody help fix problem with handshake?
I use python 2.7.3
How you configured SMTP in Opera and The Bat! ? There is SSL mode (which you seems to use), where all connection is wrapped in SSL/TLS, and STARTTLS mode, where connection is plain but SSL/TLS excahnge is started after client issues STARTTLS command.
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?