简体   繁体   English

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

[英]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.我有一个基于 ASP.NET 核心的 Web API 并作为服务托管在 Z3A580F142203677F1F0ZBC308 上。 I have a simple functionality for sending an email.我有一个发送 email 的简单功能。 But it's only working on the localhost.但它只适用于本地主机。 When I publish it on the Azure, the POST query returns me error: 500.当我在 Azure 上发布它时,POST 查询返回错误:500。

It's a MailKit.Net.Smtp library.这是一个 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; }
    }```

Visit below link, and set Allow less secure app:Enabled like me.访问下面的链接,并设置Allow less secure app:Enabled It works for me.这个对我有用。

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

在此处输入图像描述

Related Post相关帖子

how to fix “send-mail: Authorization failed 534 5.7.14 ” 如何修复“发送邮件:授权失败 534 5.7.14”

Test steps:测试步骤:

  1. Not set lesssecureapps.不设置lesssecureapps。

    在此处输入图像描述

    在此处输入图像描述

  2. After enabled.启用后。

    在此处输入图像描述

    在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将客户端 IP 从 ASP.NET Web API 转发到 Azure 中托管的 .NET Core API - Forward client IP from ASP.NET Web API to .NET Core API hosted in Azure 无法连接托管在 Azure Web 应用程序上的安全 Asp.Net Core Web 套接字(使用 TLS) - Can't connect secured Asp.Net Core Web Socket hosted on Azure Web App (using TLS) Azure上的Asp.Net Core Web API - Asp.Net Core Web API on Azure 在 ASP.NET 核心 Web Z72664DC0959F3B0C0470479 中注册后,将 email 确认令牌作为可点击链接发送给用户 - Send email confirmation token as clickable link to user upon registration in ASP.NET Core Web Api 使用 ASP.NET 内核 6.0 Web ZDB974238714CA8DE634A7CE1D08 发送 email - Sending email using ASP.NET Core 6.0 Web API 确认 Email 在 asp.net 内核 web api - Confirm Email in asp.net core web api 使用 ASP.net 内核 web api 发送多个文件 - Send multiple files using ASP.net core web api asp.net 核心 3 Web Api ! 将 json 发送到 POST 操作 - asp.net core 3 Web Api ! send json to POST Action 发送清单<t>作为post body中的可选参数 ASP.NET Core Web Api</t> - Send List<T> as an optional parameter in the post body in ASP.NET Core Web Api 有没有办法在 Azure 上使用托管 ASP.NET 核心 web 应用程序的本地数据库? - Is there a way to use a local db with a hosted ASP.NET core web app on Azure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM