我想使用没有队列触发器的 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,这是一个好的做法吗?
谢谢你
HTTP触发器。无需队列触发器即可使用 Azure Functions。根据 @Dai 的说法,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);
}
}
借助 Azure 通信服务,您可以使用自己的 SMTP 域,通过可扩展且可靠的电子邮件功能加速您的市场进入。 Azure Functions 可以根据需求自动扩展。请参阅此