如何使用Outlook帐户在Python中使用smtplib发送电子邮件

时间:2018-11-15 04:44:40

标签: python outlook smtplib

我正在关注此帖子,以使用我的Outlook帐户发送电子邮件: Having trouble with sending an email through SMTP Python

我用了它,并做了如下简单的测试代码

username='****'
password='***'
mailServer = smtplib.SMTP('smtp-mail.outlook.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(username, password)

但是失败,并显示此错误

Traceback (most recent call last):

  File "<ipython-input-3-67589181ed6a>", line 7, in <module>
mailServer.login(username, password)

  File "/home/saber/miniconda3/envs/explore/lib/python3.6/smtplib.py", line 730, in login
raise last_exception

  File "/home/saber/miniconda3/envs/explore/lib/python3.6/smtplib.py", line 721, in login
initial_response_ok=initial_response_ok)

  File "/home/saber/miniconda3/envs/explore/lib/python3.6/smtplib.py", line 642, in auth
raise SMTPAuthenticationError(code, resp)

SMTPAuthenticationError: (535, b'5.7.3 Authentication unsuccessful [YQXPR0101CA0037.CANPRD01.PROD.OUTLOOK.COM]')

任何想法可能是什么问题?

1 个答案:

答案 0 :(得分:1)

您可以更改帐户进行测试吗?请参考以下代码:

"""The first step is to create an SMTP object, each object is used for connection 
with one server."""

import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)

#Next, log in to the server
server.login("youremailusername", "password")

#Send the mail
msg = "
Hello!" # The /n separates the message from the headers
server.sendmail("you@gmail.com", "target@example.com", msg)

相关链接:Using Python to Send Email