你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
适用于 .NET 的 Azure 通信Email客户端库 - 版本 1.0.1
此包包含用于 Email Azure 通信服务 的 C# SDK。
入门
安装包
使用 NuGet 安装适用于 .NET 的 Azure 通信Email客户端库:
dotnet add package Azure.Communication.Email
先决条件
需要 Azure 订阅、通信服务资源和具有活动域的Email通信资源。
若要创建这些资源,可以使用 Azure 门户、Azure PowerShell或 .NET 管理客户端库。
关键概念
EmailClient
提供发送电子邮件的功能。
使用语句
using Azure.Communication.Email;
验证客户端
Email客户端可以使用从 Azure 门户中的 Azure 通信资源获取的连接字符串进行身份验证。
var connectionString = "<connection_string>"; // Find your Communication Services resource in the Azure portal
EmailClient emailClient = new EmailClient(connectionString);
或者,也可以使用有效的令牌凭据对Email客户端进行身份验证。 在此选项中,AZURE_CLIENT_SECRET
、AZURE_CLIENT_ID
和 AZURE_TENANT_ID
环境变量需要设置为可执行身份验证。
string endpoint = "<endpoint_url>";
TokenCredential tokenCredential = new DefaultAzureCredential();
tokenCredential = new DefaultAzureCredential();
EmailClient emailClient = new EmailClient(new Uri(endpoint), tokenCredential);
示例
发送带有状态自动轮询的简单电子邮件
若要发送电子邮件,请从 调用 或 Send
SendAsync
函数的 EmailClient
简单重载。
try
{
var emailSendOperation = emailClient.Send(
wait: WaitUntil.Completed,
senderAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
recipientAddress: "<recipient email address>"
subject: "This is the subject",
htmlContent: "<html><body>This is the html body</body></html>");
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
}
catch ( RequestFailedException ex )
{
/// OperationID is contained in the exception message and can be used for troubleshooting purposes
Console.WriteLine($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
}
使用手动轮询状态发送简单电子邮件
若要发送电子邮件,请从 调用 或 Send
SendAsync
函数的 EmailClient
简单重载。
/// Send the email message with WaitUntil.Started
var emailSendOperation = await emailClient.SendAsync(
wait: WaitUntil.Started,
senderAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
recipientAddress: "<recipient email address>"
subject: "This is the subject",
htmlContent: "<html><body>This is the html body</body></html>");
/// Call UpdateStatus on the email send operation to poll for the status
/// manually.
try
{
while (true)
{
await emailSendOperation.UpdateStatusAsync();
if (emailSendOperation.HasCompleted)
{
break;
}
await Task.Delay(100);
}
if (emailSendOperation.HasValue)
{
Console.WriteLine($"Email queued for delivery. Status = {emailSendOperation.Value.Status}");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Email send failed with Code = {ex.ErrorCode} and Message = {ex.Message}");
}
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
发送包含更多选项的电子邮件
若要发送电子邮件,请从EmailClient
采用 EmailMessage
参数的 Send
SendAsync
调用 或 函数的重载。
// Create the email content
var emailContent = new EmailContent("This is the subject")
{
PlainText = "This is the body",
Html = "<html><body>This is the html body</body></html>"
};
// Create the EmailMessage
var emailMessage = new EmailMessage(
senderAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
recipientAddress: "<recipient email address>"
content: emailContent);
try
{
var emailSendOperation = emailClient.Send(
wait: WaitUntil.Completed,
message: emailMessage);
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
}
catch ( RequestFailedException ex )
{
/// OperationID is contained in the exception message and can be used for troubleshooting purposes
Console.WriteLine($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
}
向多个收件人发送电子邮件
若要向多个收件人发送电子邮件,请将每种食谱类型的对象添加到 EmailAddress
对象 EmailRecipient
。
// Create the email content
var emailContent = new EmailContent("This is the subject")
{
PlainText = "This is the body",
Html = "<html><body>This is the html body</body></html>"
};
// Create the To list
var toRecipients = new List<EmailAddress>
{
new EmailAddress(
address: "<recipient email address>"
displayName: "<recipient displayname>"
new EmailAddress(
address: "<recipient email address>"
displayName: "<recipient displayname>"
};
// Create the CC list
var ccRecipients = new List<EmailAddress>
{
new EmailAddress(
address: "<recipient email address>"
displayName: "<recipient displayname>"
new EmailAddress(
address: "<recipient email address>"
displayName: "<recipient displayname>"
};
// Create the BCC list
var bccRecipients = new List<EmailAddress>
{
new EmailAddress(
address: "<recipient email address>"
displayName: "<recipient displayname>"
new EmailAddress(
address: "<recipient email address>"
displayName: "<recipient displayname>"
};
var emailRecipients = new EmailRecipients(toRecipients, ccRecipients, bccRecipients);
// Create the EmailMessage
var emailMessage = new EmailMessage(
senderAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
emailRecipients,
emailContent);
try
{
EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage);
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
}
catch ( RequestFailedException ex )
{
/// OperationID is contained in the exception message and can be used for troubleshooting purposes
Console.WriteLine($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
}
发送带有附件的电子邮件
Azure 通信服务支持发送包含附件的电子邮件。
// Create the EmailMessage
var emailMessage = new EmailMessage(
senderAddress: "<Send email address>" // The email address of the domain registered with the Communication Services resource
recipientAddress: "<recipient email address>"
content: emailContent);
var filePath = "<path to your file>";
var attachmentName = "<name of your attachment>";
var contentType = MediaTypeNames.Text.Plain;
var content = new BinaryData(System.IO.File.ReadAllBytes(filePath));
var emailAttachment = new EmailAttachment(attachmentName, contentType, content);
emailMessage.Attachments.Add(emailAttachment);
try
{
EmailSendOperation emailSendOperation = emailClient.Send(WaitUntil.Completed, emailMessage);
Console.WriteLine($"Email Sent. Status = {emailSendOperation.Value.Status}");
/// Get the OperationId so that it can be used for tracking the message for troubleshooting
string operationId = emailSendOperation.Id;
Console.WriteLine($"Email operation id = {operationId}");
}
catch ( RequestFailedException ex )
{
/// OperationID is contained in the exception message and can be used for troubleshooting purposes
Console.WriteLine($"Email send operation failed with error code: {ex.ErrorCode}, message: {ex.Message}");
}
疑难解答
对于 RequestFailedException
任何不成功的请求,会作为服务响应引发 。 异常包含有关从服务返回的响应代码的信息。
后续步骤
贡献
本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 cla.microsoft.com。
此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,或如果有任何其他问题或意见,请与 联系。