简体   繁体   English

尝试在 asp.net 核心应用程序中使用 SmtpClient 发送 email 时出错

[英]Getting an error when trying send an email using SmtpClient in asp.net core application

I am trying to send an email from asp.net core application but I keep getting an error that reads我正在尝试从 asp.net 核心应用程序发送 email 但我不断收到错误消息

SmtpException: The SMTP server requires a secure connection or the client was not authenticated. SmtpException:SMTP 服务器需要安全连接或客户端未通过身份验证。 The server response was: 5.7.57 Client not authenticated to send mail.服务器响应为:5.7.57 客户端未通过身份验证发送邮件。 [JN2P275CA0038.ZAFP275.PROD.OUTLOOK.COM] [JN2P275CA0038.ZAFP275.PROD.OUTLOOK.COM]

I have also tried checking firewall rules and increasing timeout and that has not helped so I am not sure what is wrong我也尝试过检查防火墙规则并增加超时,但没有帮助,所以我不确定出了什么问题

This is my code to send email这是我发送 email 的代码

   SmtpClient client = new SmtpClient("smtp.office365.com")
                    {
                        //Port = 587, //outlook port 587
                        Port = 587, 
                        EnableSsl = true,
                        //UseDefaultCredentials
                        UseDefaultCredentials = false,
                        TargetName = "STARTTLS/smtp.office365.com",
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Credentials = new NetworkCredential("myemail@myemails.com","password"),
                    };

                    MailMessage message = new MailMessage()
                    {
                        From = new MailAddress("myemail@myemails.com"),
                        Subject = "New Requisition",
                        Body = "New requisition has been created:" + " " + requisition.RequisitionNo,
                        IsBodyHtml = true,
                    };

                    message.To.Add(SubmissionNotfication.To);
                    //message.CC.Add(SubmissionNotfication.Cc);
                    //var attachment = new System.Net.Mail.Attachment(filePath); //Attachment(string fileName, string mediaType) 
                    //message.Attachments.Add(attachment);
                    client.Send(message);
                }
        

Have a look at How to set up an application to send email using Microsoft 365 or Office 365查看如何设置应用程序以使用 Microsoft 365 或 Office 365 发送 email

You might have to use your MX endpoint as the sending domain and try port 25您可能必须使用 MX 端点作为发送域并尝试使用端口 25

SmtpClient client = new SmtpClient("yourorganisation.mail.protection.outlook.com")
{
    Port = 25, 
    EnableSsl = true,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential("myemail@myemails.com","password"),
};

It could be few reasons you encounter above error message您遇到上述错误消息的原因可能有几个

  1. Incorrect email usernmae and password email 用户名和密码不正确
  2. SSL settings. SSL 设置。 Make sure you enable SSL security while sending emails through your application确保在通过应用程序发送电子邮件时启用 SSL 安全性
  3. Your port issue.你的端口问题。 Make sure that your ISP doesn't block port 25 or 587. You can check it by using telnet.确保您的 ISP 没有阻止端口 25 或 587。您可以使用 telnet 进行检查。

You may need to check tutorial about send email using outlook.您可能需要查看有关使用 outlook 发送 email 的教程。 If you are using Gmail, then you can refer to this post https://dotnetblog.asphostportal.com/using-gmail-to-send-email-in-asp-net-core/如果您使用的是 Gmail,那么您可以参考这篇文章https://dotnetblog.asphostportal.com/using-gmail-to-send-email-in-asp-net-core/

暂无
暂无

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

相关问题 无法在asp.net中使用SmtpClient发送电子邮件 - Unable to send Email Using SmtpClient in asp.net 如何从 SmtpClient() asp.net 核心 MVC 中的文件夹中发送带有图像(作为 email 的主体)的 email - how to send email with image (as body of the email ) attached form the folder in SmtpClient() asp.net core MVC 使用SmtpClient时发送电子邮件 - Send email when using SmtpClient 我正在尝试在 asp.net c# 中使用 google API 发送 email 并收到错误 400 redirect_uri_mismatch - I am trying to send email using google API in asp.net c# and getting Error 400 redirect_uri_mismatch 如何使用smtpClient与ASP.Net安全地发送我的凭证 - How to send my Credentials securely using smtpClient with ASP.Net 尝试在asp.net中发送电子邮件时出错 - Error while trying to send an email in asp.net 如何在 Asp.Net Core 中使用 MailKit 发送 email - how to send email using MailKit in Asp.Net Core 尝试使用 Google.Apis.Gmail 从 asp.net core web api 应用程序从 google 工作区发送电子邮件时出错 - Error while trying to send email from google workspace from asp.net core web api app using Google.Apis.Gmail 尝试使用outllook从asp .net windows应用程序发送带有图像的电子邮件时出现“无法创建文件”错误 - 'Cannot create file' error while trying to send email with image from asp .net windows application using outllook 尝试调用删除模态确认操作时出现 405 错误(Asp.Net Core 6 javascript) - Getting a 405 error when trying to call Delete Modal Confirmation action (Asp.Net Core 6 javascript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM