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('sales@abc.com', 'abc123')
addressbook=sys.argv[1]
Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
XXX
  • 333
  • 1
  • 2
  • 9
  • 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 '11 at 09:18

9 Answers9

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('sales@abc.com', 'abc123')
Leonard Huang
  • 173
  • 1
  • 2
  • 9
  • 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 '13 at 04: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)
Andrea
  • 249
  • 2
  • 7
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()
Ivan Ferić
  • 4,725
  • 11
  • 37
  • 47
Olexiy
  • 535
  • 1
  • 7
  • 14
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')
SunilThorat
  • 1,672
  • 2
  • 13
  • 15
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.

Ferran
  • 14,563
  • 2
  • 21
  • 12
  • thanks ferran, I've tried not using server.starttls(), it seems it didnt work out well returning a list of errors. – XXX Jun 15 '11 at 09:26
  • it helped me. Thanks – Venkat M Jun 13 '18 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. – perennial_noob Oct 16 '18 at 01:29
2
from smtplib import SMTP_SSL, SMTP, SMTPAuthenticationError
from ssl import create_default_context
from email.message import EmailMessage

sender = 'aaa@bbb.com'
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'] = 'bbb@ccc.com'


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')
Vignesh Rao
  • 136
  • 3
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

teodor mihail
  • 343
  • 3
  • 7
  • Great. And just for understanding, does SMTP_SSL with port 465 create the same levels of security as using `starttls()`? – Hefe Jun 22 '22 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 – teodor mihail Jun 24 '22 at 14:09
  • Gotcha. I had the same problem when trying to use tls. – Hefe Jun 24 '22 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. – teodor mihail Jun 24 '22 at 22:41
  • 1
    Look, read this. Google made a Gmail Api that you can integrate within your Python application: https://developers.google.com/gmail/api/quickstart/python – teodor mihail Jun 24 '22 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.

jforberg
  • 6,537
  • 3
  • 29
  • 47
-3

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

Brian
  • 14,610
  • 7
  • 35
  • 43
9jera
  • 9
  • 1
  • 4