發送郵件我嘗試使用Python 3.2來發送郵件。我的代碼如下:權限被拒絕錯誤,而使用Python的smtplib
#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]')
是否有人知道如何解決這個問題?
在此先感謝。
什麼語句引發異常? – codeape 2011-06-15 10:59:09
@codeape:在堆棧跟蹤行27中看到似乎在提高錯誤。在第27行的代碼是'mailServer.ehlo()' – 2011-06-15 11:03:27
嗨mahendraliya有無論如何我可以聯繫你嗎?我有一個關於Android語音命令的問題給我發電子郵件[email protected] – 2011-06-17 13:59:45