与SmtpClient.UseDefaultCredentials物业困惑 [英] Confused with the SmtpClient.UseDefaultCredentials Property

查看:3453
本文介绍了与SmtpClient.UseDefaultCredentials物业困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我MVC4的应用程序,我使用的是 SmtpClient 通过发送出去的电子邮件Gmail的 smtp.gmail.com SMTP服务器。

我已经配置我的web.config文件中使用以下设置:

 < system.net>
 < mailSettings>
  < SMTP deliveryMethod =网络>
   <网络enableSsl =真
   的DefaultCredentials =假
   主机=smtp.gmail.com
   端口=587
   的userName =xxMyUserNamexx@gmail.com
   密码=xxMyPasswordxx/>
  < / SMTP>
 < / mailSettings>
< /system.net>

这是使用SmtpClient并发送电子邮件的方法如下:

 公共无效的SendMail(字符串fromDisplayName,串fromEmailAddress,串toEmailAddress,弦乐主题,绳体)
{
    MailAddress从=新的MailAddress(fromEmailAddress,fromDisplayName);
    MailAddress到=新MailAddress(toEmailAddress);
    MAILMESSAGE MAILMESSAGE新= MAILMESSAGE(从,到);
    mailMessage.Body =身体;
    mailMessage.Subject =主体;    SmtpClient客户端=新SmtpClient();
    //client.UseDefaultCredentials = FALSE;
    client.Send(MAILMESSAGE);
}

在code以上的作品如预期是好的。什么混淆我是注释行 client.UseDefaultCredentials = FALSE; - 如果我取消注释行,我会收到一条异常消息:


  

SMTP服务器要求安全连接或客户端未
  验证。服务器响应为:5.5.1需要验证


更重要的是,如果我在 UseDefaultCredentials 属性设置为真的不要紧,我仍然会收到异常消息。对我来说,避免了异常信息的唯一方法是完全消除行。

这是正常的行为?你能解释为什么我收到异常消息?


解决方案

  

那么,为什么我明确地属性设置为false,抛出一个异常?


这样做的原因是因为二传手 UseDefaultCredentials 证书属性,如果你将其设置为空假的,或者,如果设置为true,将它设置为 CredentialCache.DefaultNetworkCredentials 属性。在 DefaultNetworkCredentials 属性是<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultnetworkcredentials.aspx\">defined通过MSDN为:


  

由DefaultNetworkCredentials返回的凭据重新presents在其中运行应用程序的当前安全上下文中的身份验证凭据。对于客户端应用程序,这些通常是运行应用程序的用户的Windows凭证(用户名,密码和域)。对于ASP.NET应用程序,默认网络凭据是登录用户的用户凭证,或者用户正在模拟。


当您设置 UseDefaultCredentials 为真,它使用您的IIS用户,我假设你的IIS用户不具有相同的身份验证凭据您的帐户,无论您正在使用SMTP服务器。设置 UseDefaultCredentials 为假空的出时设置的凭据。所以,无论哪种方式,你得到这个错误。

下面是一个使用 dotPeek 看看二传手为 UseDefaultCredentials

 
{
    如果(this.InCall)
    {
        抛出新的InvalidOperationException异常(
            SR.GetString(SmtpInvalidOperationDuringSend));
    }
    this.transport.Credentials =价值
        ? (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials
        (ICredentialsByHost)空;
  }

In my MVC4 application, I am using the SmtpClient to send out email via Gmail's smtp.gmail.com SMTP server.

I have configured my Web.Config file with the following settings:

<system.net>
 <mailSettings>
  <smtp deliveryMethod="Network">
   <network enableSsl="true" 
   defaultCredentials="false" 
   host="smtp.gmail.com" 
   port="587" 
   userName="xxMyUserNamexx@gmail.com" 
   password="xxMyPasswordxx" />
  </smtp>
 </mailSettings>
</system.net>

The method that uses the SmtpClient and sends an email message looks like:

public void SendMail(string fromDisplayName, string fromEmailAddress, string toEmailAddress, string subject, string body)
{
    MailAddress from = new MailAddress(fromEmailAddress, fromDisplayName);
    MailAddress to = new MailAddress(toEmailAddress);
    MailMessage mailMessage = new MailMessage(from, to);
    mailMessage.Body = body;
    mailMessage.Subject = subject;

    SmtpClient client = new SmtpClient();
    //client.UseDefaultCredentials = false;
    client.Send(mailMessage);
}

The code above works as expected and is fine. What confuses me is the commented line client.UseDefaultCredentials = false; - If I were to uncomment that line, I will receive an exception message that states:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

What's more is, it doesn't matter if I set the UseDefaultCredentials property to true or false, I will still receive the exception message. The only way for me to avoid the exception message is to remove the line altogether.

Is this behavior normal? Can you explain why I am receiving the exception message?

解决方案

So why would me explicitly setting the property to false throw an exception?

The reason for this is because the setter for UseDefaultCredentials sets the Credentials property to null if you set it to false, or it sets it to the CredentialCache.DefaultNetworkCredentials property if set to true. The DefaultNetworkCredentials property is defined by MSDN as:

The credentials returned by DefaultNetworkCredentials represents the authentication credentials for the current security context in which the application is running. For a client-side application, these are usually the Windows credentials (user name, password, and domain) of the user running the application. For ASP.NET applications, the default network credentials are the user credentials of the logged-in user, or the user being impersonated.

When you set UseDefaultCredentials to true, it's using your IIS user, and I'm assuming that your IIS user does not have the same authentication credentials as your account for whatever SMTP server you're using. Setting UseDefaultCredentials to false null's out the credentials that are set. So either way you're getting that error.

Here's a look at the setter for UseDefaultCredentials using dotPeek:

set
{
    if (this.InCall)
    {
        throw new InvalidOperationException(
            SR.GetString("SmtpInvalidOperationDuringSend"));
    }
    this.transport.Credentials = value 
        ? (ICredentialsByHost) CredentialCache.DefaultNetworkCredentials 
        : (ICredentialsByHost) null;
  }

这篇关于与SmtpClient.UseDefaultCredentials物业困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆