Azure 电子邮件服务和 Azure 队列触发器 - 我可以在没有队列的情况下使用 ACS 吗?是否可扩展?

问题描述 投票:0回答:1

我想使用没有队列触发器的 ACS,但我不确定是否只有 ACS 可以处理电子邮件发送过程以及它是否可扩展。

  [Function(nameof(Function1))]
 public void Run([QueueTrigger("queueName", Connection = "ConnectionName")] QueueMessage message)
 {
     _logger.LogInformation($"C# Queue trigger function processed: {message.MessageText}");
 }

我仅使用 ACS 来发送邀请,请告诉我是否可以仅使用 ACS,这是一个好的做法吗?

谢谢你

azure azure-functions acs azure-communication-services
1个回答
0
投票
每当消息添加到 Azure 队列存储时,队列存储触发器就会运行一个函数。尽管队列触发器可以激活函数来响应队列事件,但最好根据用例使用

HTTP触发器。无需队列触发器即可使用 Azure Functions。根据 @Dai 的说法,Azure 通信服务不需要队列。

创建电子邮件通信服务和域,设置通信服务并参考此

MSDOC 连接 Azure 通信服务中经过验证的电子邮件域。

我参考了此

文档,通过 C# 使用 Azure 通信服务发送电子邮件。我还参考了 Azure Functions 的文档

以下示例代码是在azure函数中使用http触发器使用Azure通信服务发送电子邮件。

using System; using System.Threading.Tasks; using Azure; using Azure.Communication.Email; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; public static class Function1 { [FunctionName("SendEmailFunction")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "post","get", Route = "send-email")] HttpRequest req, ILogger log) { log.LogInformation("Processing email send request."); string connectionString = "COMMUNICATION_SERVICES_CONNECTION_STRING"; EmailClient emailClient = new EmailClient(connectionString); var subject = "Invitation to Join Us"; var htmlContent = "<html><body><h1>You're Invited!</h1><p>Click <a href='https://example.com'>here</a> to join us.</p></body></html>"; var sender = "[email protected]"; // Replace with your sender address var recipient = "[email protected]"; // Replace with recipient address try { EmailSendOperation emailSendOperation = await emailClient.SendAsync( Azure.WaitUntil.Started, sender, recipient, subject, htmlContent ); while (true) { await emailSendOperation.UpdateStatusAsync(); if (emailSendOperation.HasCompleted) { break; } await Task.Delay(100); } if (emailSendOperation.HasValue) { log.LogInformation($"Email queued for delivery. Status = {emailSendOperation.Value.Status}"); return new OkObjectResult($"Email sent successfully. Operation ID = {emailSendOperation.Id}"); } } catch (RequestFailedException ex) { log.LogError($"Email send failed with Code = {ex.ErrorCode} and Message = {ex.Message}"); return new StatusCodeResult(StatusCodes.Status500InternalServerError); } return new StatusCodeResult(StatusCodes.Status500InternalServerError); } }

enter image description here

enter image description here

借助 Azure 通信服务,您可以使用自己的 SMTP 域,通过可扩展且可靠的电子邮件功能加速您的市场进入。 Azure Functions 可以根据需求自动扩展。请参阅此

MSDOC,了解有关 Azure Functions 扩展和托管的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.