【问题标题】:send email with Gmail Python使用 Gmail Python 发送电子邮件
【发布时间】:2020-07-12 08:48:25
【问题描述】:

我正在尝试发送电子邮件,但遇到了以下错误: smtplib.SMTPAuthenticationError: (534, b'5.7.9 Application-specific password required. Learn more at\n5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor d2sm13023190qkl.98 - gsmtp')

在网址中我没有看到任何超级有用的东西,有人有什么建议吗?出于这样的目的,我将电子邮件帐户密码保留为test,而不是分享我的个人信息..

import smtplib
import ssl


# User configuration
sender_email = 'test@gmail.com'
receiver_email = 'test@gmail.com'
password = 'test'



# Email text
email_body = '''
    This is a test email sent by Python. Isn't that cool?
'''

# Creating a SMTP session | use 587 with TLS, 465 SSL and 25
server = smtplib.SMTP('smtp.gmail.com', 587)
# Encrypts the email
context = ssl.create_default_context()
server.starttls(context=context)
# We log in into our Google account
server.login(sender_email, password)
# Sending email from sender, to receiver with the email body
server.sendmail(sender_email, receiver_email, email_body)
print('Email sent!')

print('Closing the server...')
server.quit()

【问题讨论】:

  • “需要应用程序专用密码”——你有吗?
  • 你应该考虑使用正式的gmail API
  • 我在我的 gmail 帐户中运行了此页面中的示例并且它有效:julien.danjou.info/…
  • 目前 Gmail 比其他邮件服务器更受限制,每个应用程序都需要自己的密码才能访问邮件。

标签: python email smtplib


【解决方案1】:

您必须在 Google 配置中允许“安全性较低的应用”。

这是另一个主题的链接:Link

【讨论】:

    【解决方案2】:

    我尽力了...我认为这应该可行!

    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    
    email = "test@gmail.com" # the email where you sent the email
    password = "yourPassword"
    send_to_email = "yourEmail@gmail.com" # for whom
    subject = "Gmail"
    message = "This is a test email sent by Python. Isn't that cool?!"
    
    msg = MIMEMultipart()
    msg["From"] = email
    msg["To"] = send_to_email
    msg["Subject"] = subject
    
    msg.attach(MIMEText(message, 'plain'))
    
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(email, password)
    text = msg.as_string()
    server.sendmail(email, send_to_email, text)
    server.quit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-28
      • 2015-01-16
      • 2017-04-01
      • 2018-01-26
      • 2018-11-08
      • 2016-09-09
      • 2020-10-14
      相关资源
      最近更新 更多