Python 服务器不支持 SMTP AUTH 扩展
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37224073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SMTP AUTH extension not supported by server
提问by KKD
Using python I want to send email from my app but it shows the error
使用 python 我想从我的应用程序发送电子邮件,但它显示错误
SMTP AUTH extension not supported by server
Code for the program,
程序代码,
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
fromaddr = "[email protected]"
toaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test Mail"
body = "Test mail from python"
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP('smtp.example.com', 25)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
Telnet Output:
远程登录输出:
ehlo test1.example.com
250-hidden
250-HELP
250-SIZE 104857600
250-ENHANCEDSTATUSCODES
250-8BITMIME
250-STARTTLS
250 OK
I need to authenticate and send mail from app.
我需要验证并从应用程序发送邮件。
回答by Kris
a connection is required before login and sendemail.
登录和发送电子邮件之前需要连接。
server = smtplib.SMTP('smtp.example.com', 25)
server.connect("smtp.example.com",465)
server.ehlo()
server.starttls()
server.ehlo()
server.login(fromaddr, "password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
回答by cardamom
It is probably just the server I was using, but was getting the same error as the OP even after implementing the accepted solution. Turned out the server did not want a login, so after deleting the line server.login(fromaddr, "password")
, the error went away and it worked.
它可能只是我正在使用的服务器,但即使在实施了公认的解决方案后,也遇到了与 OP 相同的错误。原来服务器不想登录,所以删除该行后server.login(fromaddr, "password")
,错误消失了,它起作用了。
回答by Bobzz kenya
import smtplib
s = smtplib.SMTP('smtplib.gmail.com',587)
s.ehlo()
s.starttls()
s.login('frmaddr','password')
try:
s.sendmail('fromaddr','toaddr','message')
except:
print (failed)
回答by Asaga
There is no need to call smtp.connect()
and smtp.ehlo()
, because they are called automatically by SMTP()
and smtp.starttls()
. The issue is solved simply setting port to 587
instead of 28
.
不需要调用smtp.connect()
and smtp.ehlo()
,因为它们会被SMTP()
and自动调用smtp.starttls()
。只需将端口设置为587
而不是28
.
For client use, if you don't have any special requirements for your security policy, it is highly recommended that you use the create_default_context()
function to create your SSL context. It will load the system's trusted CA certificates, enable certificate validation and hostname checking, and try to choose reasonably secure protocol and cipher settings.
对于客户端使用,如果您对您的安全策略没有任何特殊要求,强烈建议您使用该create_default_context()
功能来创建您的 SSL 上下文。它将加载系统的可信 CA 证书,启用证书验证和主机名检查,并尝试选择合理安全的协议和密码设置。
In general, you will want to use the email
package's features to construct an email message, which you can then send via send_message()
.
通常,您会希望使用email
包的功能来构建电子邮件,然后您可以通过send_message()
.
import smtplib, ssl
from email.message import EmailMessage
msg = EmailMessage()
msg.set_content("The body of the email is here")
msg["Subject"] = "An Email Alert"
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
context=ssl.create_default_context()
with smtplib.SMTP("smtp.example.com", port=587) as smtp:
smtp.starttls(context=context)
smtp.login(msg["From"], "p@55w0rd")
smtp.send_message(msg)
回答by qarly_blue
The first tap is enable your gmail account to send emails by another applications, allow in this section:
第一次点击是启用您的 Gmail 帐户以通过其他应用程序发送电子邮件,在此部分中允许:
https://myaccount.google.com/lesssecureapps
https://myaccount.google.com/lesssecureapps
Then, you should can to send an email
然后,您应该可以发送电子邮件
import json
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
message = "This is an email"
password = "yourpasswordemailsender"
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
msg['Subject'] = "Title of email"
msg.attach(MIMEText(message, 'plain'))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
回答by Benchur Wong
you need to 'starttls' before login.
您需要在登录前“starttls”。