我正在尝试开发一个小python应用程序,允许它的用户通过python发送多封邮件,我正在使用gmail,我已经允许Access安全性较低的应用程序,它不会发送。 我已经搜索了stackoverflow以查找类似的问题,但似乎大多数使用过的代码在实现方面完全不同,即使在尝试了许多下摆之后,它们都是异常
import smtplib
from telnetlib import Telnet
def addSenders(message):
num = input("enter number of recievers : ")
num = int (num)
i = 0
emailList = []
while i < num :
nameStr = input("enter name")
mailStr = input("enter e-mail")
emailList.append(mailStr)
if i == 0:
message += nameStr + " <" + mailStr + ">"
print(message)
else:
message += "," + nameStr + " <" + mailStr + ">"
i = i + 1
return emailList, message
sender = 'xxxx@gmail.com'
password = "xxxx"
message = """From: xxxx xxxx <xxxxx@gmail.com>
To: To """
to, message = addSenders(message)
message += """
MIME-Version: 1.0
Content-type: text/html
Subject: Any Subject!
<h1> Good Morning :D <h1>
"""
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login(sender, password)
try:
server.sendmail(sender, [to], message)
print ("Successfully sent email")
except:
print ("Error: unable to send email")
server.quit()
输出:Error: unable to send email