smtplib-无法连接到服务器

时间:2019-06-01 15:29:28

标签: python python-3.x

因此,我尝试使用smtplib库通过"smtp.gmail.com"主机发送电子邮件。这是我的代码:

#!/usr/bin/python3

import smtplib
from getpass import getpass

class NoReceivers(Exception):
    """Exception raised when there are no receivers."""
    pass

sender = # EMAIL ADRESS
print(f"Account used: {sender}")
password = getpass("\nEnter password: ")
server = smtplib.SMTP("smtp.gmail.com", 587)
print("\nConnecting to gmail.com...")

try:
    server.starttls()
    server.login(sender, password)
    receivers = input("\nEnter e-mail adresses: ")
    receivers = receivers.split(",")
    if not receivers: raise NoReceivers
    content = input("\nEnter content: ")
    server.sendmail(sender, receivers, content)
except NoReceivers:
    print("\nError: no receivers input!")
except smtplib.SMTPAuthenticationError:
    print("\nError while authentificating! Please retry!")
except smtplib.SMTPRecipientsRefused:
    print("\nReceivers' adresses not found or forbidden!")
except:
    print("\nAn unexpected error occured! Please retry later!")
finally:
    try: server.quit()
    except: pass

但是smtplib.SMTPAuthenticationError总是被引发。谁能帮我吗?

0 个答案:

没有答案