SmtpClient.UseDefaultCredentials 属性
定义
获取或设置 Boolean 值,该值控制 DefaultCredentials 是否随请求一起发送。Gets or sets a Boolean value that controls whether the DefaultCredentials are sent with requests.
public:
property bool UseDefaultCredentials { bool get(); void set(bool value); };
public bool UseDefaultCredentials { get; set; }
[set: System.MonoNotSupported("no DefaultCredential support in Mono")]
public bool UseDefaultCredentials { get; set; }
member this.UseDefaultCredentials : bool with get, set
[<set: System.MonoNotSupported("no DefaultCredential support in Mono")>]
member this.UseDefaultCredentials : bool with get, set
Public Property UseDefaultCredentials As Boolean
属性值
如果使用默认凭据,则为 true;否则为 false。true if the default credentials are used; otherwise false. 默认值是 false。The default value is false.
- 属性
-
MonoNotSupportedAttribute
例外
发送电子邮件时无法更改此属性的值。You cannot change the value of this property when an email is being sent.
示例
下面的代码示例演示如何使用此属性。The following code example demonstrates using this property.
static void CreateTestMessage2( String^ server )
{
String^ to = L"jane@contoso.com";
String^ from = L"ben@contoso.com";
MailMessage^ message = gcnew MailMessage( from,to );
message->Subject = L"Using the new SMTP client.";
message->Body = L"Using this new feature, you can send an email message from an application very easily.";
SmtpClient^ client = gcnew SmtpClient( server );
// Credentials are necessary if the server requires the client
// to authenticate before it will send email on the client's behalf.
client->UseDefaultCredentials = true;
client->Send( message );
client->~SmtpClient();
}
public static void CreateTestMessage2(string server)
{
string to = "jane@contoso.com";
string from = "ben@contoso.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an email message from an application very easily.";
SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client
// to authenticate before it will send email on the client's behalf.
client.UseDefaultCredentials = true;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateTestMessage2(): {0}",
ex.ToString());
}
}
注解
某些 SMTP 服务器要求先对客户端进行身份验证,然后服务器才能代表其发送电子邮件。Some SMTP servers require that the client be authenticated before the server sends email on its behalf. true SmtpClient 如果服务器请求,则将此属性设置为 "如果服务器请求,则使用当前登录用户的默认凭据进行身份验证"。Set this property to true when this SmtpClient object should, if requested by the server, authenticate using the default credentials of the currently logged on user. 对于客户端应用程序,这在大多数情况下都是所需的行为。For client applications, this is the desired behavior in most scenarios.
还可以使用应用程序和计算机配置文件指定凭据信息。Credentials information can also be specified using the application and machine configuration files. 有关详细信息,请参阅 < mailSettings > 元素 (网络设置) 。For more information, see <mailSettings> Element (Network Settings).
如果将 UseDefaultCredentials 属性设置为, false, 则在 Credentials 连接到服务器时,属性中设置的值将用于凭据。If the UseDefaultCredentials property is set to false, then the value set in the Credentials property will be used for the credentials when connecting to the server. 如果将 UseDefaultCredentials 属性设置为 false 并且 Credentials 尚未设置该属性,则会以匿名方式将邮件发送到服务器。If the UseDefaultCredentials property is set to false and the Credentials property has not been set, then mail is sent to the server anonymously.
注意
如果提供了用于基本身份验证的凭据,这些凭据将以明文形式发送到服务器。If you provide credentials for basic authentication, they are sent to the server in clear text. 这可能会导致安全问题,因为你的凭据可能会被他人使用。This can present a security issue because your credentials can be seen, and then used by others.