你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
本文介绍如何使用简单邮件传输协议(SMTP)发送电子邮件。
先决条件
- 具有活动订阅的 Azure 帐户。 免费创建帐户。
- 适用于你的操作系统的最新版本 .NET Core 客户端库。
- 已使用预配的域创建并准备好一个 Azure 通信服务电子邮件资源。 创建电子邮件通信资源入门
- 一个与电子邮件域连接的活动 Azure 通信服务资源,以及一个连接字符串。 开始将电子邮件资源与通信资源相连接
- 使用有权访问 Azure 通信服务资源的 Microsoft Entra 应用程序创建的 SMTP 凭据。 为简单邮件传输协议 (SMTP) 身份验证创建凭据
完成本文需要从你的 Azure 帐户中扣取最多几美分的费用。
注释
还可以从自己的已验证域发送电子邮件。 将自定义的已验证域添加到电子邮件通信服务。
本文介绍如何使用 SMTP 通过 Azure 通信服务发送电子邮件。
先决条件检查
- 在终端或命令窗口中,运行
dotnet
命令来查看是否安装了 .NET 客户端。 - 若要查看与 Azure 通信电子邮件资源关联的子域,请登录到 Azure 门户。 找到 Azure 通信电子邮件资源,然后从左侧导航窗格中打开“ 预配域 ”选项卡。
新建 C# 应用程序
在控制台窗口(例如 cmd、PowerShell 或 Bash)中,使用 dotnet new
命令创建名为 EmailQuickstart
的新控制台应用。 此命令使用单个源文件创建一个简单的“Hello World”C# 项目:Program.cs。
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 发送电子邮件
若要发送电子邮件,需要:
- 使用 Azure 通信服务主机 URL 和 SMTP 身份验证凭据创建
SmtpClient
。 - 创建 MailMessage。
- 使用
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 帐户。 免费创建帐户。
- 已使用预配的域创建并准备好一个 Azure 通信服务电子邮件资源。 创建电子邮件通信资源入门
- 一个与电子邮件域连接的活动 Azure 通信服务资源,以及一个连接字符串。 开始将电子邮件资源与通信资源相连接
- 使用有权访问 Azure 通信服务资源的 Microsoft Entra 应用程序创建的 SMTP 凭据。 为简单邮件传输协议 (SMTP) 身份验证创建凭据
完成本快速入门会从你的 Azure 帐户中扣取最多几美分的费用。
注释
我们还可以从我们自己的已验证域发送电子邮件。 将自定义的已验证域添加到电子邮件通信服务。
本文介绍如何使用 SMTP 通过 Azure 通信服务发送电子邮件。
使用 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 脚本发送电子邮件。 “发件人”值是来自经验证域的地址的邮件。 “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