因此,我尝试使用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
总是被引发。谁能帮我吗?