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

2024-04-28 12:50:11 发布

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

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

SMTPAuthenticationError: 550, b'5.2.1 Mailbox cannot be accessed

如果我在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脚本中出现这种行为


Tags: textimport脚本messageisaddress电子邮件email