SmtpClient.Host Property

Definition

Gets or sets the name or IP address of the host used for SMTP transactions.

C#
public string? Host { get; set; }
C#
public string Host { get; set; }

Property Value

A String that contains the name or IP address of the computer to use for SMTP transactions.

Exceptions

The value specified for a set operation is null.

The value specified for a set operation is equal to Empty ("").

You cannot change the value of this property when an email is being sent.

Examples

The following code example demonstrates sending an email message by using the host and port specified in an application configuration file.

C#
public static void CreateTestMessage4(string server)
{
    MailAddress from = new MailAddress("ben@contoso.com");
    MailAddress to = new MailAddress("Jane@contoso.com");
    MailMessage message = new MailMessage(from, to);
    message.Subject = "Using the SmtpClient class.";
    message.Body = @"Using this feature, you can send an email message from an application very easily.";
    SmtpClient client = new SmtpClient(server);
    Console.WriteLine("Sending an email message to {0} by using SMTP host {1} port {2}.",
         to.ToString(), client.Host, client.Port);

    try
    {
        client.Send(message);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in CreateTestMessage4(): {0}",
            ex.ToString());
    }
}

Remarks

The value of the Host property can also be set using constructors or the application or machine configuration file. For more information, see <mailSettings> Element (Network Settings).

If information is specified using this property, this information overrides the configuration file settings.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

See also