我正在尝试使用 python 发送电子邮件(Gmail),但出现以下错误。
Traceback (most recent call last): File "emailSend.py", line 14, in <module> server.login(username,password) File "/usr/lib/python2.5/smtplib.py", line 554, in login raise SMTPException("SMTP AUTH extension not supported by server.") smtplib.SMTPException: SMTP AUTH extension not supported by server.
Python 脚本如下。
import smtplib fromaddr = 'user_me@gmail.com' toaddrs = 'user_you@gmail.com' msg = 'Why,Oh why!' username = 'user_me@gmail.com' password = 'pwd' server = smtplib.SMTP('smtp.gmail.com:587') server.starttls() server.login(username,password) server.sendmail(fromaddr, toaddrs, msg) server.quit()
您遇到的错误(SMTPException: SMTP AUTH extension not supported by server)通常意味着您尝试连接的服务器不支持您尝试使用的身份验证方法。
SMTPException: SMTP AUTH extension not supported by server
您使用的脚本似乎可以正确通过 Gmail 的 SMTP 服务器发送电子邮件,但您可以尝试以下几种方法来解决该问题:
smtp.gmail.com
587
Subject
From
这是脚本的更新版本,其中考虑了以下事项:
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart fromaddr = 'user_me@gmail.com' toaddrs = 'user_you@gmail.com' subject = 'Subject of the email' msg_body = 'Why, Oh why!' # Create the email message msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddrs msg['Subject'] = subject msg.attach(MIMEText(msg_body, 'plain')) username = 'user_me@gmail.com' password = 'pwd' try: # Connect to the server server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() # Upgrade the connection to a secure encrypted SSL/TLS connection server.login(username, password) # Send the email server.sendmail(fromaddr, toaddrs, msg.as_string()) print("Email sent successfully") except smtplib.SMTPException as e: print(f"Error: {e}") finally: server.quit() # Terminate the SMTP session
通过遵循这些步骤,您应该能够使用 Gmail 的 SMTP 服务器通过 Python 发送电子邮件。