Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article describes how to send email using Simple Mail Transfer Protocol (SMTP).
Prerequisites
- An Azure account with an active subscription. Create an account for free.
- The latest version .NET Core client library for your operating system.
- An Azure Communication Email Resource created and ready with a provisioned domain. Get started with Creating Email Communication Resource
- An active Azure Communication Services Resource connected with Email Domain and a Connection String. Get started by Connecting Email Resource with a Communication Resource
- SMTP credentials created using a Microsoft Entra application with access to the Azure Communication Services Resource. Create credentials for Simple Mail Transfer Protocol (SMTP) authentication
Completing this article incurs a small cost of a few USD cents or less in your Azure account.
Note
You can also send an email from your own verified domain. Add custom verified domains to Email Communication Service.
This article describes 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 Microsoft Entra ID.
- Define the Email Subject and Body.
- Define your Sender Address. Get your MailFrom Address from your Verified Domain.
- Define the Recipient Address.
Replace with your domain details and modify the content. Add recipient details as required.
//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.";
Send an email using System.Net.Mail.SmtpClient
To send an email message, you need to:
- Create an
SmtpClient
using the Azure Communication Services host URL and the SMTP Authentication credentials. - Create a MailMessage.
- Send using the
SmtpClient
Send method.
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}.");
}
Prerequisites
- An Azure account with an active subscription. Create an account for free.
- An Azure Communication Email Resource created and ready with a provisioned domain. Get started with Creating Email Communication Resource
- An active Azure Communication Services Resource connected with Email Domain and a Connection String. Get started by Connecting Email Resource with a Communication Resource
- SMTP credentials created using a Microsoft Entra application with access to the Azure Communication Services Resource. Create credentials for Simple Mail Transfer Protocol (SMTP) authentication
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.
This article describes 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 '<SMTP Username>', $Password
Use the following PowerShell script 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 want 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