使用SMTPLib Python时获取未经授权的发件人地址 [英] Getting unauthorized sender address when using SMTPLib Python

查看:78
本文介绍了使用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}\n\n{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:\Users\James\AppData\Local\Programs\Python\Python37-32\lib\smtplib.py", line 888, in sendmail888, in sendmail    raise SMTPDataError(code, resp)smtplib.SMTPDataError: (554, b'Transaction failed\nUnauthorized 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}\n\n{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时获取未经授权的发件人地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆