Quickstart: Send email with SMTP
In this quick start, you learn about how to send email using 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 an Entra application with access to the Azure Communication Services Resource. How to create authentication credentials for sending emails using Smtp
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:
- Create an SmtpClient using the Azure Communication Services host Url and the Smtp Authentication credentials.
- Create a MailMessage.
- 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
- 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 an Entra application with access to the Azure Communication Services Resource. How to create authentication credentials for sending emails using Smtp
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
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for