问题描述
我编写了一个非常简单的 Python 脚本,用于自动发送电子邮件.这是它的代码:
I have a very simple Python script that I wrote to send out emails automatically. Here is the code for it:
import smtplib
From = "LorenzoTheGabenzo@gmx.com"
To = ["LorenzoTheGabenzo@gmx.com"]
with smtplib.SMTP('smtp.gmx.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login("LorenzoTheGabenzo@gmx.com", Password)
Subject = "Test"
Body = "TestingTheBesting"
Message = f"{Subject}
{Body}"
smtp.sendmail(From, To, Message)
每当我运行此代码时,我都会收到一个非常奇怪的错误,告诉我此发件人是未经授权的发件人".这是完整的错误
Whenever I run this code I get a very strange error telling me that this sender is an "unauthorized sender". Here is the error in full
File "test.py", line 17, in <module> smtp.sendmail(From, To, Message)
File "C:UsersJamesAppDataLocalProgramsPythonPython37-32libsmtplib.py", line 888, in sendmail888, in sendmail raise SMTPDataError(code, resp)smtplib.SMTPDataError: (554, b'Transaction failed
Unauthorized sender address.')
我已经在 GMX 设置中启用了 SMTP 访问,但我不确定现在还可以做些什么来解决这个问题.
I've already enabled SMTP access in the GMX settings and I'm unsure about what else to do now to fix this issue.
注意:我知道变量密码还没有定义.这是因为我在发布之前故意将其删除,它是在我的原始代码中定义的.
Note: I know that the variable password has not been defined. This is because I intentionally removed it before posting, it's defined in my original code.
推荐答案
GMX 检查邮件标头是否匹配标头中的发件人"条目和实际发件人.您提供了一个简单的字符串作为消息,因此没有标题,因此 GMX 出错.为了解决这个问题,您可以使用电子邮件包中的消息对象.
GMX checks a messages header for a match between the "From" entry in the header and the actual sender. You provided a simple string as message, so there is no header, and hence the error by GMX. In order to fix this, you can use a message object from the email package.
import smtplib
from email.mime.text import MIMEText
Subject = "Test"
Body = "TestingTheBesting"
Message = f"{Subject}
{Body}"
msg = MIMEText(Message)
msg['From'] = "LorenzoTheGabenzo@gmx.com"
msg['To'] = ["LorenzoTheGabenzo@gmx.com"]
with smtplib.SMTP('smtp.gmx.com', 587) as smtp:
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login("LorenzoTheGabenzo@gmx.com", Password)
smtp.sendmail(msg['From'], msg['To'], msg)
这篇关于使用 SMTPLib Python 时获取未经授权的发件人地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!