尝试发送邮件时收到“连接意外关闭:[Errno 104]对等重置连接”错误

时间:2019-01-28 09:29:52

标签: python smtp smtpexception

我想从服务器发送电子邮件。我正在尝试使用已经拥有的hotmail帐户发送邮件。

        try:
            SMTP_SERVER = "smtp.office365.com"
            SMTP_PORT = "587"
            FROM_ADDR = "foo@example.com"
            USERNAME = "foo@example.com"
            PWD = "**whatever**"
            reply_to = to_addr.get('reply_to')
            if not reply_to:
                reply_to = "arun@cloud.io"
            from_address = from_alias if from_alias else FROM_ADDR
            if not USERNAME:
                USERNAME = FROM_ADDR
            to_addrs = to_addr['to'] + to_addr['bcc'] + to_addr['cc']
            if FROM_ADDR and SMTP_SERVER:
                msg = MIMEMultipart()
                main_msg = email.email_convert(subject, message, **email_template_args)
                msg.attach(MIMEText(main_msg, 'html'))
                if attachments:
                    for item in attachments:
                        part = MIMEBase('application', "octet-stream")
                        with open(item, "rb") as f:
                            part.set_payload(f.read())
                        Encoders.encode_base64(part)
                        part.add_header('Content-Disposition',
                                        'attachment; filename="%s"' % os.path.basename(item))
                        msg.attach(part)
                msg.add_header('From', from_alias or from_address)
                msg.add_header('Subject', utils.html_decode(subject))
                msg.add_header('To', ','.join(to_addr['to']))
                if to_addr['cc']:
                    msg.add_header('cc', ','.join(to_addr['cc']))
                if reply_to:
                    msg.add_header('reply-to', ','.join(reply_to))
                smtpObject = SMTP(SMTP_SERVER, SMTP_PORT)
                smtpObject.starttls()
                if PWD:
                    smtpObject.ehlo()
                    smtpObject.login(USERNAME, PWD)
                smtpObject.sendmail(from_address, to_addrs, msg.as_string())
                smtpObject.quit()
            else:
                LOG.error("Notification parameter is missing in configuration file")
                raise Exception("Notification parameter is missing in configuration file")
            return True
        except SMTPException as e:
            message = "Unable to send email :  {err}".format(err=e)
            LOG.error(message)
            raise Exception(e.message or message)

预期结果:要发送的邮件。

实际结果:无法发送电子邮件:连接意外关闭:[Errno 104]对等重置连接

0 个答案:

没有答案