首页 > 解决方案 > 无法使用 smtplib 在 Outlook 上的 python 中发送邮件

问题描述

我尝试通过 Outlook 帐户发送电子邮件,但我不能。我使用 Gmail 成功发送了电子邮件,但无法使用 Outlook。

我试图在 SMTP.office365.com 端口:587 和..之间更改主机和端口

smtp-mail.outlook.com 端口:587 不工作。同样的错误。

Gmail 发送:

def sendmail(from_email, password, to_email, subject, message):
    msg = MIMEMultipart()
    msg['From'] = from_email
    msg['To'] = to_email
    msg['Subject'] = subject
    msg.attach(MIMEText(message, 'plain'))
    try:
        server = smtplib.SMTP_SSL('smtp.gmail.com', 465) #Info pour send depuis une addresse courriel
        server.ehlo()
        server.login(from_email, password)
        server.sendmail(from_email, to_email, msg.as_string())
        server.close
        return True
    except Exception as e:
        print ("Something went wrong :", str(e))
        return False

sendmail('email@gmail.com', 'password', 'email1@gmail.com', 'subject', 'this is message body ; so hot ! ')

展望“发送”:

def sendmail(from_email, password, to_email, subject, message):
    msg = MIMEMultipart()
    msg['From'] = from_email
    msg['To'] = to_email
    msg['Subject'] = subject
    msg.attach(MIMEText(message, 'plain'))
    try:
        server = smtplib.SMTP_SSL('SMTP.office365.com', 587) #Info pour send depuis une addresse courriel
        server.ehlo()
        server.login(from_email, password)
        server.sendmail(from_email, to_email, msg.as_string())
        server.close
        return True
    except Exception as e:
        print ("Something went wrong :", str(e))
        return False

sendmail('email@outlook.com', 'password', 'email1@gmail.com', 'subject', 'this is message body ; so hot ! ')

例外:通过 email1@gmail.com 发送邮件

错误:出了点问题:[SSL: WRONG_VERSION_NUMBER] 错误的版本号 (_ssl.c:1056)

当我删除 _SSL 时,发生错误;

出了点问题:服务器不支持 SMTP AUTH 扩展

标签: pythonsmtpsmtplib

解决方案


推荐阅读