SMTP AUTH extension not supported by server
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()
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
.
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.
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()
.
import smtplib, sslfrom email.message import EmailMessagemsg = EmailMessage()msg.set_content("The body of the email is here")msg["Subject"] = "An Email Alert"msg["From"] = "me@example.com"msg["To"] = "you@example.com"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)