首页 > 解决方案 > 为什么我会收到 smtplib.SMTPRecipientsRefused: 错误?

问题描述

所以基本上我正在尝试开发一个 python 程序,它可以让我在不登录的情况下向我朋友的电子邮件发送电子邮件。但是我收到一个错误,上面写着smtplib.SMTPRecipientsRefused

这是我的代码..

    import smtplib

    myemail = 'myemail@gmail.com'
    mypassword = 'password'
    friendemail = input('Please enter the email of the person:')
    with smtplib.SMTP("smtp.gmail.com", 587) as smtp:
         subject = input("Subject: ")
         body = input('Message:')
         msg = f"subject:{subject}\n\n{body}"

         smtp.starttls()
         smtp.login(myemail,mypassword)
         smtp.sendmail(myemail, mypassword, msg)

如果有人可以提供帮助,那就太好了!谢谢你!

标签: pythonwindowsgmailsmtplib

解决方案


我尝试运行您提供的代码,但下图有问题,对吗?

在此处输入图像描述

根据提示,我更改了sendmail的参数。

smtp.sendmail(myemail, friendemail, msg)

这里的参数代表:发件人邮箱、收件人邮箱、邮件内容。

改完后在vscode中运行成功,发邮件到朋友的Gmail邮箱。

注意:邮箱名和密码要正确填写,不能省略。


推荐阅读