c# - 如何使用azure通信服务从web api发送电子邮件

标签 c# azure email azure-communication-services

我正在使用 Azure 通信服务发送电子邮件,现在我想在 Web API 中执行此操作。每当我调用 Web API post 函数时,记录都将存储在数据库中,并且必须使用其电子邮件 ID 作为电子邮件发送给该特定用户。

[HttpPost]
    public async Task<ActionResult<UserTable>> Post([FromBody] UserTable userinfo)
    {
        try
        {            
            var item = await _userTableRepository.AddUser(userinfo);          
            string recipientEmail = userinfo.Email;
            string subject = "Welcome to our platform";
            string body = $"Hello {userinfo.UserId}";
            await SendEmail(recipientEmail,subject, body);
            return Ok(item);
        }
        catch (Exception)
        {
            return StatusCode(StatusCodes.Status500InternalServerError, "Error sending email or saving data");
        }
    }

    private async Task SendEmail(string recipientEmail, string subject, string body)
    {
        var client = new EmailClient(emailconnectionstring);
        var emailMessage = new EmailMessage()
        {
            From = new EmailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7506101b11100735100d14180519105b161a18" rel="noreferrer noopener nofollow">[email protected]</a>"),
            To = new List<EmailAddress>()
            {
                new EmailAddress(recipientEmail)
            },
            Subject = subject,
            Body = new EmailBody()
            {
                ContentType = EmailBodyType.Text,
                Content = body
            }
        };
        await client.SendAsync(emailMessage);
    }

这是我使用 Azure 通信服务发送电子邮件的代码。但我遇到了很多错误,当我单独尝试电子邮件代码时,它工作得很好。当我在 Web API 中使用它时,出现这种错误。

  1. 错误 CS1729“EmailMessage”不包含采用 0 个参数的构造函数
  2. 错误 CS0117“EmailMessage”不包含“发件人”的定义
  3. 错误 CS0117“EmailMessage”不包含“收件人”的定义
  4. 错误 CS0117“EmailMessage”不包含“主题”的定义
  5. 错误 CS0117“EmailMessage”不包含“Body”的定义
  6. 错误 CS0246 找不到类型或命名空间名称“EmailBody”(是否缺少 using 指令或程序集引用?)
  7. 错误 CS0103 当前上下文中不存在名称“EmailBodyType”
  8. 错误 CS1501“SendAsync”方法没有重载需要 1 个参数

我尝试解决这个问题,但没有成功。由于我是新手,所以我不知道这是对还是错。代码中有什么我遗漏的吗?还有其他方法可以使用 Azure 通信服务从 Web API 发送电子邮件吗?
谢谢!!!

最佳答案

EmailMessage 没有默认构造函数。请参阅here .

将电子邮件发送给多个收件人还需要一个额外的 EmailRecipients 变量,它支持“收件人”、“抄送”和“密件抄送”。请参阅here .

针对您的案例修改了代码。

private async Task SendEmail(string recipientEmail, string subject, string body)
{
    var client = new EmailClient(emailconnectionstring);
    
    // Fill the EmailMessage
    var sender = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd8e989399988fbd98859c908d9198d39e9290" rel="noreferrer noopener nofollow">[email protected]</a>";
    var subject = subject;

    var emailContent = new EmailContent(subject)
    {
        PlainText = body
    };
    
    var toRecipients = new List<EmailAddress>()
    {
        new EmailAddress(recipientEmail)
    };
    
    var emailRecipients = new EmailRecipients(toRecipients);

    var emailMessage = new EmailMessage(sender, emailRecipients, emailContent);
    
    await client.SendAsync(emailMessage);
}

请参阅GitHub repo获取更多样本。

关于c# - 如何使用azure通信服务从web api发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76452135/

相关文章:

email - 跟踪大规模电子邮件事件

c# - 从日期时间中去除秒数

c# - C# 中可读的 C++\CLI 异常消息?

c# - AngularJS Web Api AntiForgeryToken CSRF

azure - 无法删除 azure 上的 docker 镜像,给出 "Error: No such image"

c# - 由于缺少 Azure 存储连接字符串和 SAS 连接 uri,Blob 存储的 secret 初始化失败

C# 获取和设置列表集合的属性

c# - 如何使用c#客户端获取azure token ?

java - 我无法从 java 发送电子邮件

PHP:邮件()与发送邮件