XXX
XXX

Reputation: 333

STARTTLS extension not supported by server

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]

Upvotes: 24

Views: 77860

Answers (9)

Teodor Mihail
Teodor Mihail

Reputation: 906

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

Upvotes: 2

Vignesh Rao
Vignesh Rao

Reputation: 179

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')

Upvotes: 2

Andrea
Andrea

Reputation: 259

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)

Upvotes: 11

SunilThorat
SunilThorat

Reputation: 1748

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

server = smtplib.SMTP('smtp.abc.com:587')

Upvotes: 3

9jera
9jera

Reputation: 9

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

Upvotes: -3

Olexiy
Olexiy

Reputation: 555

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()

Upvotes: 6

Leonard Huang
Leonard Huang

Reputation: 173

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')

Upvotes: 14

jforberg
jforberg

Reputation: 6752

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.

Upvotes: 0

Ferran
Ferran

Reputation: 14993

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.

Upvotes: 3

Related Questions