SmtpClient.Credentials 属性

定义

获取或设置用于验证发件人身份的凭据。Gets or sets the credentials used to authenticate the sender.

public:
 property System::Net::ICredentialsByHost ^ Credentials { System::Net::ICredentialsByHost ^ get(); void set(System::Net::ICredentialsByHost ^ value); };
public System.Net.ICredentialsByHost Credentials { get; set; }
member this.Credentials : System.Net.ICredentialsByHost with get, set
Public Property Credentials As ICredentialsByHost

属性值

表示用于身份验证的凭据的 ICredentialsByHost,如果没有指定凭据,则为 nullAn ICredentialsByHost that represents the credentials to use for authentication; or null if no credentials have been specified.

异常

发送电子邮件时无法更改此属性的值。You cannot change the value of this property when an email is being sent.

示例

下面的代码示例演示如何设置用于发送电子邮件的凭据。The following code example demonstrates setting the credentials used to send an email.

static void CreateTestMessage1( String^ server, int port )
{
   String^ to = L"jane@contoso.com";
   String^ from = L"ben@contoso.com";
   String^ subject = L"Using the new SMTP client.";
   String^ body = L"Using this new feature, you can send an email message from an application very easily.";
   MailMessage^ message = gcnew MailMessage( from,to,subject,body );
   SmtpClient^ client = gcnew SmtpClient( server,port );
   
   // Credentials are necessary if the server requires the client 
   // to authenticate before it will send email on the client's behalf.
   client->Credentials = CredentialCache::DefaultNetworkCredentials;
   
   //Display the properties on the service point.
   ServicePoint^ p = client->ServicePoint;
   Console::WriteLine( L"Connection lease timeout: {0}", p->ConnectionLeaseTimeout );
   client->Send( message );
   client->~SmtpClient();
}


public static void CreateTestMessage1(string server, int port)
{
    string to = "jane@contoso.com";
    string from = "ben@contoso.com";
    string subject = "Using the new SMTP client.";
    string body = @"Using this new feature, you can send an email message from an application very easily.";
    MailMessage message = new MailMessage(from, to, subject, body);
    SmtpClient client = new SmtpClient(server, port);
    // Credentials are necessary if the server requires the client 
    // to authenticate before it will send email on the client's behalf.
    client.Credentials = CredentialCache.DefaultNetworkCredentials;
    client.Send(message);
}

注解

某些 SMTP 服务器要求先对客户端进行身份验证,然后服务器才能代表其发送电子邮件。Some SMTP servers require that the client be authenticated before the server will send email on its behalf. 若要使用默认网络凭据,可以将 UseDefaultCredentials 设置为 true,而不是设置此属性。To use your default network credentials, you can set the UseDefaultCredentials to true instead of setting this property. 如果 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.

还可以使用应用程序和计算机配置文件指定凭据信息。Credentials information can also be specified using the application and machine configuration files. 有关详细信息,请参阅<mailSettings> 元素(网络设置)For more information, see <mailSettings> Element (Network Settings). 如果使用 Credentials 属性指定信息,则此信息将重写配置文件设置。If information is specified using the Credentials property, this information overrides the configuration file settings.

注意

如果提供了用于基本身份验证的凭据,这些凭据将以明文形式发送到服务器。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.

适用于