SmtpClient.Send 方法

定義

傳送電子郵件訊息給 SMTP 伺服器進行傳遞。Sends an email message to an SMTP server for delivery. 在傳送訊息時,會封鎖這些方法。These methods block while the message is being transmitted.

多載

Send(MailMessage)

傳送指定的訊息給 SMTP 伺服器進行傳遞。Sends the specified message to an SMTP server for delivery.

Send(String, String, String, String)

傳送指定的電子郵件訊息給 SMTP 伺服器進行傳遞。Sends the specified email message to an SMTP server for delivery. 訊息寄件者、收件者、主旨和訊息主體是使用 String 物件來指定。The message sender, recipients, subject, and message body are specified using String objects.

Send(MailMessage)

傳送指定的訊息給 SMTP 伺服器進行傳遞。Sends the specified message to an SMTP server for delivery.

public:
 void Send(System::Net::Mail::MailMessage ^ message);
public void Send (System.Net.Mail.MailMessage message);
member this.Send : System.Net.Mail.MailMessage -> unit
Public Sub Send (message As MailMessage)

參數

message
MailMessage

MailMessage,包含要傳送的訊息。A MailMessage that contains the message to send.

例外狀況

messagenullmessage is null.

這個 SmtpClient 有正在進行中的 SendAsync 呼叫。This SmtpClient has a SendAsync call in progress.

-或--or- FromnullFrom is null.

-或--or- ToCCBcc 屬性中沒有指定任何收件者。There are no recipients specified in To, CC, and Bcc properties.

-或--or- DeliveryMethod 屬性設定為 Network,且 HostnullDeliveryMethod property is set to Network and Host is null.

-或--or- DeliveryMethod 屬性設為 Network,且 Host 等於空字串 ("")。DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-或--or- DeliveryMethod 屬性設定為 Network ,而且 Port 為零、負數或大於 65,535。DeliveryMethod property is set to Network and Port is zero, a negative number, or greater than 65,535.

這個物件已經過處置。This object has been disposed.

與 SMTP 伺服器的連接失敗。The connection to the SMTP server failed.

-或--or- 驗證失敗。Authentication failed.

-或--or- 作業逾時。The operation timed out. -或--or- EnableSsl 設定為 true,但 DeliveryMethod 屬性設定為 SpecifiedPickupDirectoryPickupDirectoryFromIisEnableSsl is set to true but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.

-或--or- EnableSsl 設為 true, 不過 SMTP 郵件伺服器並未在對 EHLO 命令的回應中通告 STARTTLS。EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

無法將 message 傳遞給 ToCCBcc 中的一個收件者。The message could not be delivered to one of the recipients in To, CC, or Bcc.

無法將 message 傳遞給 ToCCBcc 中的兩個或多個收件者。The message could not be delivered to two or more of the recipients in To, CC, or Bcc.

範例

下列程式碼範例將示範如何使用這個方法。The following code example demonstrates using this method.

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

備註

傳送電子郵件時,此方法會封鎖。This method blocks while the email is transmitted. 您可以使用屬性來指定超時值, Timeout 以確保方法會在經過指定的時間量之後返回。You can specify a time-out value using the Timeout property to ensure that the method returns after a specified amount of time elapses.

在呼叫這個方法之前, HostPort 屬性必須透過設定檔來設定,方法是設定相關屬性,或將此資訊傳遞至函式 SmtpClient(String, Int32)Before calling this method, the Host and Port properties must be set either through the configuration files by setting the relevant properties, or by passing this information into the SmtpClient(String, Int32) constructor.

如果有以非同步方式傳送的訊息,您就無法呼叫這個方法。You cannot call this method if there is a message being sent asynchronously.

如果 SMTP 主機需要認證,您必須在呼叫這個方法之前設定這些認證。If the SMTP host requires credentials, you must set them before calling this method. 若要指定認證,請使用 UseDefaultCredentialsCredentials 屬性。To specify credentials, use the UseDefaultCredentials or Credentials properties.

如果您收到 SmtpException 例外狀況,請檢查 StatusCode 屬性以找出作業失敗的原因。If you receive an SmtpException exception, check the StatusCode property to find the reason the operation failed. SmtpException也可以包含內部例外狀況,指出作業失敗的原因。The SmtpException can also contain an inner exception that indicates the reason the operation failed.

使用傳送電子郵件 Send 給多個收件者,而 SMTP 伺服器接受某些收件者為有效並拒絕其他收件者時, Send 會傳送電子郵件給接受的收件者,然後在 SmtpFailedRecipientsException (, SmtpFailedRecipientException 如果只拒絕一個收件者) ,則為。When sending email using Send to multiple recipients and the SMTP server accepts some recipients as valid and rejects others, Send sends email to the accepted recipients and then a SmtpFailedRecipientsException is thrown (or a SmtpFailedRecipientException if only one recipient is rejected). SmtpFailedRecipientsException包含已拒絕之收件者的清單。A SmtpFailedRecipientsException contains a list of the recipients that were rejected.

注意

如果 EnableSsl 屬性設定為 true ,而且 SMTP 郵件伺服器未在 STARTTLS 中通告對 EHLO 命令的回應,則對 Send 或方法的呼叫 SendAsync 將會擲回 SmtpExceptionIf the EnableSsl property is set to true, and the SMTP mail server does not advertise STARTTLS in the response to the EHLO command, then a call to the Send or SendAsync methods will throw an SmtpException.

適用於

Send(String, String, String, String)

傳送指定的電子郵件訊息給 SMTP 伺服器進行傳遞。Sends the specified email message to an SMTP server for delivery. 訊息寄件者、收件者、主旨和訊息主體是使用 String 物件來指定。The message sender, recipients, subject, and message body are specified using String objects.

public:
 void Send(System::String ^ from, System::String ^ recipients, System::String ^ subject, System::String ^ body);
public:
 void Send(System::String ^ from, System::String ^ to, System::String ^ subject, System::String ^ body);
public void Send (string from, string recipients, string? subject, string? body);
public void Send (string from, string recipients, string subject, string body);
public void Send (string from, string to, string subject, string body);
member this.Send : string * string * string * string -> unit
member this.Send : string * string * string * string -> unit
Public Sub Send (from As String, recipients As String, subject As String, body As String)
Public Sub Send (from As String, to As String, subject As String, body As String)

參數

from
String

String,包含訊息寄件者的地址資訊。A String that contains the address information of the message sender.

recipientsto
String

String,包含訊息要傳送到的地址。A String that contains the addresses that the message is sent to.

subject
String

String,包含訊息的主旨行。A String that contains the subject line for the message.

body
String

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

例外狀況

fromnullfrom is null.

-或--or- recipientsnullrecipients is null.

fromEmptyfrom is Empty.

-或--or- recipientsEmptyrecipients is Empty.

這個 SmtpClient 有正在進行中的 SendAsync 呼叫。This SmtpClient has a SendAsync call in progress.

-或--or- DeliveryMethod 屬性設定為 Network,且 HostnullDeliveryMethod property is set to Network and Host is null.

-或--or- DeliveryMethod 屬性設為 Network,且 Host 等於空字串 ("")。DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-或--or- DeliveryMethod 屬性設定為 Network ,而且 Port 為零、負數或大於 65,535。DeliveryMethod property is set to Network and Port is zero, a negative number, or greater than 65,535.

這個物件已經過處置。This object has been disposed.

與 SMTP 伺服器的連接失敗。The connection to the SMTP server failed.

-或--or- 驗證失敗。Authentication failed.

-或--or- 作業逾時。The operation timed out. -或--or- EnableSsl 設定為 true,但 DeliveryMethod 屬性設定為 SpecifiedPickupDirectoryPickupDirectoryFromIisEnableSsl is set to true but the DeliveryMethod property is set to SpecifiedPickupDirectory or PickupDirectoryFromIis.

-或--or- EnableSsl 設為 true, 不過 SMTP 郵件伺服器並未在對 EHLO 命令的回應中通告 STARTTLS。EnableSsl is set to true, but the SMTP mail server did not advertise STARTTLS in the response to the EHLO command.

無法將 message 傳遞給 ToCCBcc 中的一個收件者。The message could not be delivered to one of the recipients in To, CC, or Bcc.

無法將 message 傳遞給 ToCCBcc 中的兩個或多個收件者。The message could not be delivered to two or more of the recipients in To, CC, or Bcc.

備註

傳送電子郵件時,此方法會封鎖。This method blocks while the email is transmitted. 您可以使用屬性來指定超時值, Timeout 以確保方法會在經過指定的時間量之後返回。You can specify a time-out value using the Timeout property to ensure that the method returns after a specified amount of time elapses.

在呼叫這個方法之前, HostPort 屬性必須透過設定檔來設定,方法是設定相關屬性,或將此資訊傳遞至函式 SmtpClient(String, Int32)Before calling this method, the Host and Port properties must be set either through the configuration files by setting the relevant properties, or by passing this information into the SmtpClient(String, Int32) constructor.

如果有以非同步方式傳送的訊息,您就無法呼叫這個方法。You cannot call this method if there is a message being sent asynchronously.

如果 SMTP 主機需要認證,您必須在呼叫這個方法之前設定這些認證。If the SMTP host requires credentials, you must set them before calling this method. 若要指定認證,請使用 UseDefaultCredentialsCredentials 屬性。To specify credentials, use the UseDefaultCredentials or Credentials properties.

如果您收到 SmtpException 例外狀況,請檢查 StatusCode 屬性以找出作業失敗的原因。If you receive an SmtpException exception, check the StatusCode property to find the reason the operation failed. SmtpException也可以包含內部例外狀況,指出作業失敗的原因。The SmtpException can also contain an inner exception that indicates the reason the operation failed.

使用傳送電子郵件 Send 給多個收件者,而 SMTP 伺服器接受某些收件者為有效並拒絕其他收件者時, Send 會傳送電子郵件給接受的收件者,然後在 SmtpFailedRecipientsException (, SmtpFailedRecipientException 如果只拒絕一個收件者) ,則為。When sending email using Send to multiple recipients and the SMTP server accepts some recipients as valid and rejects others, Send sends email to the accepted recipients and then a SmtpFailedRecipientsException is thrown (or a SmtpFailedRecipientException if only one recipient is rejected). SmtpFailedRecipientsException包含已拒絕之收件者的清單。A SmtpFailedRecipientsException contains a list of the recipients that were rejected.

注意

如果 EnableSsl 屬性設定為 true ,而且 SMTP 郵件伺服器未在 STARTTLS 中通告對 EHLO 命令的回應,則對 Send 或方法的呼叫 SendAsync 將會擲回 SmtpExceptionIf the EnableSsl property is set to true, and the SMTP mail server does not advertise STARTTLS in the response to the EHLO command, then a call to the Send or SendAsync methods will throw an SmtpException.

適用於