使用smtplib发送邮件遇SMTPNotSupportedError,求解决方案
Fixing SMTPNotSupportedError: STARTTLS Extension Not Supported
Hey there, let's break down what's happening here and fix that error!
The Root Cause
Your error SMTPNotSupportedError: STARTTLS extension not supported by server means exactly what it says: the MailEnable SMTP server you're connecting to doesn't support the STARTTLS encryption protocol. When you call server.starttls(), the server doesn't recognize that command, hence the crash.
Step-by-Step Fix
Here's how to adjust your code to work with your server:
- Remove the
starttls()call: Since your server doesn't support this encryption method, we can skip this step entirely. - Simplify the server connection: You're currently initializing the SMTP server and then calling
connect()separately—we can combine these into one line to clean things up. - Verify server settings: Double-check that your SMTP address (
'mailenable') and port (25) are correct for your MailEnable instance (25 is the standard unencrypted SMTP port).
Modified Working 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')) # Combine initialization and connection into one line server = smtplib.SMTP('mailenable', 25) # Removed server.starttls() since it's not supported server.ehlo() # This is usually optional but harmless server.login(fromaddr, password) text = msg.as_string() server.sendmail(fromaddr, toaddr, text) server.quit()
Additional Troubleshooting Tips
- If you still get login errors, check if your MailEnable server allows plain-text authentication (since we're not using encryption here). You might need to adjust the server's security settings to permit this.
- If your server does support encryption but on a different port:
- For STARTTLS on port 587: Use
smtplib.SMTP('mailenable', 587)and re-addserver.starttls()(only if the server actually supports it on this port). - For SSL encryption on port 465: Use
smtplib.SMTP_SSL('mailenable', 465)instead ofsmtplib.SMTP()(no need forstarttls()in this case).
- For STARTTLS on port 587: Use
内容的提问来源于stack exchange,提问作者Mert Türkyılmaz
相关产品推荐
相关产品推荐

