クイックスタート: SMTP を使ってメールを送信する
このクイック スタートでは、SMTP を使用してメールを送信する方法について説明します。
前提条件
- アクティブなサブスクリプションが含まれる Azure アカウント。 無料でアカウントを作成できます。
- お使いのオペレーティング システムに対応した最新バージョンの .NET Core クライアント ライブラリ。
- Azure Communication Email Resource が作成され、プロビジョニングされたドメインが準備されている。メール通信リソースの作成に関する概要
- メール ドメインと接続文字列に接続されているアクティブな Azure Communication Services リソース。 電子メール リソースと通信リソースを接続して開始する
- Azure Communication Services リソースへのアクセス権がある Entra アプリケーションを使用して作成された SMTP 資格情報。 SMTP を使用してメールを送信するための認証資格情報を作成する方法
このクイックスタートを完了すると、ご利用の Azure アカウントでわずかな (数セント未満の) コストが発生します。
注意
また、独自の検証済みドメインからメールを送信することもできます。 メール通信サービスに Azure マネージド ドメインを追加する方法。
このクイック スタートでは、SMTP を使用して Azure Communication Services でメールを送信する方法について説明します。
前提条件のチェック
- ターミナルまたはコマンド ウィンドウで
dotnet
コマンドを実行して、.NET クライアント ライブラリがインストールされていることを確認します。 - Azure Communication Email Resource に関連付けられているサブドメインを表示するには、Azure portal にサインインし、Azure Communication Email Resource を見つけて、左側のナビゲーション ペインから [ドメインのプロビジョニング] タブを開いてください。
新しい C# アプリケーションを作成する
コンソール ウィンドウ (cmd、PowerShell、Bash など) で、dotnet new
コマンドを使用し、EmailQuickstart
という名前で新しいコンソール アプリを作成します。 このコマンドにより、1 つのソース ファイルを使用する単純な "Hello World" C# プロジェクトが作成されます。Program.cs。
dotnet new console -o EmailSmtpQuickstart
新しく作成したアプリ フォルダーにディレクトリを変更し、dotnet build
コマンドを使用してアプリケーションをコンパイルします。
cd EmailSmtpQuickstart
dotnet build
メール メッセージを作成する
メール メッセージを作成するには、次の手順を行う必要があります。
- Entra アプリケーションを使用して SMTP 認証資格情報を定義します。
- メールの件名と本文を定義します。
- 送信者アドレスを定義します。 検証済みドメインから MailFrom アドレスを取得します。
- 受信者のアドレスを定義します。
ドメインの詳細に置き換え、必要に応じてコンテンツ、受信者の詳細を変更します
//Replace with your domain and modify the content, recipient details as required
string smtpAuthUsername = "<Azure Communication Services Resource name>|<Entra Application Id>|<Entra Application Tenant Id>";
string smtpAuthPassword = "<Entra Application Client Secret>";
string sender = "donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net";
string recipient = "emailalias@contoso.com";
string subject = "Welcome to Azure Communication Service Email SMTP";
string body = "This email message is sent from Azure Communication Service Email using SMTP.";
System.Net.Mail.SmtpClient を使用してメールを送信する
メール メッセージを送信するには、次を行う必要があります。
- Azure Communication Services ホスト URL と SMTP 認証資格情報を使用して SmtpClient を作成してください。
- MailMessage を作成してください。
- SmtpClient の Send メソッドを使用して送信してください。
using System.Net;
using System.Net.Mail;
string smtpAuthUsername = "<Azure Communication Services Resource name>|<Entra Application Id>|<Entra Application Tenant Id>";
string smtpAuthPassword = "<Entra Application Client Secret>";
string sender = "donotreply@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.azurecomm.net";
string recipient = "emailalias@contoso.com";
string subject = "Welcome to Azure Communication Service Email SMTP";
string body = "This email message is sent from Azure Communication Service Email using SMTP.";
string smtpHostUrl = "smtp.azurecomm.net";
var client = new SmtpClient(smtpHostUrl)
{
Port = 587,
Credentials = new NetworkCredential(smtpAuthUsername, smtpAuthPassword),
EnableSsl = true
};
var message = new MailMessage(sender, recipient, subject, body);
try
{
client.Send(message);
Console.WriteLine("The email was successfully sent using Smtp.");
}
catch (Exception ex)
{
Console.WriteLine($"Smtp send failed with the exception: {ex.Message}.");
}
前提条件
- アクティブなサブスクリプションが含まれる Azure アカウント。 無料でアカウントを作成できます。
- Azure Communication Email Resource が作成され、プロビジョニングされたドメインが準備されている。メール通信リソースの作成に関する概要
- メール ドメインと接続文字列に接続されているアクティブな Azure Communication Services リソース。 電子メール リソースと通信リソースを接続して開始する
- Azure Communication Services リソースへのアクセス権がある Entra アプリケーションを使用して作成された SMTP 資格情報。 SMTP を使用してメールを送信するための認証資格情報を作成する方法
このクイックスタートを完了すると、ご利用の Azure アカウントでわずかな (数セント未満の) コストが発生します。
注意
また、独自の検証済みドメインからメールを送信することもできます。 メール通信サービスに Azure マネージド ドメインを追加する方法。
このクイック スタートでは、SMTP を使用して Azure Communication Services でメールを送信する方法について説明します。
Send-MailMessage を使用してメールを送信する
資格情報は、Microsoft PowerShell ユーティリティ Send-MailMessage を使用して確認できます。 構文については、「Send-MailMessage」を参照してください。
必要な PSCredential 形式で資格情報を格納するには、次の PowerShell コマンドを使用してください。
$Password = ConvertTo-SecureString -AsPlainText -Force -String '<Entra Application Client Secret>'
$Cred = New-Object -TypeName PSCredential -ArgumentList '<Azure Communication Services Resource name>|<Entra Application ID>|<Entra Tenant ID>', $Password
次の PowerShell スクリプトを使用すると、メールを送信できます。 [差出人] の値は、確認済みドメインのメールの差出人アドレスです。 [宛先] の値は、送信先のメール アドレスです。
Send-MailMessage -From 'User01 <user01@fabrikam.com>' -To 'User02 <user02@fabrikam.com>' -Subject 'Test mail' -Body 'test' -SmtpServer 'smtp.azurecomm.net' -Port 587 -Credential $Cred -UseSsl