Search code examples
pythonemailsmtplib

SMTPNotSupportedError: STARTTLS extension not supported by server


I'm trying to sending mail with smtplib and i got error like below, i searched the other topicks about this error but i cant't find the solution. Can someone to help me?

CODE :

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
toaddr = "mailto@mail.com"
head = "TRY"  
body = "XxX"
fromaddr = "mailsender@mail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = head
password = "password"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('SMTP', 25)
server.connect('SMTP', 25)
server.starttls()
server.ehlo()
server.login(fromaddr, password)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()

ERROR:

SMTPNotSupportedError                     Traceback (most recent call last)
<ipython-input-4-357dd2da8ab1> in <module>
 17 server = smtplib.SMTP('mailenable', 25)
 18 server.connect('mailenable', 25)
 ---> 19 server.starttls()
 20 server.ehlo()
 21 server.login(fromaddr, password)

 /opt/anaconda/envs/env_python/lib/python3.6/smtplib.py in starttls(self, keyfile, certfile, context)
 750         if not self.has_extn("starttls"):
 751             raise SMTPNotSupportedError(
 --> 752                 "STARTTLS extension not supported by server.")
 753         (resp, reply) = self.docmd("STARTTLS")
 754         if resp == 220:

 SMTPNotSupportedError: STARTTLS extension not supported by server.

Solution

  • Figured that problem. Just delete this lines.

    server.starttls()
    server.ehlo()
    server.login(fromaddr, password)