Udostępnij za pośrednictwem


MailMessage Konstruktory

Definicja

Inicjuje nowe wystąpienie klasy MailMessage.

Przeciążenia

Nazwa Opis
MailMessage()

Inicjuje puste wystąpienie MailMessage klasy.

MailMessage(MailAddress, MailAddress)

Inicjuje nowe wystąpienie MailMessage klasy przy użyciu określonych MailAddress obiektów klasy.

MailMessage(String, String)

Inicjuje nowe wystąpienie MailMessage klasy przy użyciu określonych String obiektów klasy.

MailMessage(String, String, String, String)

Inicjuje nowe wystąpienie klasy MailMessage.

MailMessage()

Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs

Inicjuje puste wystąpienie MailMessage klasy.

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

Uwagi

Właściwość From jest ustawiona na wartość w elemecie sieciowym dla elementusmtp> mailSettings<(Ustawienia sieciowe), jeśli istnieje.

Dotyczy

MailMessage(MailAddress, MailAddress)

Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs

Inicjuje nowe wystąpienie MailMessage klasy przy użyciu określonych MailAddress obiektów klasy.

public:
 MailMessage(System::Net::Mail::MailAddress ^ from, System::Net::Mail::MailAddress ^ to);
public MailMessage(System.Net.Mail.MailAddress from, System.Net.Mail.MailAddress to);
new System.Net.Mail.MailMessage : System.Net.Mail.MailAddress * System.Net.Mail.MailAddress -> System.Net.Mail.MailMessage
Public Sub New (from As MailAddress, to As MailAddress)

Parametry

from
MailAddress

Element MailAddress zawierający adres nadawcy wiadomości e-mail.

to
MailAddress

Element MailAddress zawierający adres adresata wiadomości e-mail.

Wyjątki

Parametr from ma wartość null.

— lub —

Parametr to ma wartość null.

from lub to jest źle sformułowany.

Przykłady

W poniższym przykładzie kodu pokazano wywołanie tego konstruktora.

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);
}
Public Shared Sub CreateTestMessage3()
    Dim [to] As MailAddress = New MailAddress("jane@contoso.com")
    Dim from As MailAddress = New MailAddress("ben@contoso.com")
    Dim message As MailMessage = 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.
    Dim client As SmtpClient = 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)
End Sub

Uwagi

Właściwość From jest inicjowana przy użyciu metody from , a To właściwość jest inicjowana przy użyciu polecenia to.

Dotyczy

MailMessage(String, String)

Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs

Inicjuje nowe wystąpienie MailMessage klasy przy użyciu określonych String obiektów klasy.

public:
 MailMessage(System::String ^ from, System::String ^ to);
public MailMessage(string from, string to);
new System.Net.Mail.MailMessage : string * string -> System.Net.Mail.MailMessage
Public Sub New (from As String, to As String)

Parametry

from
String

Element String zawierający adres nadawcy wiadomości e-mail.

to
String

Element String zawierający adresy adresatów wiadomości e-mail. Wiele adresów e-mail musi być rozdzielonych przecinkami (",").

Wyjątki

Parametr from ma wartość null.

— lub —

Parametr to ma wartość null.

from is Empty ("").

— lub —

to is Empty ("").

from lub to jest źle sformułowany.

Przykłady

W poniższym przykładzie kodu pokazano wywołanie tego konstruktora.

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());
    }
}
Public Shared Sub CreateTestMessage2(ByVal server As String)
    Dim [to] As String = "jane@contoso.com"
    Dim from As String = "ben@contoso.com"
    Dim message As MailMessage = 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."
    Dim client As SmtpClient = 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 ex As Exception
        Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString())
    End Try
End Sub

Uwagi

Właściwość From jest inicjowana przy użyciu metody from , a To właściwość jest inicjowana przy użyciu polecenia to.

Dotyczy

MailMessage(String, String, String, String)

Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs
Źródło:
MailMessage.cs

Inicjuje nowe wystąpienie klasy MailMessage.

public:
 MailMessage(System::String ^ from, System::String ^ to, System::String ^ subject, System::String ^ body);
public MailMessage(string from, string to, string? subject, string? body);
public MailMessage(string from, string to, string subject, string body);
new System.Net.Mail.MailMessage : string * string * string * string -> System.Net.Mail.MailMessage
Public Sub New (from As String, to As String, subject As String, body As String)

Parametry

from
String

Element String zawierający adres nadawcy wiadomości e-mail.

to
String

Element String zawierający adresy adresatów wiadomości e-mail. Wiele adresów e-mail musi być rozdzielonych przecinkami (",").

subject
String

Element String zawierający tekst tematu.

body
String

Element String zawierający treść komunikatu.

Wyjątki

Parametr from ma wartość null.

— lub —

Parametr to ma wartość null.

from is Empty ("").

— lub —

to is Empty ("").

from lub to jest źle sformułowany.

Przykłady

W poniższym przykładzie kodu pokazano wywołanie tego konstruktora.

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);
}
Public Shared Sub CreateTimeoutTestMessage(ByVal server As String)
    Dim [to] As String = "jane@contoso.com"
    Dim from As String = "ben@contoso.com"
    Dim subject As String = "Using the new SMTP client."
    Dim body As String = "Using this new feature, you can send an email message from an application very easily."
    Dim message As MailMessage = New MailMessage(from, [to], subject, body)
    Dim client As SmtpClient = 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)
End Sub

Uwagi

Właściwości nowego MailMessage obiektu są inicjowane w następujący sposób:

Parameter Majątek
from From
to To
subject Subject
body Body

Domyślnie temat i zawartość są zakładane, aby używać domyślnego kodowania na podstawie ustawień komputera lokalnego. BodyEncoding Użyj właściwości iSubjectEncoding, aby określić różne kodowania.

Dotyczy