用于发送电子邮件的Azure通信

k2arahey  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(182)

我看到Azure.Communication.Email是预发布的,所以它可能不工作,但是下面的示例代码挂起了。你知道Azure.Communication.Email是否工作吗?

var connectionString = "endpoint=xxxx";

            // Authenticate the client
            EmailClient emailClient = new EmailClient(connectionString);

            EmailContent emailContent = new EmailContent("Welcome to Azure Communication Service Email APIs.");
            emailContent.PlainText = "This email message is sent from Azure Communication Service Email using .NET SDK.";
            List<EmailAddress> emailAddresses = new List<EmailAddress> { new EmailAddress("hello@test.com") { DisplayName = "Test Person" } };
            EmailRecipients emailRecipients = new EmailRecipients(emailAddresses);
            EmailMessage emailMessage = new EmailMessage("donotreply@test.com", emailContent, emailRecipients);

            try
            {
                SendEmailResult emailResult = emailClient.Send(emailMessage, CancellationToken.None);

            }
            catch (Exception ex) { Console.WriteLine(string.Format("Error: {0}", ex.Message)); }

            Console.WriteLine("Done");

            Console.Read();
vq8itlhq

vq8itlhq1#

    • 我在我的环境中尝试,得到以下结果:**

我遵循此MS-DOCS使用Azure通信服务发送电子邮件。

    • NuGet软件包:**
Azure.Communication.Mail - 1.0.0-beta.1
    • 代码:**
using Azure.Communication.Email;
using Azure.Communication.Email.Models;

var connectionString = "<Connection string of azure communication service>';

EmailClient emailClient = new EmailClient(connectionString);

EmailContent emailContent = new EmailContent("Welcome to Azure Communication Service Email APIs.");
emailContent.PlainText = "This email message is sent from Azure Communication Service Email using .NET SDK.";
List<EmailAddress> emailAddresses = new List<EmailAddress> { new EmailAddress("hihello.com") { DisplayName = "Friendly Display Name" } };
EmailRecipients emailRecipients = new EmailRecipients(emailAddresses);
EmailMessage emailMessage = new EmailMessage("DoNotReply@XXxxxxxxxxxxxxxxxxxx.azurecomm.net", emailContent, emailRecipients);
try
{
    SendEmailResult emailResult = emailClient.Send(emailMessage, CancellationToken.None);

}
catch (Exception ex) { Console.WriteLine(string.Format("Error: {0}", ex.Message)); }

Console.WriteLine("Done");

Console.Read();
    • 控制台:**

请确保Azure通信服务与域已连接。

    • Mail:**通过向收件人发送邮件成功执行了上述代码。

相关问题