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 时获取未经授权的发件人地址
基础教程推荐
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
