客户端未通过身份验证MAIL期间发送匿名邮件FROM

客户端未通过身份验证MAIL期间发送匿名邮件FROM

本文介绍了使用异常的Office365默认的SMTP凭据 - 客户端未通过身份验证MAIL期间发送匿名邮件FROM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NLOG发送日志作为一个自定义邮件目标电子邮件。我送从我的office365帐户设置为默认在我的web.config(我的主要项目)如下:

I'm using NLog to send logs as email with a custom mail target. I am sending from my office365 account set up as the default in my web.config (of my main project) as follows:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network defaultCredentials="false" host="smtp.office365.com" port="587" userName="[email protected]" password="mypassword" enableSsl="true" />
      </smtp>
    </mailSettings>
  </system.net>

我重写我的日志目标Write方法(在我NLOG实现包)如下:

I override the Write method with my log target (in my NLog implementation package) as follows:

    protected override void Write(LogEventInfo logEvent)
    {
        try
        {
            using (var mail = new MailMessage())
            {
                this.SetupMailMessage(mail, logEvent, this.Layout.Render(logEvent));

                using (SmtpClient smtpClient = new SmtpClient())
                {
                    smtpClient.UseDefaultCredentials = true;
                    smtpClient.Send(mail);
                }
            }
        }
        catch (Exception exception)
        {
            throw new NLogRuntimeException("An error occurred when sending a log mail message.", exception);
        }
    }

当系统会尝试从该帐户发送邮件,以下 System.Net.Mail.SmtpException 被抛出:

When the system tries to send a mail from this account, the following System.Net.Mail.SmtpException is thrown:

SMTP服务器要求安全连接或客户端未通过身份验证。服务器响应为:57年7月5日SMTP;客户端未通过身份验证MAIL期间发送匿名邮件FROM

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM

我有四重检查的凭据,他们是正确的。有谁知道还有什么可以引起这个异常?

I have quadruple checked the credentials and they are correct. Does anyone know what else could be causing this exception?

更新:原来,CredentialCache.DefaultCredentials属性为全空的字符串。然而,当我解压设置,使用下面的code手动我可以从web.config中的设置。

UPDATE: It turns out the CredentialCache.DefaultCredentials property is full of empty strings. Yet, when I extract the settings manually using the below code I can get the settings from the web.config.

SmtpSection settings = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
smtpClient.Credentials = new NetworkCredential(settings.Network.UserName, settings.Network.Password);
smtpClient.Host = settings.Network.Host;
smtpClient.Port = settings.Network.Port;
smtpClient.EnableSsl = settings.Network.EnableSsl;

var creds = CredentialCache.DefaultCredentials;  // Is empty

我可以用这作为一种变通方法。但是,怎么办?为什么会默认凭据是空的?

I can use this as a workaround. But what gives? Why would the default credentials be empty?

推荐答案

虽然我在回答更​​新中提到的解决方法做的工作,我对此很不开心手动获取这些值。对我来说,解决办法是为删除行

Although the workaround I mentioned in the answer update did work, I was not happy about manually fetching those values. The solution for me was to remove the line

smtpClient.UseDefaultCredentials = true;

从原来的code我张贴。事实证明, smtpClient 与默认凭据初始化我在web.config成立,而上述取出线与从 CredentialCache.DefaultCredentials 。我仍然不知道为什么 CredentialCache.DefaultCredentials 为空或当这应该是从web.config填充,但是这是我的问题的根源。

from the original code I posted. It turns out that smtpClient is initialized with the default credentials I set up in the web.config, and the above removed line was overwriting them with empty strings from CredentialCache.DefaultCredentials. I still don't know why CredentialCache.DefaultCredentials is empty or when this is supposed to be populated from the web.config, but this was the source of my problem.

如果任何人有任何进一步的深入了解此请张贴一个更好的答案!

If anyone has any further insight into this please post a better answer!

这篇关于使用异常的Office365默认的SMTP凭据 - 客户端未通过身份验证MAIL期间发送匿名邮件FROM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:37