我能够使用'python-o365'库成功连接和阅读电子邮件:
Connection.oauth2('Client_ID','Client_Secret',store_token=True)
inbox = FluentInbox()
for message in inbox.fetch_next(2):
print(message.getSubject())
但是,当我尝试使用更基本的示例发送电子邮件时,我收到了来自服务器的401
响应。
Connection.oauth2('Client_ID','Client_Secret',store_token=True)
att = Attachment(path=FilePath)
m = Message()
m.setRecipients(EmailTo)
m.setSubject('DBM Errors Identified - ' + FileName)
m.setBody(MessageBody)
m.attachments.append(att)
m.sendMessage()
我还尝试设置连接对象并将其作为参数传递:
auth = Connection.oauth2('Client_ID','Client_Secret',store_token=True)
m = Message(*auth=auth*)
但这会导致以下错误消息:
TypeError: 'Connection' object is not callable
感谢您的帮助!
答案 0 :(得分:0)
通过切换到上面使用的'fork'库的python-o365,我可以绕过此问题。我觉得该库可能缺少一些明显的东西,但这解决了问题
这是身份验证流程的简化版本,以防万一有人感兴趣:
scopes = ['https://graph.microsoft.com/Mail.Read'']
account = Account(('Client_Id', 'Client_Secret'], auth_method='oauth',scopes=scopes)
account.connection.get_authroization_url() #generate the url for user to authenticate
result_url = input('Paste the result URL once you have authenticated...')
account.connection.get_session() #generate a session
m = account.new_message()
m.to.add('EmailTo')
m.body = 'MessageText'
m.send()