👋

【Azure Communication Service】- メールの送信を試す

2025/01/09に公開

執筆日

2024/01/09

やること

Azure Communication Serviceのメール機能を試します。

セットアップ手順

  1. Azure Communication Servicesを構築する
  2. Email Communication Servicesを構築する
  1. 左タブのメール>メールを試すをクリックする
  2. 無料のAzureサブドメインを設定するをクリックする
  3. 2構築したEmail Communication Servicesを選択し、作成とアクティブ化をクリックする
  4. 左タブのメール>ドメインをクリックする
  5. ドメインを接続するをクリックする
  6. 2構築したEmail Communication Servicesを選択し、確認済みのドメインを選択する。
  7. 接続をクリックする

試す

  1. 左タブのメール>メールを試すをクリックする
  2. メールの送信元、受信者のメールアドレスを設定し、sendをクリックする
  3. メールが届いていることを確認する
  4. codeで実行する
pip install azure-communication-email
main.py
from azure.communication.email import EmailClient

def main():
    try:
        connection_string = "ACSの接続文字列"
        client = EmailClient.from_connection_string(connection_string)

        message = {
            "senderAddress": "取得したメールアドレス",
            "recipients": {"to": [{"address": "送信したメールアドレス"}]},
            "content": {
                "subject": "テスト メール",
                "plainText": "メールで Hello World。",
                "html": """
				<html>
					<body>
						<h1>メールで Hello World。</h1>
					</body>
				</html>""",
            },
        }

        poller = client.begin_send(message)
        result = poller.result()
        print("メールを送信しました。")

    except Exception as ex:
        print(ex)


main()

カスタムドメインの設定手順

https://learn.microsoft.com/ja-jp/azure/communication-services/quickstarts/email/add-custom-verified-domains?pivots=platform-azp

料金

従量課金制になります。料金の観点が2つあり、メール1通1MB単位で発生します。
2025/1/9時点で、メール1通あたり$0.00025、1MBあたり$0.00012という価格になっています。
https://azure.microsoft.com/ja-jp/pricing/details/communication-services/

ヘッドウォータース

Discussion