通过smtpclient smtp.office365.com发送电子邮件

发布于 2025-02-05 05:52:33 字数 1587 浏览 5 评论 0原文

任何人都可以帮忙。

我正在使用以下代码来发送我的ASP.NET,C#Web表单应用程序的电子邮件,该应用程序生成错误。相同的代码正在用于其他应用程序。我的域名中包含一个连字符( - )。那是原因吗?我需要查看电子邮件配置或服务器/域级设置吗?请帮忙。

string smtpServer = "smtp.office365.com";
int port = 587;
using (MailMessage mail = new MailMessage())
{
    using (SmtpClient SmtpServer = new SmtpClient(smtpServer))
    {
        mail.From = new MailAddress(_emailIDFrom);
        mail.To.Add(_emailIDTo);
        mail.Subject = _subject;
        mail.IsBodyHtml = true;
        mail.Body = _emailBody;
        SmtpServer.Port = port;
        SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
        SmtpServer.UseDefaultCredentials = false;
        SmtpServer.Credentials = new System.Net.NetworkCredential(_emailIDFrom, _emailIDFromPassword);
        SmtpServer.EnableSsl = true;
        try
        {
            SmtpServer.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            return false;
        }
    }
}

我的tls代码在global.asax application_start中如下所示

if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
{
    ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}

- SMTP服务器需要安全的连接或客户端未经认证。服务器响应是:5.7.57客户端未验证发送邮件。错误:535 5.7.139身份验证失败,SMTPClientAuthentication被禁用了租户。访问 https://aka.ms/smtp_auth_disabled 以获取更多信息。 [dxxp273ca0023.arep273.prod.outlook.com]

Anyone can help please.

I am using the following code for sending email from my ASP.Net, C# web forms application which generating the error. The same code is working for other application. My domain name contains a hyphen in it (-). Is that the reason? Do I need to see email configuration or server/domain level settings? Please help.

string smtpServer = "smtp.office365.com";
int port = 587;
using (MailMessage mail = new MailMessage())
{
    using (SmtpClient SmtpServer = new SmtpClient(smtpServer))
    {
        mail.From = new MailAddress(_emailIDFrom);
        mail.To.Add(_emailIDTo);
        mail.Subject = _subject;
        mail.IsBodyHtml = true;
        mail.Body = _emailBody;
        SmtpServer.Port = port;
        SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
        SmtpServer.UseDefaultCredentials = false;
        SmtpServer.Credentials = new System.Net.NetworkCredential(_emailIDFrom, _emailIDFromPassword);
        SmtpServer.EnableSsl = true;
        try
        {
            SmtpServer.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
            return false;
        }
    }
}

My Tls code in Global.asax Application_Start as follows

if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)
{
    ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;
}

ERROR--
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant. Visit https://aka.ms/smtp_auth_disabled for more information. [DXXP273CA0023.AREP273.PROD.OUTLOOK.COM]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文