使用python脚本从Azure服务器发送电子邮件

时间:2018-12-10 11:42:36

标签: python azure smtp azure-web-sites

我正在使用python脚本发送电子邮件。 脚本的邮件部分看起来像-

import smtplib
sender = 'abc.xyz@company.com'
receiver = 'abc.xyz@company.com'
message = """From: From Person <abc.xyz@company.com>
To: To Person <abc.xyz@company.com>
Subject: Inbound Folder Check - Alert
Unchanged even after an hour, please check
"""
smtpObj = smtplib.SMTP('smtp.office365.com',587)
smtpObj.starttls()
smtpObj.login('def@company.onmicrosoft.com','Password')
smtpObj.sendmail(sender, receiver, message)

我得到的错误是-

  

提高SMTPDataError(code,resp)smtplib.SMTPDataError:(554,b'5.2.0   STOREDRV.Submission.Exception:SendAsDeniedE   xception.MapiExceptionSendAsDenied;由于出现以下原因,无法处理消息   消息的永久异常无法提交消息。 ...,   20.52176:140FCC8414001010640 10000,20.50032:140FCC848417101069010000,0.35180:0A006586,255.23226:6E010000,   255.27962:0A000000,255.27962:0E000000,255.31418:0A00EB87,16.55847:69000000,1   7.43559:0000000090010000000000000D00000000000000,20.52176:140FCC84140010100A00F 736,20.50032:140FCC84841710100A00F836,0.35180:8C010000,255.23226:40000730,25   5.27962:32000000,255.17082:DC040000,0.27745:9A010000,4.21921:DC040000,255.27 962:FA000000,255.1494:0A00070E,0.37692:01000100,0.44092:01000100,0.41232:010 00100,0.60208:01000100,0.37136:01000100,0.34608:01000100, 0.55056:01000100,0 .42768:01000100,0.56112:05000780,0.52807:00000000,4.33016:DC040000,7.40748:0 10000000000010C00000000,7.57132:000000000000000005000780,1.63016:32000000,4.3 9640:DC040000,8.45434:EC5537826FAD8844ABBA433565F2D5720500023036034323023043342230343603230230433426033428034233004330423042343023023043343230343   338612D343530642D386265322D62646464616261333932613900602005000780,   255.1750:4B01 0000,255.31418:0A004C82,0.22753:50010000,255.21817:DC040000,4.60547:DC040000,0.21966:B4100000,4.30158:DC040000 [主机名= abc]')

我在互联网上获得的唯一帮助来自以下页面-https://support.microsoft.com/en-us/help/3132755/554-5-2-0-storedrv-deliver-exception-objectnotfoundexception-mapiexcep 似乎说杂波已启用,但邮箱中没有该文件夹,建议关闭无用的杂波选项。

PS-这可以在公司网络中的本地计算机上运行,​​但这与从Azure服务器运行它有关。

1 个答案:

答案 0 :(得分:0)

首先,请遵循官方文档How to set up a multifunction device or application to send email using Office 365,以确保设置正确。

第二,如果您使用Azure VM通过SMTP发送邮件,则可以参考本文档Understand outbound SMTP problems in Azure VMs尝试解决问题。

同时,对于编码,有一些资源可能会有所帮助。

  1. Python: Send Email via Office 365
  2. Send email on App Service using Office 365 (O365) in PHP
  3. Sending email from an Azure App Service using an O365 SMTP server in C#

或者直接使用python-o365之类的某些软件包:GitHubPyPI

最后,有一个名为SendGrid的服务,可用于电子邮件服务。您可以在Azure上的情况下使用它来发送邮件,请参考官方教程以了解How to Send Email Using SendGrid with Azure(C#)。对于Python,您可以通过its REST APIsendgrid python library进行编码。