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

快速入门:创建和管理电子邮件通信服务资源

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

警告

请注意,不能同时创建一个资源组作为 Azure 通信服务的资源。 创建资源时,必须使用已创建的资源组。

先决条件

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

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

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

    一个屏幕截图,显示了如何在市场中搜索 Email Communication Service。

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

    一个屏幕截图,显示了用于创建 Email Communication Service 的“Create”链接。

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

    • 选择现有的 Azure 订阅。

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

    • 为资源提供有效名称。

    • 选择需要提供资源的区域。

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

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

    • 添加任何名称/值对。

      一个屏幕截图,显示了如何审阅和创建 Email Communication Service 的摘要。

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

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

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

    一个屏幕截图,显示了 Email Communication Service 资源的概览。

先决条件

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

要创建电子邮件通信服务资源,请登录 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;

订阅 ID

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

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

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

身份验证

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

对客户端进行身份验证

创建经过身份验证的客户端的默认选项是使用 DefaultAzureCredential。 由于所有管理 API 都经过同一终结点,因此为了与资源交互,必须仅创建一个顶级 ArmClient。

要向 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 = "11112222-3333-4444-5555-666677778888";
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 = "11112222-3333-4444-5555-666677778888";
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 = "11112222-3333-4444-5555-666677778888";
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 = "11112222-3333-4444-5555-666677778888";
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 = "11112222-3333-4444-5555-666677778888";
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

注意

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

后续步骤