通过python不用SMTP使用gmail

发布于 2025-02-08 00:17:03 字数 2652 浏览 1 评论 0 原文

截至5月30日,不再接受SMTP。

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

鉴于我已经诊断出该问题并要求替代方法,因此不确定为什么要要求我进行代码和错误。 这里是。这是一个方便的电子邮件机,我在家工作时向我发短信给我锻炼。

import time
import smtplib
import random
gmail_user = '[email protected]'
gmail_password = 'TheCorrectPassword'

sent_from = gmail_user
to = ['[email protected]']
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=badcredentials JJ1-20020A17090303048100B00163247B64B64BFSM76555137PLB.115 -GSMTP '115 -GSMTP')

somtp for App saspes sopps passects。 可以在此处找到应用程序密码创建步骤,但是您必须先启用2个因子Auth,然后才能创建应用程序密码。

https://myaccount.google.com/security

As of may 30th, smtp is no longer accepted.

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

What is the new way to make a simple python emailer rather than a full application with the "login with google" option?

Not sure why I was asked for the code and error, given that I already diagnosed the issue and was asking for alternative methods.
Here it is. Its a handy emailer that texts me to workout when I work at home.

import time
import smtplib
import random
gmail_user = '[email protected]'
gmail_password = 'TheCorrectPassword'

sent_from = gmail_user
to = ['[email protected]']
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)

error:

(535, b'5.7.8 Username and Password not accepted. Learn more at\n5.7.8 https://support.google.com/mail/?p=BadCredentials jj1-20020a170903048100b00163247b64bfsm7655137plb.115 - gsmtp')

Solved below: SMTP is still accepted for app passwords.
App passwords creation steps can be found here, but you must enable 2 factor auth first, before app passwords can be created.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉风有信 2025-02-15 00:17:03

2022年5月30日之后进行更正,发送用户实际密码不再被Google SMTP服务器接受,

您应该配置应用程序密码这起作用。然后用此新应用程序密码替换代码中的密码。

应用程序密码是一个16位密码,可允许使用较小的应用程序或设备访问您的Google帐户。 App密码只能与具有2步验证的帐户一起使用。

gmail_user = '[email protected]'
gmail_password = 'AppsPassword'

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

另一个选项是使用 xoauth22

Correction after May 30 2022, sending the users actual password is no longer accepted by googles smtp server

You should configuring an apps password this works. Then replace the password in your code with this new apps password.

An App Password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. App Passwords can only be used with accounts that have 2-Step Verification turned on.

gmail_user = '[email protected]'
gmail_password = 'AppsPassword'

This is normally due to the user having 2fa enabled.

Another option would be to use Xoauth2.

把时间冻结 2025-02-15 00:17:03
import smtplib

host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "[email protected]"
TO = "[email protected]"
MSG = "Subject: Test email python\n\nBody of your message!"
server.sendmail(FROM, TO, MSG)

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

host = "server.smtp.com"
server = smtplib.SMTP(host)
FROM = "[email protected]"
TO = "[email protected]"
MSG = "Subject: Test email python\n\nBody of your message!"
server.sendmail(FROM, TO, MSG)

server.quit()
print ("Email Send")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文