我最近发现了一个使用 python 发送电子邮件的代码。它仅适用于单个用户,因此我对其进行了修改,以从 txt 文件中获取电子邮件,该文件将电子邮件存储在每一行,然后向他们发送邮件。但是我发现邮件最终进入垃圾邮件文件夹(如果是 Gmail)或垃圾文件夹(如果是 hotmail 或 live)。是否可以更改代码以使邮件进入收件箱而不是被过滤为垃圾邮件?我是不是搞错了什么?
import smtplib,sys
server = 'smtp.gmail.com'
port = 587
sender = 'my-username@gmail.com'
subject = 'Gmail SMTP Test'
body = 'blah blah blah'
"Sends an e-mail to the specified recipient."
session = smtplib.SMTP(server, port)
session.ehlo()
session.starttls()
session.ehlo
session.login(sender, 'my-password!')
f = open('emails.txt')
for line in f:
recipient = line
print recipient
headers = ["From: " + sender,
"Subject: " + subject,
"To: " + recipient]
headers = "\r\n".join(headers)
session.sendmail(sender, recipient, headers + "\r\n\r\n" + body)
f.close()
session.quit()