你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 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 发送电子邮件

若要发送电子邮件,需要:

  1. 使用 Azure 通信服务主机 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 通信服务发送电子邮件。

使用 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