简体   繁体   中英

ASP.NET Core Web API hosted on Azure don't send an email

I have an Web API based on ASP.NET Core and hosted on Azure as App Service. I have a simple functionality for sending an email. But it's only working on the localhost. When I publish it on the Azure, the POST query returns me error: 500.

It's a MailKit.Net.Smtp library.

    {
        [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; }
    }```

Visit below link, and set Allow less secure app:Enabled like me. It works for me.

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

在此处输入图像描述

Related Post

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

Test steps:

  1. Not set lesssecureapps.

    在此处输入图像描述

    在此处输入图像描述

  2. After enabled.

    在此处输入图像描述

    在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM