Microsoft Graph
一种 Microsoft 可编程性模型,用于公开 REST API 和客户端库以访问 Microsoft 365 服务上的数据。
93 个问题
通过https://learn.microsoft.com/zh-cn/graph/sdks/sdk-installation#install-the-microsoft-graph-python-sdk 使用了Graph API去发送邮件
代码如下:
# Code snippets are only available for the latest version. Current version is 1.x
import asyncio
import time
from azure.identity import ClientSecretCredential, DeviceCodeCredential
from msgraph import GraphServiceClient
from msgraph.generated.models.body_type import BodyType
from msgraph.generated.models.email_address import EmailAddress
from msgraph.generated.models.item_body import ItemBody
from msgraph.generated.models.message import Message
from msgraph.generated.models.recipient import Recipient
from msgraph.generated.users.item.send_mail.send_mail_post_request_body import SendMailPostRequestBody
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
scopes = ['https://graph.microsoft.com/.default']
# Multi-tenant apps can use "common",
# single-tenant apps must use the tenant ID from the Azure portal
credential = ClientSecretCredential(
client_id='',
client_secret='',
tenant_id='',
)
graph_client = GraphServiceClient(credentials=credential, scopes=scopes)
async def get_user():
user = await graph_client.users.by_user_id("邮箱").get()
if user:
print(user)
print(user.user_principal_name)
#asyncio.run(get_user())
async def send_mail():
try:
#print(request_body)
request_body = SendMailPostRequestBody(
message = Message(
subject = "测试测试",
body = ItemBody(
content_type = BodyType.Text,
content = "测试测试",
),
to_recipients = [
Recipient(
email_address = EmailAddress(
address = "邮箱",
),
),
],
),
save_to_sent_items = True,
)
response = await graph_client.users.by_user_id("邮箱").send_mail.post(mailbericht)
time.sleep(2)
print(response)
except Exception as e:
print(e)
asyncio.run( send_mail())
以上代码没有报错,但是邮箱在投递的时候报错了
Delivery has failed to these recipients or groups:
{邮箱}
Your message wasn't delivered because the recipient's email provider rejected it.
Diagnostic information for administrators:
Generating server: KL1PR03MB8731.apcprd03.prod.outlook.com
******@bytedance.com
Remote server returned '550 5.7.501 Service unavailable. Spam abuse detected from IP range. For more information please go to http://go.microsoft.com/fwlink/?LinkId=526653. S(2017052602) [TYSPR03MB8519.apcprd03.prod.outlook.com 2025-04-17T06:31:51.189Z 08DD78BD0C82EB71]'
Original message headers:
Received: from KL1PR03MB8731.apcprd03.prod.outlook.com
([fe80::694:3344:2fa:83be]) by KL1PR03MB8731.apcprd03.prod.outlook.com
([fe80::694:3344:2fa:83be%4]) with mapi id 15.20.8606.029; Thu, 17 Apr 2025
06:31:51 +0000
MIME-Version: 1.0
Content-Type: text/plain
Date: Thu, 17 Apr 2025 06:31:51 +0000
Message-ID:
<******@KL1PR03MB8731.apcprd03.prod.outlook.com>
Subject: 测试测试
帮忙看下是什么问题