【问题标题】:Using gmail through python without smtp在没有 smtp 的情况下通过 python 使用 gmail
【发布时间】:2023-01-28 19:16:41
【问题描述】:

自 5 月 30 日起,不再接受 smtp。

https://support.google.com/accounts/answer/6010255?hl=en&ref_topic=7188673

制作简单的 python 电子邮件而不是使用“使用 google 登录”选项的完整应用程序的新方法是什么?

不知道为什么我被要求提供代码和错误,因为我已经诊断出问题并且正在寻求替代方法。 这里是。当我在家工作时,它是一个方便的电子邮件发送器,可以给我发短信让我锻炼身体。

import time
import smtplib
import random
gmail_user = 'usernameImNotSharing@gmail.com'
gmail_password = 'TheCorrectPassword'

sent_from = gmail_user
to = ['myphonenumber@vtext.com']
exercises = ['push ups', 'jumps in place', '20lb curls', 'tricep extensions', 'quarter mile runs']
levels = [1, 2, 3]
level1 = ['10', '15', '16', '20', '1']
level2 = ['15', '30', '30', '40', '2']
level3 = ['20', '50', '48', '70', '4']
while True:
    if int(time.strftime('%H')) > 9:
        if int(time.strftime('%H')) < 23:
            abc = random.uniform(0, 1)
            picker = random.randint(0, 4)
            if abc < 0.3:
                level = level1
            if 0.3 < abc and abc < 0.8:
                level = level2
            if abc > 0.8:
                level = level3
            exersize = exercises[picker]
            amount = level[picker]
            try:
                subject = f'Test'
                body = f'Do {amount} {exersize}'
                server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
                server.ehlo()
                server.login(gmail_user, gmail_password)
                server.sendmail(sent_from, to, body)
                server.close()
                print('Email sent!')
            except Exception as error:
                print(error)
            time.sleep(random.randint(1500, 4800))
    time.sleep(100)

错误:

(535,b'5.7.8 用户名和密码未被接受。在 \n5.7.8 https://support.google.com/mail/?p=BadCredentialsjj1-20020a170903048100b00163247b64bfsm7655137plb.115 - gsmtp' 了解更多信息)

解决如下:应用程序密码仍然接受 SMTP。 可以在此处找到应用密码创建步骤,但您必须先启用 2 因素身份验证,然后才能创建应用密码。

https://support.google.com/accounts/answer/185833 https://myaccount.google.com/security

【问题讨论】:

  • 请编辑您的问题并包含您的代码。我想看看你目前是如何尝试做到这一点的。还请包括任何错误消息
  • @DaImTo 完成了,虽然我觉得没有添加任何重要的东西。该错误将您指向我最初链接的文档。我相信关于应用程序密码的较低答案可能就足够了,但除非您启用 2fa,否则应用程序密码似乎是不可能的
  • 在你的问题中包含你的代码是一个很好的做法。它允许我复制你的代码并向你展示如何使用应用程序密码修复它。应用程序密码在 2fa 上工作正常我在我的帐户上启用了它并且我一直在测试它一整天。没问题。
  • 这可能是真的,但我想我需要创建一个新的专用电子邮件帐户,因为我不想在这上面有 2fa 并减慢其他地方的登录速度
  • 安全确实会减慢速度。 TBH 绝不会使用您的标准帐户进行授权。

标签: python smtp gmail google-oauth


【解决方案1】:

2022 年 5 月 30 日后更正,发送用户实际密码不再被 googles smtp 服务器接受

你应该配置一个 apps password 这有效。然后用这个新的应用程序密码替换您代码中的密码。

应用密码是一个 16 位数的密码,它允许安全性较低的应用或设备访问您的 Google 帐户。应用密码只能用于已开启两步验证的帐户。

gmail_user = 'usernameImNotSharing@gmail.com'
gmail_password = 'AppsPassword'

这通常是由于用户启用了 2fa。

另一种选择是使用Xoauth2

【讨论】:

  • 在主要帖子中注意,这仅在您有应用程序密码时才有效。应用密码只能在您启用 2 因素身份验证后创建
  • 它在我链接到如何创建一个的页面顶部。
【解决方案2】:
import smtplib

host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "testpython@test.com"
TO = "bla@test.com"
MSG = "Subject: Test email python

Body of your message!"
server.sendmail(FROM, TO, MSG)

server.quit()
print ("Email Send")

【讨论】:

  • 我可以在我的 mac 上做这个,但在我的老式 debian 中这个错误仍然存​​在。我想知道这是否与 openssl 或 debian 中的其他一些密码学有关?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-20
  • 2014-08-18
  • 2011-10-22
相关资源
最近更新 更多