通过Gmail发送电子邮件时程序卡住

program gets stuck when sending email via gmail

提问人:Alex Ironside 提问时间:10/31/2023 最后编辑:user3783243Alex Ironside 更新时间:10/31/2023 访问量:38

问:

我有以下python函数:

import smtplib, ssl

port = 465  # For SSL

password = "valid password"
context = ssl.create_default_context()


def sendEmail(to_email, subject, content):
    from_email = "[email protected]"
    message = (f"Tresc zadania: {content}",)
    try:
        print("connecting")
        with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
            print("connected")
            server.login("[email protected]", password)
            print("logged")
            server.sendmail(
                to_addrs=to_email,
                from_addr=from_email,
                msg=message,
            )
            print("sent")
    except Exception as e:
        print(e.message)

以下是日志:

connecting
connected
logged

因此,它会连接,登录到谷歌邮件,但在尝试发送电子邮件时会卡住。

我错过了什么?

我允许不太安全的应用程序,密码是从谷歌安全 2FA 选项卡(应用程序密码)生成的。

enter image description here

python django django-rest-framework smtplib

评论

0赞 Tim Roberts 10/31/2023
是 Gmail 域名吗?您是否打开了 smtplib 的调试以查看 IT 所看到的痕迹?zaliczenie.pl
0赞 Alex Ironside 10/31/2023
不,我没有。我会试试的

答: 暂无答案