Azure Communication Service(ACS)の一部であるメール送信機能はAzure 側の設定さえすましてしまえば、
プログラムレベルではpacakgeが用意されている言語であれば簡単に
メール送信を実現することができます
その一方で、メール送信結果(例えば、メールアドレスが存在しなくて不達とか)を受け取るには
少し実装をする必要があります
例えば、C#ではAzure.Communication.Email
というライブラリが準備されており、
EmailClient.SendAsyncメソッドでメール送信を行うことができますが、
SendAsyncはメール操作の結果のみを返り値に持つのでメール送信結果の取得まではできません
実際のメール送信結果を取得するにはEmailDeliveryReportReceived
をEventGridTrigger等でサブスクライブして受け取る必要があります
EmailDeliveryReportReceive
EmailDeliveryReportReceiveをサブスクライブするためには、通常にEventGridと同様にイベントサブスクリプションを作成してpushするイベントとしてEmailDeliveryReportReceivedを選択します
また、通知するエンドポイントを設定します
エンドポイントはWebhookの通知を受けとることができれば何でもよいですが、
Azure の世界で完結させるのであれば
ノーコードであればAzure Logic Appsが、煩雑な実装するのであればAzure FuncionsのEventGridTriggerが良いのではないでしょうか
私は、EventGridTriggerが一番楽なのでEventGridTriggerで実装しました
EventGridTrigger
実装自体は通常のEventGridTriggerと大差ないです
以下はAzure Functions の Azure Event Grid トリガー | Microsoft Learnより
using Azure.Messaging; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.EventGrid; using Microsoft.Extensions.Logging; namespace Company.Function { public static class CloudEventTriggerFunction { [FunctionName("CloudEventTriggerFunction")] public static void Run( ILogger logger, [EventGridTrigger] CloudEvent e) { logger.LogInformation("Event received {type} {subject}", e.Type, e.Subject); } } }
実際の中身は以下
[{ "id": "00000000-0000-0000-0000-000000000000", "topic": "/subscriptions/{subscription-id}/resourceGroups/{group-name}/providers/microsoft.communication/communicationservices/{communication-services-resource-name}", "subject": "sender/senderid@azure.com/message/00000000-0000-0000-0000-000000000000", "data": { "sender": "senderid@azure.com", "recipient": "receiver@azure.com", "messageId": "00000000-0000-0000-0000-000000000000", "status": "Delivered", "deliveryStatusDetails": { "statusMessage": "Status Message" }, "deliveryAttemptTimeStamp": "2020-09-18T00:22:20.2855749Z", }, "eventType": "Microsoft.Communication.EmailDeliveryReportReceived", "dataVersion": "1.0", "metadataVersion": "1", "eventTime": "2020-09-18T00:22:20Z" }]
Azure Communication Services - Email イベント - Azure Event Grid | Microsoft Learn
実際のメール送信結果はdata.status
もしくはdata.deliveryStatusDetails.statusMessage
の中にいます
statusがとりうる値としてDelivered
、Expanded
、Bounced
、Suppressed
、FilteredSpam
、Failed
が定義されていますが
現状、これらがOutlookExchangeのどのエラーコードに対応しているかを示すパブリックな情報はなさそうでした
なので、その辺はstatusとstatusMessageから推測するしかなそうです
参考
Simpler, Faster Azure Communication Services Email Now Generally Available (microsoft.com)