Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python通过公司帐户发送电子邮件_Python_Email_Smtplib - Fatal编程技术网

使用Python通过公司帐户发送电子邮件

使用Python通过公司帐户发送电子邮件,python,email,smtplib,Python,Email,Smtplib,我想用我的公司帐户发送电子邮件。但是,我在Python中遇到以下错误: SMTPAuthenticationError:550,无法访问b'5.2.1邮箱 如果我在outlook上打开我的公司帐户,我就可以使用Powershell脚本通过它发送电子邮件。但是Python脚本只给出了上面的错误 以下是我的python代码: import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text impor

我想用我的公司帐户发送电子邮件。但是,我在Python中遇到以下错误:

SMTPAuthenticationError:550,无法访问b'5.2.1邮箱

如果我在outlook上打开我的公司帐户,我就可以使用Powershell脚本通过它发送电子邮件。但是Python脚本只给出了上面的错误

以下是我的python代码:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail_content = "Hello, This is a simple mail. There is only text, no \
attachments are there The mail is sent using Python SMTP library"

sender_address = 'corporate_email_account'
sender_pass = 'XXXX'
receiver_address = 'corporate_email_account'

message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'   
message.attach(MIMEText(mail_content, 'plain'))
session = smtplib.SMTP('smtp.office365.com', 587) 
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')

为什么Python无法发送电子邮件?这可能是防火墙问题,或者Microsoft不允许在python脚本中使用此行为,但在Powershell脚本中使用此行为?

SMTP和Exchange MAPI是不同的协议。可能未在Exchange服务器上为您的帐户启用SMTP-它是可选的。Powershell可能正在使用MS专有MAPI协议。我刚刚复制了您的代码并使用了我的365公司帐户数据,它运行良好(无代理,python 3.7.5)。SMTP和Exchange MAPI是不同的协议。可能未在Exchange服务器上为您的帐户启用SMTP-它是可选的。Powershell可能正在使用MS专有MAPI协议。我刚刚复制了您的代码并使用了我的365公司帐户数据,它运行良好(无代理,python 3.7.5)。