SmtpClient Konstruktory

Definice

Inicializuje novou instanci SmtpClient třídy.

Přetížení

SmtpClient()

Inicializuje novou instanci třídy pomocí nastavení konfiguračního SmtpClient souboru.

SmtpClient(String)

Inicializuje novou instanci SmtpClient třídy, která odesílá e-mail pomocí zadaného serveru SMTP.

SmtpClient(String, Int32)

Inicializuje novou instanci SmtpClient třídy, která odesílá e-mail pomocí zadaného serveru SMTP a portu.

SmtpClient()

Zdroj:
SmtpClient.cs
Zdroj:
SmtpClient.cs
Zdroj:
SmtpClient.cs

Inicializuje novou instanci třídy pomocí nastavení konfiguračního SmtpClient souboru.

public:
 SmtpClient();
public SmtpClient ();
Public Sub New ()

Příklady

Následující příklad kódu ukazuje odeslání e-mailové zprávy.

static void CreateTestMessage3()
{
   MailAddress^ to = gcnew MailAddress( L"jane@contoso.com" );
   MailAddress^ from = gcnew MailAddress( 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.";
   
   // Use the application or machine configuration to get the 
   // host, port, and credentials.
   SmtpClient^ client = gcnew SmtpClient;
   Console::WriteLine( L"Sending an email message to {0} at {1} by using the SMTP host {2}.", to->User, to->Host, client->Host );
   client->Send( message );
}
public static void CreateTestMessage3()
{
    MailAddress to = new MailAddress("jane@contoso.com");
    MailAddress from = new MailAddress("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.";
    // Use the application or machine configuration to get the
    // host, port, and credentials.
    SmtpClient client = new SmtpClient();
    Console.WriteLine("Sending an email message to {0} at {1} by using the SMTP host={2}.",
        to.User, to.Host, client.Host);
    client.Send(message);
}

Příklad <uzlu mailSettings> v konfiguračním souboru aplikace nebo počítače najdete v tématu <mailSettings> – element (nastavení sítě).

Poznámky

Tento konstruktor inicializuje Hostvlastnosti , Credentialsa Port pro nový SmtpClient pomocí nastavení v konfiguračních souborech aplikace nebo počítače. Další informace najdete v tématu <element mailSettings> (nastavení sítě).

Platí pro

SmtpClient(String)

Zdroj:
SmtpClient.cs
Zdroj:
SmtpClient.cs
Zdroj:
SmtpClient.cs

Inicializuje novou instanci SmtpClient třídy, která odesílá e-mail pomocí zadaného serveru SMTP.

public:
 SmtpClient(System::String ^ host);
public SmtpClient (string? host);
public SmtpClient (string host);
new System.Net.Mail.SmtpClient : string -> System.Net.Mail.SmtpClient
Public Sub New (host As String)

Parametry

host
String

A String , který obsahuje název nebo IP adresu hostitelského počítače používaného pro transakce SMTP.

Příklady

Následující příklad kódu ukazuje volání tohoto konstruktoru.

static void CreateTimeoutTestMessage( String^ server )
{
   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 );
   Console::WriteLine( L"Changing time out from {0} to 100.", client->Timeout );
   client->Timeout = 100;
   
   // 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 );
}
public static void CreateTimeoutTestMessage(string server)
{
    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);
    Console.WriteLine("Changing time out from {0} to 100.", client.Timeout);
    client.Timeout = 100;
    // 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);
}

Poznámky

Parametr host se používá k inicializaci hodnoty Host vlastnosti. Vlastnosti Credentials a Port se inicializují pomocí nastavení v konfiguračních souborech aplikace nebo počítače. Pokud host je null nebo rovno String.Empty, Host je inicializován pomocí nastavení v konfiguračních souborech aplikace nebo počítače.

Další informace o používání konfiguračních souborů aplikace a počítače najdete v tématu <mailSettings> – element (nastavení sítě). Pokud jsou informace zadány pomocí SmtpClient konstruktorů nebo vlastností, přepíše tyto informace nastavení konfiguračního souboru.

Platí pro

SmtpClient(String, Int32)

Zdroj:
SmtpClient.cs
Zdroj:
SmtpClient.cs
Zdroj:
SmtpClient.cs

Inicializuje novou instanci SmtpClient třídy, která odesílá e-mail pomocí zadaného serveru SMTP a portu.

public:
 SmtpClient(System::String ^ host, int port);
public SmtpClient (string? host, int port);
public SmtpClient (string host, int port);
new System.Net.Mail.SmtpClient : string * int -> System.Net.Mail.SmtpClient
Public Sub New (host As String, port As Integer)

Parametry

host
String

A String , který obsahuje název nebo IP adresu hostitele používaného pro transakce SMTP.

port
Int32

Hodnota Int32 větší než nula, která obsahuje port, který se má použít na host.

Výjimky

port nesmí být menší než nula.

Příklady

Následující příklad kódu ukazuje volání tohoto konstruktoru.

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;
   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);
}

Poznámky

Parametry host a port nastaví hodnotu Host vlastností a Port v uvedeném pořadí. Pokud host je null nebo rovno String.Empty, Host je inicializován pomocí nastavení v konfiguračních souborech aplikace nebo počítače. Pokud port je nula, Port inicializuje se pomocí nastavení v konfiguračních souborech aplikace nebo počítače. Vlastnost Credentials se inicializuje pomocí nastavení v konfiguračních souborech aplikace nebo počítače.

Další informace o používání konfiguračních souborů aplikace a počítače najdete v tématu <mailSettings> – element (nastavení sítě). Pokud jsou informace zadány pomocí SmtpClient konstruktorů nebo vlastností, přepíše tyto informace nastavení konfiguračního souboru.

Platí pro