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

创建和管理电子邮件通信服务资源

通过预配第一个电子邮件通信服务资源开始使用电子邮件。 通过 Azure 门户 或使用 .NET 管理客户端库预配电子邮件通信服务资源。 使用管理客户端库和 Azure 门户,可以使用 Azure 的部署和管理服务: Azure 资源管理器创建、配置、更新和删除资源和接口。 客户端库中提供的所有函数在 Azure 门户中均可用。

警告

不能与 Azure 通信服务的资源同时创建资源组。 必须先创建资源组,然后在预配电子邮件资源时使用它。

先决条件

使用门户创建电子邮件通信服务资源

  1. 打开 Azure 门户 以创建新资源。

  2. 搜索 电子邮件通信服务

    显示如何在市场搜索电子邮件通信服务的屏幕截图。

  3. 选择 电子邮件通信服务 ,然后单击“ 创建”。

    显示创建电子邮件通信服务的“创建”链接的屏幕截图。

  4. “基本 信息”选项卡中输入所需信息:

    • 选择现有的 Azure 订阅。

    • 选择现有资源组,或创建新资源组,单击“ 创建新 ”链接。

    • 提供资源的有效名称。

    • 选择资源需要可用的区域。

    • 选择 “美国 ”作为数据位置。

    • 若要添加标记,请单击“ 下一步:标记”

    • 添加任何名称/值对。

      显示如何查看和创建电子邮件通信服务的摘要的屏幕截图。

  5. 单击“下一步: 查看 + 创建”。

  6. 等待验证完成,然后单击“ 创建”。

  7. 等待部署完成,然后单击“ 转到资源” 以打开电子邮件通信服务概述。

    显示电子邮件通信服务资源的概述的屏幕截图。

先决条件

创建电子邮件通信服务资源

若要创建电子邮件通信服务资源, 请登录到 Azure CLI。 可以从终端运行 az login 命令并提供凭据进行登录。 要创建资源,请运行以下命令:

az communication email create --name "<EmailServiceName>" --___location "Global" --data-___location "United States" --resource-group "<resourceGroup>"

如果要选择特定的订阅,还可以指定 --subscription 标志并提供订阅 ID。

az communication email create --name "<EmailServiceName>" --___location "Global" --data-___location "United States" --resource-group "<resourceGroup>" --subscription "<subscriptionId>"

可以使用以下选项配置电子邮件通信服务资源:

  • 资源组
  • 电子邮件通信服务资源的名称。
  • 与资源关联的地理位置。

在下一步中,可以为资源分配标记。 然后,可以使用标记来组织 Azure 电子邮件资源。 有关标记的详细信息,请参阅资源标记文档

管理电子邮件通信服务资源

若要将标记添加到电子邮件通信服务资源,请运行以下命令。 也可以指定特定的订阅。

az communication email update --name "<EmailServiceName>" --tags newTag="newVal1" --resource-group "<resourceGroup>"

az communication email update --name "<EmailServiceName>" --tags newTag="newVal2" --resource-group "<resourceGroup>" --subscription "<subscriptionId>"

若要列出给定资源组中的所有电子邮件通信服务资源,请使用以下命令:

az communication email list --resource-group "<resourceGroup>"

若要显示给定电子邮件通信服务资源的所有信息,请使用以下命令。 也可以指向特定的订阅。

az communication email show --name "<EmailServiceName>" --resource-group "<resourceGroup>"

az communication email show --name "<EmailServiceName>" --resource-group "<resourceGroup>" --subscription "<subscriptionId>"

清理资源

如果要清理和删除电子邮件通信服务订阅,可以删除资源或资源组。 可以通过运行以下命令来删除电子邮件通信资源。

az communication email delete --name "<EmailServiceName>" --resource-group "<resourceGroup>"

删除资源组时也会删除与之关联的任何其他资源。

注释

资源删除是永久性的。如果删除资源,则无法恢复任何数据,包括事件网格筛选器、电话号码或与资源相关的其他数据

有关其他命令的信息,请参阅 电子邮件通信 CLI

先决条件

安装 SDK

首先,在 C# 项目中加入通信服务管理 SDK:

using Azure.ResourceManager.Communication;

订阅编号

你需要知道 Azure 订阅的 ID。 可以从门户获取 ID:

  1. 登录到 Azure 帐户。
  2. 在左侧栏中选择“订阅”。
  3. 选择所需的订阅。
  4. 单击“概述”。
  5. 选择订阅 ID。

在本快速入门中,我们假设你已将订阅 ID 存储在名为 AZURE_SUBSCRIPTION_ID 的环境变量中。

身份验证

若要与 Azure 通信服务进行通信,你必须首先向 Azure 验证自己的身份。

对客户端进行身份验证

创建经过身份验证的客户端的默认选项是使用 DefaultAzureCredential。 由于所有管理 API 都经过同一终结点,以便与资源交互,只需创建一个顶级 ArmClientAPI。

要向 Azure 进行身份验证并创建 ArmClient,请执行以下代码:

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Communication;
using Azure.ResourceManager.Resources;
...
// get your azure access token, for more details of how Azure SDK get your access token, please refer to https://learn.microsoft.com/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

与 Azure 资源交互

现在,你已进行身份验证。

对于以下示例中的每个示例,我们将电子邮件服务资源分配给现有资源组。

如需创建资源组,你可以使用 Azure 门户Azure 资源管理器 SDK 完成此操作。

创建电子邮件服务资源

创建电子邮件服务资源时,请指定资源组名称和资源名称。

注释

Location 属性始终是 global,并且在公共预览版期间,DataLocation 值必须是 UnitedStates

// this example assumes you already have this ResourceGroupResource created on azure
// for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
string subscriptionId = "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e";
string resourceGroupName = "MyResourceGroup";
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName);
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

// get the collection of this EmailServiceResource
EmailServiceResourceCollection collection = resourceGroupResource.GetEmailServiceResources();

// invoke the operation
string emailServiceName = "MyEmailServiceResource";
EmailServiceResourceData data = new EmailServiceResourceData(new AzureLocation("Global"))
{
    DataLocation = "United States",
};
ArmOperation<EmailServiceResource> lro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, emailServiceName, data);
EmailServiceResource result = lro.Value;

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
EmailServiceResourceData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");

管理电子邮件通信服务资源

更新电子邮件通信服务资源

...
// this example assumes you already have this EmailServiceResource created on azure
// for more information of creating EmailServiceResource, please refer to the document of EmailServiceResource
string subscriptionId = "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e";
string resourceGroupName = "MyResourceGroup";
string emailServiceName = "MyEmailServiceResource";
ResourceIdentifier emailServiceResourceId = EmailServiceResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, emailServiceName);
EmailServiceResource emailServiceResource = client.GetEmailServiceResource(emailServiceResourceId);

// invoke the operation
EmailServiceResourcePatch patch = new EmailServiceResourcePatch()
{
    Tags =
    {
    ["newTag"] = "newVal",
    },
};
ArmOperation<EmailServiceResource> lro = await emailServiceResource.UpdateAsync(WaitUntil.Completed, patch);
EmailServiceResource result = lro.Value;

// the variable result is a resource, you could call other operations on this instance as well
// but just for demo, we get its data from this resource instance
EmailServiceResourceData resourceData = result.Data;
// for demo we just print out the id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");

按资源组列出所有电子邮件通信服务资源

// this example assumes you already have this ResourceGroupResource created on azure
// for more information of creating ResourceGroupResource, please refer to the document of ResourceGroupResource
string subscriptionId = "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e";
string resourceGroupName = "MyResourceGroup";
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName);
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

// get the collection of this EmailServiceResource
EmailServiceResourceCollection collection = resourceGroupResource.GetEmailServiceResources();

// invoke the operation and iterate over the result
await foreach (EmailServiceResource item in collection.GetAllAsync())
{
    // the variable item is a resource, you could call other operations on this instance as well
    // but just for demo, we get its data from this resource instance
    EmailServiceResourceData resourceData = item.Data;
    // for demo we just print out the id
    Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}

Console.WriteLine($"Succeeded");

按订阅列出所有电子邮件通信服务资源

// this example assumes you already have this SubscriptionResource created on azure
// for more information of creating SubscriptionResource, please refer to the document of SubscriptionResource
string subscriptionId = "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e";
ResourceIdentifier subscriptionResourceId = SubscriptionResource.CreateResourceIdentifier(subscriptionId);
SubscriptionResource subscriptionResource = client.GetSubscriptionResource(subscriptionResourceId);

// invoke the operation and iterate over the result
await foreach (EmailServiceResource item in subscriptionResource.GetEmailServiceResourcesAsync())
{
    // the variable item is a resource, you could call other operations on this instance as well
    // but just for demo, we get its data from this resource instance
    EmailServiceResourceData resourceData = item.Data;
    // for demo we just print out the id
    Console.WriteLine($"Succeeded on id: {resourceData.Id}");
}

Console.WriteLine($"Succeeded");

清理资源

// this example assumes you already have this EmailServiceResource created on azure
// for more information of creating EmailServiceResource, please refer to the document of EmailServiceResource
string subscriptionId = "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e";
string resourceGroupName = "MyResourceGroup";
string emailServiceName = "MyEmailServiceResource";
ResourceIdentifier emailServiceResourceId = EmailServiceResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, emailServiceName);
EmailServiceResource emailServiceResource = client.GetEmailServiceResource(emailServiceResourceId);

// invoke the operation
await emailServiceResource.DeleteAsync(WaitUntil.Completed);

Console.WriteLine($"Succeeded");

注释

资源删除是永久性的。如果删除资源,则无法恢复任何数据,包括事件网格筛选器、电话号码或与资源相关的其他数据

先决条件

创建电子邮件通信服务资源

若要创建电子邮件通信服务资源,请使用 Connect-AzAccount 以下命令登录到 Azure 帐户并提供凭据。

PS C:\> Connect-AzAccount

首先,使用以下命令安装 Azure 通信服务模块 Az.Communication

PS C:\> Install-Module Az.Communication

若要创建资源,请运行以下命令:

PS C:\> New-AzEmailService -ResourceGroupName ContosoResourceProvider1 -Name ContosoEmailServiceResource1 -DataLocation UnitedStates

如果要选择特定的订阅,还可以指定 --subscription 标志并提供订阅 ID。

PS C:\> New-AzEmailService -ResourceGroupName ContosoResourceProvider1 -Name ContosoEmailServiceResource1 -DataLocation UnitedStates -SubscriptionId SubscriptionID

可使用以下选项来配置通信服务资源:

  • 资源组
  • 电子邮件通信服务资源的名称。
  • 要与资源关联的地理位置。

在下一步中,可以为资源分配标记。 使用标记来组织 Azure 电子邮件资源。 有关标记的详细信息,请参阅 资源标记

管理电子邮件通信服务资源

若要将标记添加到电子邮件通信服务资源,请运行以下命令。 也可以指向特定的订阅。

PS C:\> Update-AzEmailService -Name ContosoEmailServiceResource1 -ResourceGroupName ContosoResourceProvider1 -Tag @{ExampleKey1="ExampleValue1"}

PS C:\> Update-AzEmailService -Name ContosoEmailServiceResource1 -ResourceGroupName ContosoResourceProvider1 -Tag @{ExampleKey1="ExampleValue1"} -SubscriptionId SubscriptionID

若要列出给定订阅中的所有电子邮件通信服务资源,请使用以下命令:

PS C:\> Get-AzEmailService -SubscriptionId SubscriptionID

若要列出给定资源的所有相关信息,请使用以下命令:

PS C:\> Get-AzEmailService -Name ContosoEmailServiceResource1 -ResourceGroupName ContosoResourceProvider1

清理资源

如果要清理和删除电子邮件通信服务资源,可以通过运行以下命令删除电子邮件通信资源:

PS C:\> Remove-AzEmailService -Name ContosoEmailServiceResource1 -ResourceGroupName ContosoResourceProvider1

注释

资源删除是永久性的。如果删除资源,则无法恢复任何数据,包括事件网格筛选器、电话号码或与资源相关的其他数据

后续步骤