【问题标题】:ASP.NET Core Web API hosted on Azure don't send an emailAzure 上托管的 ASP.NET Core Web API 不发送电子邮件
【发布时间】:2021-07-20 20:45:35
【问题描述】:

我有一个基于 ASP.NET Core 并作为应用服务托管在 Azure 上的 Web API。我有一个发送电子邮件的简单功能。但它只适用于本地主机。当我在 Azure 上发布它时,POST 查询返回错误:500。

这是一个 MailKit.Net.Smtp 库。

    {
        [HttpPost]
        [AllowAnonymous]
        public async Task<ActionResult> SendMail([FromBody] Receiver receiver)
        {
            var message = new MimeMessage();
            message.From.Add(new MailboxAddress("Step OSBB", "receiver@gmail.com"));

            message.To.Add(new MailboxAddress("User name", receiver.Email));

            message.Subject = "Henlo, it's a testing email";

            message.Body = new TextPart("plain")
            {
                Text = "I am using MailKit to send this message"
            };

            using (var client = new SmtpClient())
            {
                client.Connect("smtp.gmail.com", 587, false);
                client.Authenticate("myemail@gmail.com", "mypass");
                client.Send(message);
                client.Disconnect(true);
            }
            return StatusCode(200);
        }
    }

    public class Receiver
    {
        public string Email { get; set; }
    }```

【问题讨论】:

  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…
  • 我的回答对你有用吗?

标签: c# asp.net azure asp.net-core smtp


【解决方案1】:

访问下面的链接,并像我一样设置Allow less secure app:Enabled。它对我有用。

https://www.google.com/settings/security/lesssecureapps

相关帖子

how to fix “send-mail: Authorization failed 534 5.7.14 ”

测试步骤:

  1. 未设置 lesssecureapps。

  2. 启用后。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2023-04-04
    • 2020-08-16
    相关资源
    最近更新 更多