快速入門:如何將 Azure 受控網域加入至電子郵件通訊服務

在本快速入門中,您會瞭解如何在 Azure 通訊服務 中將 Azure 受控網域布建至電子郵件通訊服務。

必要條件

佈建 Azure 受控網域

  1. 開啟您在開始使用建立電子郵件通訊資源建立的電子郵件通訊服務資源 [概觀] 頁面。

  2. 使用下列其中一個選項建立 Azure 受控網域。

    • (選項 1) 按一下 [加入一個免費的 Azure 子網域] 底下的 [1 鍵加入] 按鈕。 繼續進行步驟 3

    醒目提示新增免費 Azure 受控網域的螢幕快照。

    • (選項 2) 按一下左側導覽面板上的 [佈建網域]

    顯示 [布建網域] 瀏覽頁面的螢幕快照。

    • 按一下上方導覽列上的 [新增網域]
    • 從下拉式清單中選取 [Azure 網域]
  3. 等待部署完成。

    顯示部署進度的螢幕快照。

  4. 建立網域之後,您會看到具有新網域的清單檢視。

    顯示已布建電子郵件網域清單的螢幕快照。

  5. 按兩下布建網域的名稱,以開啟網域資源類型的概觀頁面。

    顯示 Azure 受控網域概觀頁面的螢幕快照。

必要條件

建立網域資源

若要建立網域資源, 請登入 Azure CLI。 您可以從終端機執行 az login 命令,並提供您的認證來登入。 若要建立資源,請執行下列命令:

az communication email domain create --domain-name AzureManagedDomain --email-service-name "<EmailServiceName>" --location "Global" --resource-group "<resourceGroup>" --domain-management AzureManaged

如果您想要選取特定的訂用帳戶,您也可以指定 --subscription 旗標並提供訂用帳戶識別碼。

az communication email domain create --domain-name AzureManagedDomain --email-service-name "<EmailServiceName>" --location "Global" --resource-group "<resourceGroup>" --domain-management AzureManaged --subscription "<subscriptionId>"

您可以使用下列選項來設定網域資源:

  • 資源群組
  • 電子郵件通訊服務資源的名稱
  • 資源要產生關聯的地理位置
  • 網域資源的名稱:
    • 針對 Azure 網域,名稱應該是 AzureManagedDomain。
  • Domain management 屬性的值。
    • 針對 Azure 網域,此值應該是 AzureManaged。

在下一個步驟中,您可以將標籤指派給網域資源。 標記可用來組織您的網域資源。 如需標記的詳細資訊,請參閱 資源標記文件

管理您的網域資源

若要將標籤新增至您的網域資源,請執行下列命令。 您也可以以特定訂用帳戶為目標。

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

az communication email domain update --domain-name AzureManagedDomain --email-service-name "<EmailServiceName>" --resource-group "<resourceGroup>" --tags newTag="newVal1" --subscription "<subscriptionId>"

若要列出指定電子郵件通訊服務中的所有網域資源,請使用下列命令:

az communication email domain list --email-service-name "<EmailServiceName>" --resource-group "<resourceGroup>"

若要顯示指定網域資源上的所有資訊,請使用下列命令:

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

清除網域資源

如果您要清除並移除網域資源,您可以執行下列命令來刪除。

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

注意

資源刪除為永久性,而且如果您刪除資源,則無法復原任何資料,包括事件方格篩選、電話號碼或其他繫結至資源的資料。

如需其他命令的相關信息,請參閱 網域 CLI

必要條件

安裝 SDK

首先,將通訊服務管理 SDK 包含在您的 C# 專案中:

using Azure.ResourceManager.Communication;

訂用帳戶識別碼

您必須知道 Azure 訂用帳戶的識別碼。 可以從入口網站取得:

  1. 登入您的 Azure 帳戶
  2. 選取左邊側邊欄的 [訂用帳戶]
  3. 選取所需的任何訂用帳戶
  4. 按一下 [概觀]
  5. 選取訂用帳戶識別碼

在本快速入門中,我們假設您已將訂用帳戶識別碼儲存在名為 AZURE_SUBSCRIPTION_ID 的環境變數中。

驗證

若要與網域資源通訊,您必須先向 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://video2.skills-academy.com/dotnet/azure/sdk/authentication?tabs=command-line
TokenCredential cred = new DefaultAzureCredential();
// authenticate your client
ArmClient client = new ArmClient(cred);

與 Azure 資源互動

現在您已通過驗證。

針對下列每個範例,我們會將網域資源指派給現有的電子郵件通訊服務。

如果您需要建立電子郵件通訊服務,您可以使用 Azure 入口網站

建立網域資源

建立網域資源時,您必須指定資源組名、電子郵件通訊服務名稱、資源名稱和 DomainManagement。

注意

Location 屬性一律為 global

// 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);

// get the collection of this CommunicationDomainResource
CommunicationDomainResourceCollection collection = emailServiceResource.GetCommunicationDomainResources();

// invoke the operation
string domainName = "AzureManagedDomain";
CommunicationDomainResourceData data = new CommunicationDomainResourceData(new AzureLocation("Global"))
{
    DomainManagement = DomainManagement.AzureManaged,
};
ArmOperation<CommunicationDomainResource> lro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, domainName, data);            
CommunicationDomainResource 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
CommunicationDomainResourceData 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 CommunicationDomainResource created on azure
// for more information of creating CommunicationDomainResource, please refer to the document of CommunicationDomainResource
string subscriptionId = "11112222-3333-4444-5555-666677778888";
string resourceGroupName = "MyResourceGroup";
string emailServiceName = "MyEmailServiceResource";
string domainName = "AzureManagedDomain";
ResourceIdentifier communicationDomainResourceId = CommunicationDomainResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, emailServiceName, domainName);
CommunicationDomainResource communicationDomainResource = client.GetCommunicationDomainResource(communicationDomainResourceId);

// invoke the operation
CommunicationDomainResourcePatch patch = new CommunicationDomainResourcePatch()
{
    Tags =
    {
    ["newTag"] = "newVal",
    },
};
ArmOperation<CommunicationDomainResource> lro = await communicationDomainResource.UpdateAsync(WaitUntil.Completed, patch);
CommunicationDomainResource 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
CommunicationDomainResourceData 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);

// get the collection of this CommunicationDomainResource
CommunicationDomainResourceCollection collection = emailServiceResource.GetCommunicationDomainResources();

// invoke the operation and iterate over the result
await foreach (CommunicationDomainResource 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
    CommunicationDomainResourceData 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);

// get the collection of this CommunicationDomainResource
CommunicationDomainResourceCollection collection = emailServiceResource.GetCommunicationDomainResources();

// invoke the operation
string domainName = "AzureManagedDomain";
bool result = await collection.ExistsAsync(domainName);

Console.WriteLine($"Succeeded: {result}");

清除網域資源

// this example assumes you already have this CommunicationDomainResource created on azure
// for more information of creating CommunicationDomainResource, please refer to the document of CommunicationDomainResource
string subscriptionId = "11112222-3333-4444-5555-666677778888";
string resourceGroupName = "MyResourceGroup";
string emailServiceName = "MyEmailServiceResource";
string domainName = "AzureManagedDomain";
ResourceIdentifier communicationDomainResourceId = CommunicationDomainResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, emailServiceName, domainName);
CommunicationDomainResource communicationDomainResource = client.GetCommunicationDomainResource(communicationDomainResourceId);

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

Console.WriteLine($"Succeeded");

注意

資源刪除為永久性,而且如果您刪除資源,則無法復原任何資料,包括事件方格篩選、電話號碼或其他繫結至資源的資料。

必要條件

建立網域資源

若要建立網域資源,請使用 Connect-AzAccount 下列命令登入您的 Azure 帳戶,並提供您的認證。

PS C:\> Connect-AzAccount

首先,請務必使用下列命令安裝 Azure 通訊服務模組 Az.Communication

PS C:\> Install-Module Az.Communication

執行下列命令以建立 Azure 受控網域資源:

PS C:\> New-AzEmailServiceDomain -ResourceGroupName ContosoResourceProvider1 -EmailServiceName ContosoEmailServiceResource1 -Name AzureManagedDomain -DomainManagement AzureManaged

您可以使用下列選項來設定網域資源:

  • 資源群組
  • 電子郵件通訊服務資源的名稱。
  • 網域資源的名稱:
    • 針對 Azure 網域,名稱應該是 - AzureManagedDomain。
  • Domain management 屬性的值。
    • 針對 Azure 網域,此值應該是 AzureManaged。

在下一個步驟中,您可以將標籤指派給網域資源。 標記可用來組織您的網域資源。 如需標記的詳細資訊,請參閱 資源標記文件

管理您的網域資源

若要將標籤新增至您的網域資源,請執行下列命令。 您也可以以特定訂用帳戶為目標。

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

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

若要列出指定電子郵件通訊服務中的所有網域資源,請使用下列命令:

PS C:\> Get-AzEmailServiceDomain -EmailServiceName ContosoEmailServiceResource1 -ResourceGroupName ContosoResourceProvider1

若要列出指定網域資源上的所有資訊,請使用下列命令:

PS C:\> Get-AzEmailServiceDomain -Name AzureManagedDomain -EmailServiceName ContosoEmailServiceResource1 -ResourceGroupName ContosoResourceProvider1

清除網域資源

如果您要清除和移除網域資源,您可以執行下列命令來刪除網域資源:

PS C:\> Remove-AzEmailServiceDomain -Name AzureManagedDomain -EmailServiceName ContosoEmailServiceResource1 -ResourceGroupName ContosoResourceProvider1

注意

資源刪除為永久性,而且如果您刪除資源,則無法復原任何資料,包括事件方格篩選、電話號碼或其他繫結至資源的資料。

相較於自定義網域的 Azure 受控網域

布建 Azure 受控網域之前,請檢閱下表,以決定哪一個網域類型最符合您的需求。

Azure 受控網域 自訂網域
優點: - 設定快速又簡單
- 不需要網域驗證
- 電子郵件是從您自己的網域傳送
缺點: - 寄件者網域未個人化且無法變更
- 寄件者使用者名稱無法個人化
- 傳送量非常有限
- 無法啟用使用者參與追蹤
- 需要驗證網域記錄
- 驗證的設定時間較長

Azure 受控網域的傳送者驗證

Azure 通訊服務 會自動設定電子郵件所需的電子郵件驗證通訊協定,如中所述電子郵件驗證最佳做法

您的電子郵件網域現在已準備好傳送電子郵件。

下一步