通过GraphAPI(python)发送邮件。邮件在投递的时候报错550 5.7.501 Service unavailable. Spam abuse detected from IP range.

zhaidavid 0 信誉分
2025-04-17T16:06:09.33+00:00

通过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: 测试测试

帮忙看下是什么问题

Microsoft Graph
Microsoft Graph
一种 Microsoft 可编程性模型,用于公开 REST API 和客户端库以访问 Microsoft 365 服务上的数据。
93 个问题
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. zhaidavid 0 信誉分
    2025-04-18T04:15:50.96+00:00

    问题已经解决。第二天重试成功了。https://sender.office.com/无法解决,没法从NDR中获取到ip信息

    0 个注释 无注释

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。