使用 Python smtplib 发送邮件时出现权限被拒绝错误

发布于 2024-11-15 15:57:19 字数 2103 浏览 3 评论 0原文

我正在尝试使用 Python 3.2 发送邮件。我的代码如下:

#from email import Encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
import os
import smtplib
from base64 import encode
from email import encoders

def sendMail(to, subject, text, files=[],server="smtp.mydomain.com"):
    assert type(to)==list
    assert type(files)==list
    fro = "From <[email protected]>"

    msg = MIMEMultipart()
    msg['From'] = fro
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload( open(file,"rb").read() )
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                       % os.path.basename(file))
        msg.attach(part)

    smtp = smtplib.SMTP_SSL(server, 465)
    smtp.ehlo()
    smtp.set_debuglevel(1)
    smtp.ehlo()
    smtp.login("[email protected]", "mypassword")
    smtp.ehlo()
    smtp.sendmail(fro, to, msg.as_string() )
    smtp.close()
    print("Email send successfully.")

sendMail(
        ["recipient"],
        "hello","cheers",
        []
    )

它给了我以下错误:

raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (501, b'5.7.1 <[email protected]>... Permission denied', '[email protected]')

有人知道如何解决这个问题吗?

提前致谢。

I am trying to send mail using Python 3.2. My code is as follows:

#from email import Encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
import os
import smtplib
from base64 import encode
from email import encoders

def sendMail(to, subject, text, files=[],server="smtp.mydomain.com"):
    assert type(to)==list
    assert type(files)==list
    fro = "From <[email protected]>"

    msg = MIMEMultipart()
    msg['From'] = fro
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    for file in files:
        part = MIMEBase('application', "octet-stream")
        part.set_payload( open(file,"rb").read() )
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"'
                       % os.path.basename(file))
        msg.attach(part)

    smtp = smtplib.SMTP_SSL(server, 465)
    smtp.ehlo()
    smtp.set_debuglevel(1)
    smtp.ehlo()
    smtp.login("[email protected]", "mypassword")
    smtp.ehlo()
    smtp.sendmail(fro, to, msg.as_string() )
    smtp.close()
    print("Email send successfully.")

sendMail(
        ["recipient"],
        "hello","cheers",
        []
    )

It gives me following error:

raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (501, b'5.7.1 <[email protected]>... Permission denied', '[email protected]')

Does anybody know how to solve this problem?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱绻入梦 2024-11-22 15:57:19

正如错误所述:在尝试使用 smtplib.SMTP_SSL 实例之前,您需要调用它的 connect 方法。 smtplib.SMTP_SSL 不会自动连接(smtplib.SMTP 也不会。)

As the error says: you need to call the connect method on the smtplib.SMTP_SSL instance before you try to use it. smtplib.SMTP_SSL does not automatically connect (and neither does smtplib.SMTP.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文