我在一家使用 Gmail 从域电子邮件发送和接收电子邮件的公司。为了更清楚地说明,我们假设它是 [电子邮件受保护]。 现在我的目标是制作一个简单的脚本,使用 csv 文件中的列表发送电子邮件。当我使用我的个人 Gmail 电子邮件时,代码工作正常,但当我将其更改为我的企业帐户时,问题就开始了 - 脚本似乎忽略了初始化步骤,因为它不是 @gmail.com。
不确定是否需要,但现在我会很高兴至少运行“yagmail 101”代码,如下所示。仅供参考,我也尝试了 smtplib ,结果相同。双因素身份验证已开启,Windows 邮件应用程序的 16 个字符的密码已创建。
#importing the Yagmail library
import yagmail
try:
#initializing the server connection
yag = yagmail.SMTP(user='[email protected]', password='mypassword')
#sending the email
yag.send(to='[email protected]', subject='Testing Yagmail', contents='Hurray, it worked!')
print("Email sent successfully")
except:
print("Error, email was not sent")
如果您的公司启用了此功能,则按照此处的步骤进行操作即可,否则您需要联系您公司的管理员: https://support.google.com/accounts/answer/185833?visit_id=638426862839173690-647846903&p=InvalidSecondFactor&rd=1s
#importing the Yagmail library
import yagmail
# connect to smtp server.
yag_smtp_connection = yagmail.SMTP( user="[email protected]", password="mypassword",
host='yourSMTPserver')
# email subject
subject = 'Hello'
contents = ['Hello this email send via YAGMAIL']
# send the email
yag_smtp_connection.send('[email protected]', subject, contents)
print("Email send!")