你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用管理 SDK 管理域抑制列表

重要

Azure 通信服务的这一功能目前以预览版提供。 预览版中的功能已公开发布,可供所有新客户和现有Microsoft客户使用。

此预览版在提供时没有附带服务级别协议,我们不建议将其用于生产工作负荷。 某些功能可能不受支持,或者功能可能受到限制。

有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款

本文介绍如何使用 Azure 通信服务管理客户端库管理 Azure 通信服务中的域抑制列表。

先决条件

安装必需包

dotnet add package Azure.ResourceManager.Communication
dotnet add package Azure.Identity

初始化管理客户端

使用你的域和电子邮件资源所位于的订阅的订阅 ID 设置环境变量 AZURE_SUBSCRIPTION_ID。 运行代码示例以初始化管理客户端。

using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Communication;

ArmClient client = new ArmClient(new DefaultAzureCredential());

向域资源添加抑制列表

若要阻止电子邮件发送到特定地址,第一步是在域资源中设置抑制列表。

使用资源组名称、电子邮件服务名称和要为其创建抑制列表的域资源的名称更新代码示例。 在门户中,通过导航到设置先决条件时创建的域资源来找到此信息。 资源的标题为 <your-email-service-name>/<your-___domain-name>。 在域资源概述的“概要”部分中找到资源组名称和订阅 ID。 为抑制列表资源选择任意名称,并同时在示例中更新该字段。

对于列表名称,请确保它与要禁止发送电子邮件的 MailFrom 地址的发件人用户名相同。 可以在门户中域资源的“MailFrom 地址”部分中找到这些 MailFrom 地址。 例如,你可能有一个 MailFrom 地址,如下所示 donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net。 这个地址的发件人用户名是 donotreply,因此请使用列表名称 donotreply

该代码示例创建抑制列表,并将其存储在变量中 suppressionListResource 以供将来作使用。

string subscriptionId = "<your-subscription-id>"; // Found in the essentials section of the ___domain resource portal overview
string resourceGroupName = "<your-resource-group-name>"; // Found in the essentials section of the ___domain resource portal overview
string emailServiceName = "<your-email-service-name>"; // Found in the first part of the portal ___domain resource title
string domainResourceName = "<your-___domain-name>"; // Found in the second part of the portal ___domain resource title
string suppressionListResourceName = "<your-suppression-list-resource-name>";

ResourceIdentifier suppressionListResourceId = SuppressionListResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName);
SuppressionListResource suppressionListResource = client.GetSuppressionListResource(suppressionListResourceId);

SuppressionListResourceData suppressionListData = new SuppressionListResourceData()
{
    ListName = "<your-sender-username>", // Should match the sender username of the MailFrom address you would like to suppress emails from
};

suppressionListResource.Update(WaitUntil.Completed, suppressionListData);

如果要禁止来自特定域中所有发件人用户名的电子邮件,可以传入列表名称的空字符串。

SuppressionListResourceData suppressionListData = new SuppressionListResourceData()
{
    ListName = "",
};

suppressionListResource.Update(WaitUntil.Completed, suppressionListData);

将地址添加到抑制列表

设置抑制列表后,现在可以添加要阻止发送电子邮件的特定电子邮件地址。

使用抑制列表地址 ID 更新代码示例。 添加的每个抑制列表地址 ID 都需要是唯一的。 建议使用 GUID。 此外,更新您希望阻止其接收您邮件的电子邮件地址。

若要向抑制列表添加多个地址,需要多次重复此代码示例。

string suppressionListAddressId = "<your-suppression-list-address-id>";

ResourceIdentifier suppressionListAddressResourceId = SuppressionListAddressResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName, suppressionListAddressId);
SuppressionListAddressResource suppressionListAddressResource = client.GetSuppressionListAddressResource(suppressionListAddressResourceId);

SuppressionListAddressResourceData suppressionListAddressData = new SuppressionListAddressResourceData()
{
    Email = "<email-address-to-suppress>" // Should match the email address you would like to block from receiving your messages
};

suppressionListAddressResource.Update(WaitUntil.Completed, suppressionListAddressData);

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含抑制的发件人用户名。 你的电子邮件不会发送到禁止的地址。

如果尝试用未被屏蔽的发件人用户名发送电子邮件,该电子邮件仍然能成功发送。

从抑制列表中移出地址

若要从抑制列表中移出地址,请创建如前面的代码示例所示的 SuppressionListAddressResource 并调用 Delete 方法。

suppressionListAddressResource.Delete(WaitUntil.Completed);

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含你选择禁止的发件人用户名。 你的电子邮件已成功发送到以前禁止的地址。

从域资源中移除抑制列表

若要从域资源中移除抑制列表,请创建如前面的代码示例所示的 SuppressionListResource 并调用 Delete 方法。

suppressionListResource.Delete(WaitUntil.Completed);

先决条件

安装必需包

npm install @azure/arm-communication
npm install @azure/identity

初始化管理客户端

将示例代码中的字段替换为你的域和电子邮件资源所在的订阅的订阅 ID。 运行代码示例以初始化管理客户端。

const { CommunicationServiceManagementClient } = require("@azure/arm-communication");
const { DefaultAzureCredential } = require("@azure/identity");

const credential = new DefaultAzureCredential();
const subscriptionId = "<your-subscription-id>";

const client = new CommunicationServiceManagementClient(credential, subscriptionId);

向域资源添加抑制列表

若要阻止电子邮件发送到特定地址,第一步是在域资源中设置抑制列表。

使用资源组名称、电子邮件服务名称和要为其创建抑制列表的域资源的名称更新代码示例。 在门户中,通过导航到设置先决条件时创建的域资源来找到此信息。 资源的标题为 <your-email-service-name>/<your-___domain-name>。 在域资源概述的“概要”部分中找到资源组名称和订阅 ID。 为抑制列表资源选择任意名称,并同时在示例中更新该字段。

对于列表名称,请确保它与要禁止发送电子邮件的 MailFrom 地址的发件人用户名相同。 可以在门户中域资源的“MailFrom 地址”部分中找到这些 MailFrom 地址。 例如,你可能有一个 MailFrom 地址,如下所示 donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net。 这个地址的发件人用户名是 donotreply,因此请使用列表名称 donotreply

const resourceGroupName = "<your-resource-group-name>"; // Found in the essentials section of the ___domain resource portal overview
const emailServiceName = "<your-email-service-name>"; // Found in the first part of the portal ___domain resource title
const domainResourceName = "<your-___domain-name>"; // Found in the second part of the portal ___domain resource title
const suppressionListResourceName = "<your-suppression-list-resource-name>";

parameters = { 
    "listName": "<your-sender-username>", // Should match the sender username of the MailFrom address you would like to suppress emails from
}

await client.suppressionLists.createOrUpdate(
    resourceGroupName,
    emailServiceName,
    domainResourceName,
    suppressionListResourceName,
    parameters
);

如果要禁止来自特定域中所有发件人用户名的电子邮件,可以传入列表名称的空字符串。

parameters = { 
    "listName": "",
}

await client.suppressionLists.createOrUpdate(
    resourceGroupName,
    emailServiceName,
    domainResourceName,
    suppressionListResourceName,
    parameters
);

将地址添加到抑制列表

设置抑制列表后,现在可以添加要阻止发送电子邮件的特定电子邮件地址。

使用抑制列表地址 ID 更新代码示例。 添加的每个抑制列表地址 ID 都需要是唯一的。 建议使用 GUID。 此外,更新您希望阻止其接收您邮件的电子邮件地址。

若要向抑制列表添加多个地址,需要多次重复此代码示例。

const suppressionListAddressId = "<your-suppression-list-address-id>";

parameters = { 
    "email": "<email-address-to-suppress>" // Should match the email address you would like to block from receiving your messages
}

await client.suppressionListAddresses.createOrUpdate(
    resourceGroupName,
    emailServiceName,
    domainResourceName,
    suppressionListResourceName,
    suppressionListAddressId,
    parameters
);

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含抑制的发件人用户名。 你的电子邮件不会发送到禁止的地址。

如果尝试用未被屏蔽的发件人用户名发送电子邮件,该电子邮件仍然能成功发送。

从抑制列表中移出地址

调用 delete 上的 suppressionListAddresses 方法,从抑制列表中移除地址。

await client.suppressionListAddresses.delete(
    resourceGroupName,
    emailServiceName,
    domainResourceName,
    suppressionListResourceName,
    suppressionListAddressId
);

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含抑制的发件人用户名。 你的电子邮件已成功发送到以前禁止的地址。

从域资源中移除抑制列表

调用 delete 上的 suppressionList 方法,从域资源中移除抑制列表。

await client.suppressionLists.delete(
    resourceGroupName,
    emailServiceName,
    domainResourceName,
    suppressionListResourceName
);

先决条件

安装必需包

将以下依赖项添加到 pom.xml

<dependency>
    <groupId>com.azure.resourcemanager</groupId>
    <artifactId>azure-resourcemanager-communication</artifactId>
    <version>2.2.0</version>
</dependency>
<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
    <version>1.11.1</version>
</dependency>

初始化管理客户端

使用你的域和电子邮件资源所位于的订阅的订阅 ID 设置环境变量 AZURE_SUBSCRIPTION_ID

在文件顶部添加以下导入。

import com.azure.core.credential.TokenCredential;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.resourcemanager.communication.CommunicationManager;

运行代码示例以初始化管理客户端。

AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
TokenCredential credential = new DefaultAzureCredentialBuilder()
        .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
        .build();

CommunicationManager manager = CommunicationManager
        .authenticate(credential, profile);

向域资源添加抑制列表

若要阻止电子邮件发送到特定地址,第一步是在域资源中设置抑制列表。

使用资源组名称、电子邮件服务名称和要为其创建抑制列表的域资源的名称更新代码示例。 在门户中,通过导航到设置先决条件时创建的域资源来找到此信息。 资源的标题为 <your-email-service-name>/<your-___domain-name>。 在域资源概述的“概要”部分中找到资源组名称和订阅 ID。 为抑制列表资源选择任意名称,并同时在示例中更新该字段。

对于列表名称,请确保它与要禁止发送电子邮件的 MailFrom 地址的发件人用户名相同。 可以在门户中域资源的“MailFrom 地址”部分中找到这些 MailFrom 地址。 例如,你可能有一个 MailFrom 地址,如下所示 donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net。 这个地址的发件人用户名是 donotreply,因此请使用列表名称 donotreply

String resourceGroupName = "<your-resource-group-name>"; // Found in the essentials section of the ___domain resource portal overview
String emailServiceName = "<your-email-service-name>"; // Found in the first part of the portal ___domain resource title
String domainResourceName = "<your-___domain-name>"; // Found in the second part of the portal ___domain resource title
String suppressionListResourceName = "<your-suppression-list-resource-name>";

manager.suppressionLists().define(suppressionListResourceName)
    .withExistingDomain(resourceGroupName, emailServiceName, domainResourceName)
    .withListName("<your-sender-username>") // Should match the sender username of the MailFrom address you would like to suppress emails from
    .create();

如果要取消来自特定域中所有发件人用户名的电子邮件,可以传入列表名称的空字符串。

manager.suppressionLists().define(suppressionListResourceName)
    .withExistingDomain(resourceGroupName, emailServiceName, domainResourceName)
    .withListName("")
    .create();

将地址添加到抑制列表

设置抑制列表后,现在可以添加要阻止发送电子邮件的特定电子邮件地址。

使用抑制列表地址 ID 更新代码示例。 添加的每个抑制列表地址 ID 都需要是唯一的。 建议使用 GUID。 此外,更新您希望阻止其接收您邮件的电子邮件地址。

若要向抑制列表添加多个地址,需要多次重复此代码示例。

String suppressionListAddressId = "<your-suppression-list-address-id>";

manager.suppressionListAddresses().define(suppressionListAddressId)
    .withExistingSuppressionList(resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName)
    .withEmail("<email-address-to-suppress>") // Should match the email address you would like to block from receiving your messages
    .create();

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含抑制的发件人用户名。 你的电子邮件不会发送到禁止的地址。

如果尝试用未被屏蔽的发件人用户名发送电子邮件,该电子邮件仍然能成功发送。

从抑制列表中移出地址

调用 delete 上的 suppressionListAddresses 方法,从抑制列表中移除地址。

manager.suppressionListAddresses()
    .delete(resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName, suppressionListAddressId);

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含你选择禁止的发件人用户名。 你的电子邮件已成功发送到以前禁止的地址。

从域资源中移除抑制列表

调用 delete 上的 suppressionLists 方法,从域资源中移除抑制列表。

manager.suppressionLists()
    .delete(resourceGroupName, emailServiceName, domainResourceName, suppressionListResourceName);

先决条件

安装必需包

pip install azure-mgmt-communication
pip install azure-identity

初始化管理客户端

使用你的域和电子邮件资源所位于的订阅的订阅 ID 设置环境变量 AZURE_SUBSCRIPTION_ID。 运行代码示例以初始化管理客户端。

from azure.mgmt.communication import CommunicationServiceManagementClient
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()
subscription_id = "<your-subscription-id>"

mgmt_client = CommunicationServiceManagementClient(credential, subscription_id)

向域资源添加抑制列表

若要阻止电子邮件发送到特定地址,第一步是在域资源中设置抑制列表。

使用资源组名称、电子邮件服务名称和要为其创建抑制列表的域资源的名称更新代码示例。 在门户中,通过导航到设置先决条件时创建的域资源来找到此信息。 资源的标题为 <your-email-service-name>/<your-___domain-name>。 在域资源概述的“概要”部分中找到资源组名称和订阅 ID。 为抑制列表资源选择任意名称,并同时在示例中更新该字段。

对于列表名称,请确保它与要禁止发送电子邮件的 MailFrom 地址的发件人用户名相同。 可以在门户中域资源的“MailFrom 地址”部分中找到这些 MailFrom 地址。 例如,你可能有一个 MailFrom 地址,如下所示 donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net。 这个地址的发件人用户名是 donotreply,因此请使用列表名称 donotreply

resource_group_name = "<your-resource-group-name>"; # Found in the essentials section of the ___domain resource portal overview
email_service_name = "<your-email-service-name>"; # Found in the first part of the portal ___domain resource title
domain_resource_name = "<your-___domain-name>"; # Found in the second part of the portal ___domain resource title
suppression_list_resource_name = "<your-suppression-list-resource-name>";

mgmt_client.suppression_lists.create_or_update(
    resource_group_name,
    email_service_name,
    domain_resource_name,
    suppression_list_resource_name,
    parameters={
        "properties": {
            "listName": "<your-sender-username>" # Should match the sender username of the MailFrom address you would like to suppress emails from
        }
    },
)

如果要取消来自特定域中所有发件人用户名的电子邮件,可以传入列表名称的空字符串。

mgmt_client.suppression_lists.create_or_update(
    resource_group_name,
    email_service_name,
    domain_resource_name,
    suppression_list_resource_name,
    parameters={
        "properties": {
            "listName": ""
        }
    },
)

将地址添加到抑制列表

设置抑制列表后,现在可以添加要阻止发送电子邮件的特定电子邮件地址。

使用抑制列表地址 ID 更新代码示例。 添加的每个抑制列表地址 ID 都需要是唯一的。 建议使用 GUID。 此外,更新您希望阻止其接收您邮件的电子邮件地址。

若要向抑制列表添加多个地址,需要多次重复此代码示例。

suppression_list_address_id = "<your-suppression-list-address-id>";

mgmt_client.suppression_list_addresses.create_or_update(
    resource_group_name,
    email_service_name,
    domain_resource_name,
    suppression_list_resource_name,
    suppression_list_address_id,
    parameters={
        "properties": {
            "email": "<email-address-to-suppress>" # Should match the email address you would like to block from receiving your messages
        }
    },
)

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含抑制的发件人用户名。 你的电子邮件不会发送到禁止的地址。

如果尝试用未被屏蔽的发件人用户名发送电子邮件,该电子邮件仍然能成功发送。

从抑制列表中移出地址

调用 delete 上的 suppression_list_addresses 方法,从抑制列表中移除地址。

mgmt_client.suppression_list_addresses.delete(
    resource_group_name,
    email_service_name,
    domain_resource_name,
    suppression_list_resource_name,
    suppression_list_address_id
)

现在,你可以尝试从通信服务资源的 TryEmail 部分或使用其中一个电子邮件 SDK 将电子邮件发送到禁止使用的地址。 请确保使用 MailFrom 地址发送电子邮件,其中包含抑制的发件人用户名。 你的电子邮件已成功发送到以前禁止的地址。

从域资源中移除抑制列表

调用 delete 上的 suppression_lists 方法,从域资源中移除抑制列表。

mgmt_client.suppression_lists.delete(
    resource_group_name,
    email_service_name,
    domain_resource_name,
    suppression_list_resource_name
)