电子邮件应用程序中的连接被拒绝

2024-06-07 09:04:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我让网络应用程序发送电子邮件。我写代码

def mail(self,mail_adress, docx):

    msg = MIMEMultipart()
    msg['Subject'] = 'TEST' 
    msg['From'] = 'info@xxx.com'  
    msg['To'] = mail_adress 

    body = "TEST"
    body = MIMEText(body)
    msg.attach(body)

    mine = {'type': 'text', 'subtype': 'docx'}
    attachment = MIMEBase(mine['type'], mine['subtype'])

    name = os.path.dirname(os.path.abspath(__name__))

    attach_file = {'name': 'test.docx', 'path': name  + "/" + docx}
    file = open(attach_file['path'], 'rb')
    attachment.set_payload(file.read())
    file.close()
    encoders.encode_base64(attachment)
    msg.attach(attachment)
    attachment.add_header("Content-Disposition", "attachment", filename=attach_file['name'])

    server = smtplib.SMTP()
    server.connect()
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.send_message(msg,mail_adress)
    server.quit()

当我运行它时, [Errno 111]发生连接被拒绝错误。我真的不明白为什么会发生这样的错误。我应该如何修复此错误


Tags: pathnametestattachmentservertype错误body

热门问题