MailMessage.To 屬性

定義

取得地址集合,其中包含這個電子郵件訊息的收件者。

public:
 property System::Net::Mail::MailAddressCollection ^ To { System::Net::Mail::MailAddressCollection ^ get(); };
public System.Net.Mail.MailAddressCollection To { get; }
member this.To : System.Net.Mail.MailAddressCollection
Public ReadOnly Property To As MailAddressCollection

屬性值

MailAddressCollection

可寫入的 MailAddressCollection 物件。

範例

下列程式碼範例示範如何設定 To 屬性。

static void CreateTestMessage4( String^ server )
{
   MailAddress^ from = gcnew MailAddress( L"ben@contoso.com" );
   MailAddress^ to = gcnew MailAddress( L"Jane@contoso.com" );
   MailMessage^ message = gcnew MailMessage( from,to );
   message->Subject = L"Using the SmtpClient class.";
   message->Body = L"Using this feature, you can send an email message from an application very easily.";
   SmtpClient^ client = gcnew SmtpClient( server );
   Console::WriteLine( L"Sending an email message to {0} by using SMTP host {1} port {2}.", to, client->Host, client->Port );
   client->Send( message );
   client->~SmtpClient();
}
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());
  }
}
Public Shared Sub CreateTestMessage4(ByVal server As String)
    Dim from As MailAddress = New MailAddress("ben@contoso.com")
    Dim [to] As MailAddress = New MailAddress("Jane@contoso.com")
    Dim message As MailMessage = 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."
    Dim client As SmtpClient = 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 ex As Exception
        Console.WriteLine("Exception caught in CreateTestMessage4(): {0}", ex.ToString())
    End Try
End Sub

備註

屬性 To 用來指定電子郵件訊息的 [收件者] 行上的位址。 若要將收件者新增至電子郵件訊息,請建立 MailAddress 收件者位址的 ,然後將該物件新增至此屬性所傳回的集合。

適用於