gpt4 book ai didi

Python - 发送电子邮件时出错(中继访问被拒绝)

转载 作者:行者123 更新时间:2023-12-03 17:27:20 24 4
gpt4 key购买 nike

尝试使用 python sendmail 发送电子邮件时出现错误。这是我的python代码:
(Pas d'objet)

#! /usr/bin/python

import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# me == my email address
# you == recipient's email address
me = "my@email.com"
you = "email@gmail.com"

# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "Link"
msg['From'] = me
msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).
text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org"
html = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="[http://www.python.org">link</a]http://www.python.org">link</a> you wanted.
</p>
</body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via local SMTP server.
s = smtplib.SMTP('localhost')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()

当我发送此电子邮件时,出现以下错误:
Traceback (most recent call last):
File "./mail.py", line 47, in <module>
s.sendmail(me, you, msg.as_string())
File "/usr/lib/python2.7/smtplib.py", line 747, in sendmail
raise SMTPRecipientsRefused(senderrs)
smtplib.SMTPRecipientsRefused: {'email@gmail.com': (454, '4.7.1 <email@gmail.com>: Relay access denied')}

我真的不知道为什么这在生产中不起作用,因为它在测试环境中工作。能帮忙理解一下吗?

最佳答案

Relay access denied消息很重要:听起来您已成功建立 SMTP 连接(到示例代码中的 localhost),但该机器拒绝接受消息:您可能需要调整其中之一,

  • 您在 localhost 上的邮件服务器配置
  • 您的代码以这种方式发送电子邮件,您的 localhost邮件服务器将接受它。

  • 特别要注意,到端口 25(SMTP 的默认端口)的邮件通常用于传入电子邮件。在您的情况下,您希望邮件服务器接受消息并将其发送到其他地方(“中继”,因此是错误消息)。似乎您的邮件服务器(此处为 localhost)未设置为接受非本地域的邮件……至少不在端口 25 上。
    SMTP 的另一个重要用途是“邮件提交”,这通常设置在不同的端口上(通常为 587,如果 SSL 加密则为 465)。通过在命令行上使用 telnet 来查看是否是这种情况;也可以使用 EHLO <some_hostname>查看服务器提供的内容(对您的问题感兴趣的是 AUTHSTARTTLS ),例如,如果它在端口上响应,
    $ telnet localhost 587
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    220 localhost.example.org ESMTP Postfix (Ubuntu)
    ehlo localhost.example.org
    250-localhost.example.org
    250-VARIOUS_FEATURES_ON_LINES_LIKE_THIS
    250-STARTTLS
    250-AUTH LOGIN PLAIN
    250-AUTH=LOGIN PLAIN
    QUIT
    221 2.0.0 Bye
    Connection closed by foreign host.
    也对端口 25 执行此操作并比较结果:这应该为您提供有关如何进行的线索 - 也许您需要 AUTH例如,或使用端口 587,或者您可能需要调整( localhost )邮件服务器配置,使其充当“智能主机”将电子邮件转发到“网络”。

    关于Python - 发送电子邮件时出错(中继访问被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45675473/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com