Quickstart: Send email with SMTP

In this quick start, you learn about how to send email using SMTP.

Prerequisites

Completing this quick start incurs a small cost of a few USD cents or less in your Azure account.

Note

We can also send an email from our own verified domain. Add custom verified domains to Email Communication Service.

In this quick start, you'll learn about how to send email with Azure Communication Services using SMTP.

Prerequisite check

  • In a terminal or command window, run the dotnet command to check that the .NET client library is installed.
  • To view the subdomains associated with your Azure Communication Email Resource, sign in to the Azure portal, locate your Azure Communication Email Resource and open the Provision domains tab from the left navigation pane.

Create a new C# application

In a console window (such as cmd, PowerShell, or Bash), use the dotnet new command to create a new console app with the name EmailQuickstart. This command creates a simple "Hello World" C# project with a single source file: Program.cs.

dotnet new console -o EmailSmtpQuickstart

Change your directory to the newly created app folder and use the dotnet build command to compile your application.

cd EmailSmtpQuickstart
dotnet build

Construct your email message

To construct an email message, you need to:

  • Define the Smtp Authentication credentials using an Entra application.
  • Define the Email Subject and Body.
  • Define your Sender Address. You get your MailFrom Address from your Verified Domain.
  • Define the Recipient Address.

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

//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.";

Send an email using System.Net.Mail.SmtpClient

To send an email message, you need to:

  1. Create an SmtpClient using the Azure Communication Services host Url and the Smtp Authentication credentials.
  2. Create a MailMessage.
  3. Send using the SmtpClient's Send method.
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}.");
}

Prerequisites

Completing this quick start incurs a small cost of a few USD cents or less in your Azure account.

Note

We can also send an email from our own verified domain. Add custom verified domains to Email Communication Service.

In this quick start, you'll learn about how to send email with Azure Communication Services using SMTP.

Send an email using Send-MailMessage

The credentials can be verified using the Microsoft PowerShell utility Send-MailMessage. See Send-MailMessage for the syntax.

To store the credentials in the required PSCredential format, use the following PowerShell commands:

$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

The following PowerShell script can be used to send the email. The From value is the mail from address of your verified domain. The To value is the email address that you would like to send to.

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