使用 Python smtplib 发送邮件时出现权限被拒绝错误
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误所述:在尝试使用
smtplib.SMTP_SSL
实例之前,您需要调用它的connect
方法。smtplib.SMTP_SSL
不会自动连接(smtplib.SMTP
也不会。)As the error says: you need to call the
connect
method on thesmtplib.SMTP_SSL
instance before you try to use it.smtplib.SMTP_SSL
does not automatically connect (and neither doessmtplib.SMTP
.)