MailMessage MailMessage MailMessage MailMessage Constructors

定義

多載

MailMessage() MailMessage() MailMessage()

初始化 MailMessage 類別的空執行個體。Initializes an empty instance of the MailMessage class.

MailMessage(MailAddress, MailAddress) MailMessage(MailAddress, MailAddress) MailMessage(MailAddress, MailAddress) MailMessage(MailAddress, MailAddress)

使用指定的 MailMessage 類別物件,初始化 MailAddress 類別的新執行個體。Initializes a new instance of the MailMessage class by using the specified MailAddress class objects.

MailMessage(String, String) MailMessage(String, String) MailMessage(String, String) MailMessage(String, String)

使用指定的 MailMessage 類別物件,初始化 String 類別的新執行個體。Initializes a new instance of the MailMessage class by using the specified String class objects.

MailMessage(String, String, String, String) MailMessage(String, String, String, String) MailMessage(String, String, String, String) MailMessage(String, String, String, String)

初始化 MailMessage 類別的新執行個體。Initializes a new instance of the MailMessage class.

MailMessage() MailMessage() MailMessage()

初始化 MailMessage 類別的空執行個體。Initializes an empty instance of the MailMessage class.

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

備註

從設定為 mailSettings <smtp>元素 (網路設定)之 network 元素中的值 (如果有的話)。From is set to the value in the network element for mailSettings<smtp> Element (Network Settings), if it exists.

MailMessage(MailAddress, MailAddress) MailMessage(MailAddress, MailAddress) MailMessage(MailAddress, MailAddress) MailMessage(MailAddress, MailAddress)

使用指定的 MailMessage 類別物件,初始化 MailAddress 類別的新執行個體。Initializes a new instance of the MailMessage class by using the specified MailAddress class objects.

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)

參數

from
MailAddress MailAddress MailAddress MailAddress

MailAddress,包含電子郵件訊息寄件者的地址。A MailAddress that contains the address of the sender of the email message.

to
MailAddress MailAddress MailAddress MailAddress

MailAddress,包含電子郵件訊息收件者的地址。A MailAddress that contains the address of the recipient of the email message.

例外狀況

fromnullfrom is null.

-或--or- tonullto is null.

fromto 的格式不正確。from or to is malformed.

範例

下列程式碼範例將示範如何呼叫這個函式。The following code example demonstrates calling this constructor.

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

備註

屬性會使用from進行初始化, 而To屬性會使用to進行初始化。 FromThe From property is initialized using from and the To property is initialized using to.

MailMessage(String, String) MailMessage(String, String) MailMessage(String, String) MailMessage(String, String)

使用指定的 MailMessage 類別物件,初始化 String 類別的新執行個體。Initializes a new instance of the MailMessage class by using the specified String class objects.

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)

參數

from
String String String String

String,包含電子郵件訊息寄件者的地址。A String that contains the address of the sender of the email message.

to
String String String String

String,包含電子郵件訊息收件者的地址。A String that contains the addresses of the recipients of the email message. 必須使用逗號字元 (",") 分隔多個電子郵件地址。Multiple email addresses must be separated with a comma character (",").

例外狀況

fromnullfrom is null.

-或--or- tonullto is null.

fromEmpty ("")。from is Empty ("").

-或--or- toEmpty ("")。to is Empty ("").

fromto 的格式不正確。from or to is malformed.

範例

下列程式碼範例將示範如何呼叫這個函式。The following code example demonstrates calling this constructor.

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

備註

屬性會使用from進行初始化, 而To屬性會使用to進行初始化。 FromThe From property is initialized using from and the To property is initialized using to.

MailMessage(String, String, String, String) MailMessage(String, String, String, String) MailMessage(String, String, String, String) MailMessage(String, String, String, String)

初始化 MailMessage 類別的新執行個體。Initializes a new instance of the MailMessage class.

public:
 MailMessage(System::String ^ from, System::String ^ to, System::String ^ subject, System::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)

參數

from
String String String String

String,包含電子郵件訊息寄件者的地址。A String that contains the address of the sender of the email message.

to
String String String String

String,包含電子郵件訊息收件者的地址。A String that contains the addresses of the recipients of the email message. 必須使用逗號字元 (",") 分隔多個電子郵件地址。Multiple email addresses must be separated with a comma character (",").

subject
String String String String

String,包含主旨文字。A String that contains the subject text.

body
String String String String

String,包含訊息主體。A String that contains the message body.

例外狀況

fromnullfrom is null.

-或--or- tonullto is null.

fromEmpty ("")。from is Empty ("").

-或--or- toEmpty ("")。to is Empty ("").

fromto 的格式不正確。from or to is malformed.

範例

下列程式碼範例將示範如何呼叫這個函式。The following code example demonstrates calling this constructor.

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

備註

MailMessage物件的屬性會初始化, 如下所示:The properties for the new MailMessage object are initialized as follows:

參數Parameter 屬性Property
from From
to To
subject Subject
body Body

根據預設, 主旨和內容會假設為使用以本機電腦設定為基礎的預設編碼。By default, the subject and content are assumed to use the default encoding based on local computer settings. 您可以使用SubjectEncoding和屬性來指定不同的編碼方式。 BodyEncodingUse the BodyEncoding and SubjectEncoding properties to specify different encodings.

適用於