次の方法で共有


SMTP を使用して電子メールを送信する

この記事では、簡易メール転送プロトコル (SMTP) を使用して電子メールを送信する方法について説明します。

[前提条件]

この記事を完了すると、ご利用の Azure アカウントでわずかな (米国ドルで数セント未満の) コストが発生します。

独自の確認済みドメインからメールを送信することもできます。 メール通信サービスにカスタム検証済みドメインを追加する

この記事では、SMTP を使用して Azure Communication Services で電子メールを送信する方法について説明します。

前提条件のチェック

  • ターミナルまたはコマンド ウィンドウで dotnet コマンドを実行して、.NET クライアント ライブラリがインストールされていることを確認します。
  • Azure Communication Email Resource に関連付けられているサブドメインを表示するには、 Azure portal にサインインします。 Azure Communication Email リソースを見つけて、左側のナビゲーション ウィンドウから [ ドメインのプロビジョニング ] タブを開きます。

新しい C# アプリケーションを作成する

コンソール ウィンドウ (cmd、PowerShell、Bash など) で、dotnet new コマンドを使用し、EmailQuickstart という名前で新しいコンソール アプリを作成します。 このコマンドは、単一のソース ファイル (Program.cs) を使用して、単純な "Hello World" C# プロジェクトを作成します。

dotnet new console -o EmailSmtpQuickstart

新しく作成したアプリ フォルダーにディレクトリを変更し、dotnet build コマンドを使用してアプリケーションをコンパイルします。

cd EmailSmtpQuickstart
dotnet build

メール メッセージを作成する

電子メール メッセージを作成するには、次の手順を実行する必要があります。

  • Microsoft Entra ID を使用して SMTP 認証資格情報を定義します。
  • 電子メールの件名と本文を定義します。
  • 送信者アドレスを定義します。 確認済みドメインから MailFrom アドレスを取得します。
  • 受信者のアドレスを定義します。

実際のドメインの詳細に置き換えて、コンテンツを修正します。 必要に応じて受信者の詳細を追加します。

//Replace with your domain and modify the content, recipient details as required

string smtpAuthUsername = "<SMTP Username>";
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 を使用して電子メールを送信する

メール メッセージを送信するには、次を行う必要があります。

  1. Azure Communication Services ホスト URL と SMTP 認証資格情報を使用して、 SmtpClient を作成します。
  2. MailMessage を作成します。
  3. SmtpClient Send メソッドを使用して送信します。
using System.Net;
using System.Net.Mail;

string smtpAuthUsername = "<SMTP Username>";
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 アカウントでわずかな (数セント未満の) コストが発生します。

また、独自の検証済みドメインからメールを送信することもできます。 メール通信サービスにカスタム検証済みドメインを追加する

この記事では、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 '<SMTP Username>', $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