Azure Communication Services を使って Azure マネージドサービスのみでメールを送信出来るようになっていたので試してみた

2022.12.17

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

いわさです。

この記事は 「Azure Advent Calendar 2022」の 17 日目の記事となります。

これまで Azure にはメール送信するためのマネージドサービスがなく SendGrid などの外部サービスを利用する方法が使われていたと思います。
どうやらここ数ヶ月で Azure Communication Services を使った E メール送信というのが出来るようになったようです。知らなかった。

遡って調べてみると、この機能は今年 5 月の Microsoft Build 2022 でアナウンスされていた機能のようでして、現在プレビューで利用が可能です。

まだプレビューということもあり E メール送信システムに必要なリッチな機能が備わっているわけではない印象はありますが、今後の Azure アーキテクチャー設計に影響がある可能性もありおもしろいですね。

この記事ではサービスを導入し、簡単なコンソールアプリケーションからテストメールを送信してみましたので使い方などを紹介します。

本機能は投稿日時点でパブリックプレビューです。
プレビュー機能利用時の使用条件や注意事項はこちらをご確認ください。

Email Communications Service を作成

流れとしてはまずは Email Communications Service のリソースを作成します。
このリソース上で送信メールのドメインなどをプロビジョニングしたりします。
おそらく今後メール送信に関する細かい機能が集約されるのではないかなと勝手に推測しています。

以下のリソースを作成しましょう。
冒頭で触れていますが本日時点でまだプレビューとなっています。

リソースの初期作成パラメータとしてはリージョンとデータロケーションのみです。
リージョンは Global 固定のようです。
データロケーションは選択式ですが、プレビューのためか本日時点では United States のみです。
後述の Azure Communication Services リソースとデータロケーションは一致させる必要があり、Azure Communication Services では Japan が指定可能なので、Email Communications Service も Japan がいつか追加されそうな気がします。

リソースの作成は 1~2 分で完了します。
次にリソースにドメインをプロビジョニングする必要があります。送信ドメインの管理です。

Provision domains メニューから Add domain を選択しドメインを追加します。
選択肢として Azure domain と Custom domain があります。

Azure domain は無料で利用出来る Azure によって管理されているデフォルトドメインです。
初期状態でドメイン認証から SPF, DKIM までセットアップ済みです。
ドメインはdonotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.netのような形式になっています。

Custom domain は自己所有しているドメインを送信ドメインに使用する機能で、各検証のために DNS に検証レコードの作成が必要です。

今回は無料の Azure マネージドドメイン使いました。
無料というのはドメイン発行に料金が発生しないという意味でメール送信が無料という意味ではありません。料金については後述します。

ドメインのプロビジョニングは 1~2 分で完了しました。

Azure Communication Services リソースを作成し紐付け

Azure Communication Services の 1 機能としてメールを送信するので、Azure Communication Services に作成した Email Communications Service を接続します。
こちらも後述しますが、メール送信時の認証キーなどは Azure Communication Services のものを使用します。

今回はリソースがまだ無かったので新規作成します。
注意点としてデータロケーションを Email Communications Service と同じものにする必要があります。

作成出来たら、Email -> Domains から Connect domain を選択し先程作成した Email Communications Service リソースを選択します。

こちらもすぐ完了すると思います。

なお、データロケーションが異なる場合は以下のようにエラーとなりました。

こちらについては冒頭の公式ドキュメントでも言及されています。

We allow only connecting the domains in the same geography. Please ensure that Data location for Communication Resource and Email Communication Resource that was selected during resource creation are the same.

メール送信

では Azure 上のリソース準備が完了しましたのでメール送信を行いましょう。
構築手順はこちらです。

Azure の専用ライブラリが提供されていますのでこちらを利用します。

内部的な実装はこのようになっていて RestClinet が使われています。
SMTP ではないのでその点はご注意を。将来的に SMTP で使えると適用出来るソリューションが広がりそうな気はします。

適当なコンソールアプリケーションを .NET で作成し、NuGet パッケージを追加します。

% dotnet new console
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/iwasa.takahito/work/hoge1217mail/hoge1217mail.csproj:
  Determining projects to restore...
  Restored /Users/iwasa.takahito/work/hoge1217mail/hoge1217mail.csproj (in 31 ms).
Restore succeeded.

% dotnet add package Azure.Communication.Email --prerelease
  Determining projects to restore...
  Writing /var/folders/4d/nhd1bp3d161crsn900wjrprm0000gp/T/tmpDLqPWD.tmp
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/local/share/dotnet/sdk/7.0.100/trustedroots/codesignctl.pem'.
info : X.509 certificate chain validation will use the fallback certificate bundle at '/usr/local/share/dotnet/sdk/7.0.100/trustedroots/timestampctl.pem'.
info : Adding PackageReference for package 'Azure.Communication.Email' into project '/Users/iwasa.takahito/work/hoge1217mail/hoge1217mail.csproj'.
info :   GET https://api.nuget.org/v3/registration5-gz-semver2/azure.communication.email/index.json
info :   OK https://api.nuget.org/v3/registration5-gz-semver2/azure.communication.email/index.json 655ms
info : Restoring packages for /Users/iwasa.takahito/work/hoge1217mail/hoge1217mail.csproj...
info :   GET https://api.nuget.org/v3-flatcontainer/azure.communication.email/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/azure.communication.email/index.json 660ms
info :   GET https://api.nuget.org/v3-flatcontainer/azure.communication.email/1.0.0-beta.1/azure.communication.email.1.0.0-beta.1.nupkg
info :   OK https://api.nuget.org/v3-flatcontainer/azure.communication.email/1.0.0-beta.1/azure.communication.email.1.0.0-beta.1.nupkg 763ms
info :   GET https://api.nuget.org/v3-flatcontainer/azure.core/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/azure.core/index.json 780ms
info :   GET https://api.nuget.org/v3-flatcontainer/azure.core/1.24.0/azure.core.1.24.0.nupkg
info :   OK https://api.nuget.org/v3-flatcontainer/azure.core/1.24.0/azure.core.1.24.0.nupkg 39ms
info : Installed Azure.Communication.Email 1.0.0-beta.1 from https://api.nuget.org/v3/index.json with content hash uBYsK5TqzuY5hJsv8bqRVEJbt+DEuSlcBvkBpIG8NoBXe1Ozx0ZQouNfNU1Rl02W/ZJnyEC8tJrwNlWJhltgsA==.
info : Installed Azure.Core 1.24.0 from https://api.nuget.org/v3/index.json with content hash +/qI1j2oU1S4/nvxb2k/wDsol00iGf1AyJX5g3epV7eOpQEP/2xcgh/cxgKMeFgn3U2fmgSiBnQZdkV+l5y0Uw==.
info : Package 'Azure.Communication.Email' is compatible with all the specified frameworks in project '/Users/iwasa.takahito/work/hoge1217mail/hoge1217mail.csproj'.
info : PackageReference for package 'Azure.Communication.Email' version '1.0.0-beta.1' added to file '/Users/iwasa.takahito/work/hoge1217mail/hoge1217mail.csproj'.
info : Writing assets file to disk. Path: /Users/iwasa.takahito/work/hoge1217mail/obj/project.assets.json
log  : Restored /Users/iwasa.takahito/work/hoge1217mail/hoge1217mail.csproj (in 2.78 sec).

送信時に認証が必要で、接続文字列を使うか Azure AD 認証を行うかどちらかが選べるようです。
今回は接続文字列を使いました。

接続文字列は Azure Communication Services から取得可能です。

材料がすべて揃ったので以下のように最低限のメール送信コードを実装します。

Program.cs

using Azure.Communication.Email;
using Azure.Communication.Email.Models;

var emailClient = new EmailClient("endpoint=https://hogeiwasacom.communication.azure.com/;accesskey=hogehoge/fugafuga==");

var emailContent = new EmailContent("hoge subject");
emailContent.PlainText = "hoge content";
var emailAddresses = new List<EmailAddress> { 
        new EmailAddress("hoge@example.com") { DisplayName = "Friendly Display Name" }
    };
var emailRecipients = new EmailRecipients(emailAddresses);
var emailMessage = new EmailMessage("HogeMailFrom@03ac34f6-38dc-4457-b897-e3fd84ccb8ec.azurecomm.net", emailContent, emailRecipients);
var emailResult = emailClient.Send(emailMessage,CancellationToken.None);

ビルドして実行します。

% dotnet build
MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  hoge1217mail -> /Users/iwasa.takahito/work/hoge1217mail/bin/Debug/net7.0/hoge1217mail.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.69
% dotnet run

メールを受信することが出来ました。
良いですね!

さいごに

本日は Azure Communication Services を使って Azure マネージドサービスのみでメールを送信出来るようになっていたので試してみました。

実際に SMTP サーバーや外部サービスを用意せずに送信することが出来ました。
Azure だけでメール送信周りも完結出来るのはなかなかインパクトがありますね。

ただ、本日時点で Dedicated IP や サプレッションリストなどの機能は備わっていないようで、また SMTP としての利用も出来ないようです。
外部メール送信サービスで利用出来るメール送信運用に必要な機能が必要なシーンも多いと思います。
今後のアップデートに注目したいところですね。

ちなみにプレビュー時の料金は以下のようになっています。
こちらも GA 後にどうなるのか注目ですね。