简体   繁体   English

Python不发送电子邮件:smtplib.SMTPException:服务器不支持STARTTLS扩展

[英]Python doesn't send email: smtplib.SMTPException: STARTTLS extension not supported by server

I am trying to send email via Python but I am having an error. 我正在尝试通过Python发送电子邮件,但出现错误。

This is my code: 这是我的代码:

import smtplib

content = 'example bla bla'
mail = smtplib.SMTP('localhost')
mail.ehlo()
mail.starttls()
mail.login('from@example.com','password')
mail.sendmail('from@example.com','to@example.com',content)
mail.close()

When I run the file, this error shows up: 当我运行文件时,出现此错误:

Traceback (most recent call last):
  File "SendEmail.py", line 11, in <module>
    mail.starttls()
  File "/usr/lib/python2.7/smtplib.py", line 637, in starttls
    raise SMTPException("STARTTLS extension not supported by server.")
smtplib.SMTPException: STARTTLS extension not supported by server.

There are posts about other people with the same problem but usually is just the order of the commands that is wrong (like this one ) and apparently mine is correct. 大约有其他人有相同问题的职位,但通常是命令的只是顺序是错误的(就像这一个 ),显然我的是正确的。

If I change mail = smtplib.SMTP('localhost') to mail = smtplib.SMTP('smtp.gmail.com', 587) it works well. 如果我将mail = smtplib.SMTP('localhost')更改为mail = smtplib.SMTP('smtp.gmail.com', 587)则效果很好。 That makes me think that the problem may be in the configuration of the "localhost" but if I open http://localhost in the browser the page "It works" is presented so I think the localhost is well configured. 这使我认为问题可能出在“本地主机”的配置中,但是如果我在浏览器中打开http://localhost ,则会显示“它有效”页面,因此我认为本地主机配置正确。

Does anyone know where can be the problem? 有人知道哪里可能出问题吗?

Try commenting out mail.starttls() . 尝试注释掉mail.starttls() i had this problem and it works in my code. 我有这个问题,它可以在我的代码中工作。

import smtplib
import string
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText

smtpObj = smtplib.SMTP('mailrelay.address.com', 587) #email relay address
smtpObj.ehlo()
#smtpObj.starttls() #commented this out as was causing issues
smtpObj.login('domain\username', 'pwrd123')

You probably don't need to login to the SMTP server running on 'localhost' ('localhost' is conventionally the same computer your program is running on.) 您可能不需要登录到在“ localhost”上运行的SMTP服务器(“ localhost”通常是您的程序在同一台计算机上运行。)

Try this: 尝试这个:

import smtplib

content = 'example bla bla'
mail = smtplib.SMTP('localhost')
mail.ehlo()
mail.sendmail('from@example.com','to@example.com',content)
mail.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python smtplib登录错误smtplib.SMTPException:服务器不支持STARTTLS扩展 - Python smtplib login error smtplib.SMTPException: STARTTLS extension not supported by server 无法使用gmail通过python发送电子邮件 - smtplib.SMTPException:服务器不支持SMTP AUTH扩展 - Cant send email via python using gmail - smtplib.SMTPException: SMTP AUTH extension not supported by server SMTPException:服务器不支持STARTTLS扩展 - SMTPException: STARTTLS extension not supported by server python 3.9.0 smtplib.SMTPNotSupportedError:服务器不支持 STARTTLS 扩展 - python 3.9.0 smtplib.SMTPNotSupportedError: STARTTLS extension not supported by server Django引发此错误:SMTPException:服务器不支持STARTTLS扩展 - Django throws this error: SMTPException: STARTTLS extension not supported by server smtplib 不发送 email | Python - smtplib doesn't send an email | Python 服务器不支持 STARTTLS 扩展 - 尝试通过 Django 和私人电子邮件地址发送电子邮件时出现此错误 - STARTTLS extension not supported by server - Getting this error when trying to send an email through Django and a private email address 服务器不支持 STARTTLS 扩展 - STARTTLS extension not supported by server smtplib.SMTPNotSupportedError:服务器不支持 SMTP AUTH 扩展 - 尝试通过 Django 发送 email - smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server - trying to send email via Django SMTPNotSupportedError: 服务器不支持 STARTTLS 扩展 - SMTPNotSupportedError: STARTTLS extension not supported by server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM