24

This maybe a repeated question but I'm still facing issues on this, hope there's a solution around. Thanks in advance.

I'm trying to send mail through the company's server

I'm currently using Python version 2.6 and Ubuntu 10.04

This is the error message I got

Traceback (most recent call last):

  File "hxmass-mail-edit.py", line 227, in <module>
    server.starttls()

  File "/usr/lib/python2.6/smtplib.py", line 611, in starttls
    raise SMTPException("STARTTLS extension not supported by server.") smtplib.SMTPException: STARTTLS extension not supported by server.

Here goes part of the code

server = smtplib.SMTP('smtp.abc.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login('[email protected]', 'abc123')
addressbook=sys.argv[1]
1
  • 1
    please post the snippet of your mail sending code. maybe it's just enough to remove "server.starttls()", but difficult to tell without the code
    – Gryphius
    Jun 15, 2011 at 9:18

9 Answers 9

14

Remove the ehlo() before starttls().

starttls() + ehlo() results in two HELLO messages, which cause the server remove the STARTTLS in the reply message.

server = smtplib.SMTP('smtp.abc.com', 587)
server.starttls()
server.ehlo()
server.login('[email protected]', 'abc123')
1
  • This does not work for yahoo. Yahoo work only when I remove startTLS. But I need TLS to ensure the password is not captured by sniffers.
    – Ajoy
    Mar 12, 2013 at 4:36
11

I had a similar issue trying to send a mail through the company's server (without autentication needed)

I solved removing the server.ehlo and removing the port number:

server = smtplib.SMTP("smtp.mycompany.com")
server.sendmail(fromaddr, toaddr, text)
0
6

removing server.ehlo() before server.starttls() helped me get my code working! Thank you, Leonard! my code:

s = smtplib.SMTP("smtp.gmail.com",587)
s.starttls()
s.ehlo
try:
    s.login(gmail_user, gmail_psw)
except SMTPAuthenticationError:
    print 'SMTPAuthenticationError'
s.sendmail(gmail_user, to, msg.as_string())
s.quit()
3

The error says it all, it seems the SMTP server sou are using doesn't support STARTTLS and you aru issuing server.starttls(). Try using the server without calling server.starttls().

Without more info is the only I can say.

3
  • thanks ferran, I've tried not using server.starttls(), it seems it didnt work out well returning a list of errors.
    – XXX
    Jun 15, 2011 at 9:26
  • it helped me. Thanks
    – Venkat M
    Jun 13, 2018 at 12:04
  • That may not work too. If you altogether skip that you may see something like smtplib.SMTPException: No suitable authentication method found. So you would still need to find out what the server uses for auth. Oct 16, 2018 at 1:29
3

I am able to resolve the issue with below code, by adding port number with server name:

server = smtplib.SMTP('smtp.abc.com:587')
1
  • 587 is the submission port
    – vy32
    Nov 22, 2018 at 16:41
2
from smtplib import SMTP_SSL, SMTP, SMTPAuthenticationError
from ssl import create_default_context
from email.message import EmailMessage

sender = '[email protected]'
description = "This is the test description supposed to be in body of the email."
msg = EmailMessage()
msg.set_content(description)
msg['Subject'] = 'This is a test title'
msg['From'] = f"Python SMTP <{sender}>"
msg['To'] = '[email protected]'


def using_ssl():
    try:
        server = SMTP_SSL(host='smtp.gmail.com', port=465, context=create_default_context())
        server.login(sender, password)
        server.send_message(msg=msg)
        server.quit()
        server.close()
    except SMTPAuthenticationError:
        print('Login Failed')


def using_tls():
    try:
        server = SMTP(host='smtp.gmail.com', port=587)
        server.starttls(context=create_default_context())
        server.ehlo()
        server.login(sender, password)
        server.send_message(msg=msg)
        server.quit()
        server.close()
    except SMTPAuthenticationError:
        print('Login Failed')
2

By testing and researching myself, I found out that the gmail servers do not use tls connections with python anymore.

You must not use service.startttls(). Gmail service do not support this type of connection anymore.

Also remember to use the SMTP ports (mail reserved ports) for sending emails. POP3 and IMAP ports for receiving email.


        s_u = "Test"

        service = smtplib.SMTP_SSL("smtp.gmail.com", 465)

        service.ehlo()

        service.sendmail("SENDER_EMAIL","RECEIVER_EMAIL","MESSAGE")

        

You can't send the email even if you put the correct credentials, look at this: Login credentials not working with Gmail SMTP

5
  • Great. And just for understanding, does SMTP_SSL with port 465 create the same levels of security as using starttls()?
    – Hefe
    Jun 22, 2022 at 14:18
  • 1
    No, it does not. I have done this because the gmail servers refuse to respond if I was using starttls(). So the problem is Gmail server related, not programming related Jun 24, 2022 at 14:09
  • Gotcha. I had the same problem when trying to use tls.
    – Hefe
    Jun 24, 2022 at 18:27
  • Yes, the google servers deprecated the TLS for Simple Mail Transfer Protocol. I tried and then I read some documentation somewhere (I forgot where), and then I experimented. During a deep debug session, I got a message from the server saying that the connection was closed. Then I tried using SMTP without TLS and it worked. I think if you want to use SMTPS with Gmail you have to use some kind of modern Google owned API. Try going on this route, if you deeply need SMTPS. Jun 24, 2022 at 22:41
  • 1
    Look, read this. Google made a Gmail Api that you can integrate within your Python application: developers.google.com/gmail/api/quickstart/python Jun 24, 2022 at 22:42
0

Are you sure that you want to encrypt (StartTLS) the connection to the mail server? I would contact someone who knows the insides of that server to see what protocol/encryption to use.

You say that upon removing the call to server.starttls(), you get a different series of error messages. Could you please post those messages as well?

Also, you might want to read up on StartTLS so you understand what it is and why you would want to use it. It seems you're writing a Serious Business program, in which case you'll probably want to understand what you are doing, security-wise.

0
-3

Yes putting server.starttls() above server.ehlo() solved this.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.