Getting unauthorized sender address when using SMTPLib Python(使用 SMTPLib Python 时获取未经授权的发件人地址)
问题描述
我编写了一个非常简单的 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 时获取未经授权的发件人地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 SMTPLib Python 时获取未经授权的发件人地址


基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01