【问题标题】:send email with python - unauthorized sender address [duplicate]使用python发送电子邮件 - 未经授权的发件人地址[重复]
【发布时间】:2021-02-16 09:13:09
【问题描述】:

我尝试使用电子邮件提供程序“web.de”通过 python 发送电子邮件。
我在电子邮件服务提供商的设置中激活了电子邮件协议。
出现以下错误消息:

(554, b'Transaction failed\nUnauthorized sender address.')

应该可以这样做,因为我的电子邮件程序可以发送电子邮件。

import smtplib, ssl

smtp_server = "smtp.web.de"
port = 587  # For starttls
sender_email = "someemail@web.de"
password = input("Type your password and press enter: ")
receiver_email = "another@web.de"
message = "Subject: Hi there\n\nThis message is sent from Python."

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit() 

【问题讨论】:

    标签: python email smtp


    【解决方案1】:

    首先,我建议你使用 sendinblue,它很棒,而且在某种程度上是免费的。

    关于您的问题,我认为您不应该询问发件人密码,因为他们可能不会给您。接下来,我认为发件人的电子邮件应该包含您在电子邮件提供商处注册的电子邮件,而不是某人提供的电子邮件。我想接收者的电子邮件很好。但是我觉得这部分有一些问题:

    try:
        server = smtplib.SMTP(smtp_server,port)
        server.ehlo() # Can be omitted
        server.starttls(context=context) # Secure the connection
        server.ehlo() # Can be omitted
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message)
    
    

    我认为您忘记写电子邮件的主题,这很可能会触发错误。我从来没有用python发过邮件,但是我用过python web framework django,如果没有提供邮件的主题就会报错。

    试试这个:

    
    try:
        server = smtplib.SMTP(smtp_server,port)
        server.ehlo() # Can be omitted
        server.starttls(context=context) # Secure the connection
        server.ehlo() # Can be omitted
        server.login(sender_email, password)
        server.sendmail("This is the subject", sender_email, receiver_email, message)
    
    
    

    我也认为收件人的电子邮件应该是一个列表,而不仅仅是一个字符串,因为收件人的数量可以改变。即使您不想将其发送给很多人,也可以将其放在这样的列表中。

    receiver_email = ["another@web.de"]
    
    

    您可以通过添加逗号并将第二个收件人写在引号中来新建收件人。

    【讨论】:

    • 当我像你说的那样在 sendmail 函数中包含第四个参数时,它会给我以下错误消息:(500,b'语法错误,命令无法识别','这是主题')。当我使用字符串变量而不是仅仅输入字符串时,也会发生这种情况。
    猜你喜欢
    • 2016-06-21
    • 2012-07-22
    • 1970-01-01
    • 2019-12-05
    • 2016-01-21
    • 2015-06-27
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多