MailMessage Construtores

Definição

Inicializa uma nova instância da classe MailMessage.

Sobrecargas

MailMessage()

Inicializa uma instância vazia da classe MailMessage.

MailMessage(MailAddress, MailAddress)

Inicializa uma nova instância da classe MailMessage usando os objetos de classe MailAddress especificados.

MailMessage(String, String)

Inicializa uma nova instância da classe MailMessage usando os objetos de classe String especificados.

MailMessage(String, String, String, String)

Inicializa uma nova instância da classe MailMessage.

MailMessage()

Origem:
MailMessage.cs
Origem:
MailMessage.cs
Origem:
MailMessage.cs

Inicializa uma instância vazia da classe MailMessage.

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

Comentários

De é definido como o valor no elemento de rede para oelemento smtp> mailSettings<(Configurações de Rede), se ele existir.

Aplica-se a

MailMessage(MailAddress, MailAddress)

Origem:
MailMessage.cs
Origem:
MailMessage.cs
Origem:
MailMessage.cs

Inicializa uma nova instância da classe MailMessage usando os objetos de classe MailAddress especificados.

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)

Parâmetros

from
MailAddress

Um MailAddress que contém o endereço do remetente da mensagem de email.

to
MailAddress

Um MailAddress que contém o endereço do destinatário da mensagem de email.

Exceções

from é null.

- ou -

to é null.

O from ou to está malformado.

Exemplos

O exemplo de código a seguir demonstra a chamada desse construtor.

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

Comentários

A From propriedade é inicializada usando from e a To propriedade é inicializada usando to.

Aplica-se a

MailMessage(String, String)

Origem:
MailMessage.cs
Origem:
MailMessage.cs
Origem:
MailMessage.cs

Inicializa uma nova instância da classe MailMessage usando os objetos de classe String especificados.

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)

Parâmetros

from
String

Um String que contém o endereço do remetente da mensagem de email.

to
String

Um String que contém os endereços dos destinatários da mensagem de email. Vários endereços de email precisam ser separados por vírgula (“,”).

Exceções

from é null.

- ou -

to é null.

from é Empty ("").

- ou -

to é Empty ("").

O from ou to está malformado.

Exemplos

O exemplo de código a seguir demonstra a chamada desse construtor.

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

Comentários

A From propriedade é inicializada usando from e a To propriedade é inicializada usando to.

Aplica-se a

MailMessage(String, String, String, String)

Origem:
MailMessage.cs
Origem:
MailMessage.cs
Origem:
MailMessage.cs

Inicializa uma nova instância da classe 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)

Parâmetros

from
String

Um String que contém o endereço do remetente da mensagem de email.

to
String

Um String que contém os endereços dos destinatários da mensagem de email. Vários endereços de email precisam ser separados por vírgula (“,”).

subject
String

Um String que contém o texto do assunto.

body
String

Um String que contém o corpo da mensagem.

Exceções

from é null.

- ou -

to é null.

from é Empty ("").

- ou -

to é Empty ("").

O from ou to está malformado.

Exemplos

O exemplo de código a seguir demonstra a chamada desse construtor.

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

Comentários

As propriedades do novo MailMessage objeto são inicializadas da seguinte maneira:

Parâmetro Propriedade
from From
to To
subject Subject
body Body

Por padrão, presume-se que o assunto e o conteúdo usem a codificação padrão com base nas configurações do computador local. Use as BodyEncoding propriedades e SubjectEncoding para especificar codificações diferentes.

Aplica-se a