適用于 .NET 的 Azure 通訊Email用戶端程式庫 - 1.0.0 版
此套件包含適用于 Email Azure 通訊服務 的 C# SDK。
| 原始程式碼套件 (NuGet) | 產品檔
開始使用
安裝套件
使用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);
範例
傳送具有自動輪詢狀態的簡單電子郵件訊息
若要傳送電子郵件訊息,請從 呼叫 或 SendAsync
函式的 Send
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}");
}
傳送具有手動輪詢狀態的簡單電子郵件訊息
若要傳送電子郵件訊息,請從 呼叫 或 SendAsync
函式的 Send
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
接受 參數的 呼叫 或 SendAsync
函式的多 Send
載。
// 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 Open Source Code of Conduct (Microsoft 開放原始碼管理辦法)。 如需詳細資訊,請參閱管理辦法常見問題集,如有任何其他問題或意見請連絡 opencode@microsoft.com。