我正在尝试使用 Graph API 和 Python 发送电子邮件。我尝试使用图形资源管理器进行此操作,并且成功了。我找到了这个例子:https://github.com/vgrem/Office365-REST-Python-Client#working-with-outlook-api
from office365.graph_client import GraphClient
client = GraphClient(acquire_token_func)
client.me.send_mail(
subject="Meet for lunch?",
body="The new cafeteria is open.",
to_recipients=["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="67010609091e0327040809130814084908090a0e041508140801134904080a" rel="noreferrer noopener nofollow">[email protected]</a>"]
).execute_query()
这是我的代码:
import msal
dict_ = {'client_id': 'foo', 'secret': 'bar', 'tenant_id': 'etc'}
def acquire_token():
authority_url = f'https://login.microsoftonline.com/{dict_["tenant_id"]}'
app = msal.ConfidentialClientApplication(
authority=authority_url,
client_id=dict_["client_id"],
client_credential=dict_["secret"]
)
token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
return token
from office365.graph_client import GraphClient
client = GraphClient(acquire_token)
client.me.send_mail(
subject="Meet for lunch?",
body="The new cafeteria is open.",
to_recipients=['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c090003024201191f072c0f03011c0d0215420f0301" rel="noreferrer noopener nofollow">[email protected]</a>']
).execute_query()
尽管它与示例中的完全相同,但我仍然得到:
TypeError: send_mail() got an unexpected keyword argument 'subject'
您能帮我解决此问题或提供其他发送电子邮件的方式吗?
最佳答案
I agree with @user2250152, you need to change
/me
endpoint toclient.users[<mail>].send_mail
as you are using client credentials flow to get token.
我注册了一个 Azure AD 应用程序并授予了 Mail.Send
应用程序类型的权限:
就我而言,我使用下面的修改后的代码来使用 Graph API 和 Python 发送电子邮件:
import msal
import requests;
dict_ = {'client_id': 'appId', 'secret': 'secret', 'tenant_id': 'tenantId'}
def acquire_token():
authority_url = f'https://login.microsoftonline.com/{dict_["tenant_id"]}'
app = msal.ConfidentialClientApplication(
authority=authority_url,
client_id=dict_["client_id"],
client_credential=dict_["secret"]
)
token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
return token
result = acquire_token()
if "access_token" in result:
print("Access token created.",result["access_token"])
if "access_token" in result:
endpoint = f'https://graph.microsoft.com/v1.0/users/userId/sendMail'
toUserEmail = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2f5c5d466f5757575757575757575701404142464c5d405c40495b014c4042" rel="noreferrer noopener nofollow">[email protected]</a>"
email_msg = {'Message': {'Subject': "Meet for lunch?",
'Body': {'ContentType': 'Text', 'Content': "The new cafeteria is open."},
'ToRecipients': [{'EmailAddress': {'Address': toUserEmail}}]
},
'SaveToSentItems': 'true'}
r = requests.post(endpoint,headers={'Authorization': 'Bearer ' + result['access_token']},json=email_msg)
if r.ok:
print('Sent email successfully')
else:
print(r.json())
回应:
为了确认这一点,我检查了Sent Items
电子邮件发送成功,如下所示:
关于Python 使用 Graph API 和 Office365-REST-Python-Client 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76805337/