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

一个包含要发送的邮件的 MailMessageA 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 属性设置为 NetworkHost 等于空字符串 ("")。DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-或--or- DeliveryMethod 属性被设为 NetworkPort 为零、负数或大于 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.

在调用此方法之前,必须通过设置相关属性或通过将此信息传递到 SmtpClient(String, Int32) 构造函数来设置 HostPort 属性。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 邮件服务器不在对 EHLO 命令的响应中公布 STARTTLS,则对 SendSendAsync 方法的调用将引发 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 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

一个包含邮件发件人的地址信息的 StringA String that contains the address information of the message sender.

recipientsto
String

一个包含邮件收件人的地址的 StringA String that contains the addresses that the message is sent to.

subject
String

一个包含邮件主题行的 StringA String that contains the subject line for the message.

body
String

一个包含邮件正文的 StringA 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 属性设置为 NetworkHost 等于空字符串 ("")。DeliveryMethod property is set to Network and Host is equal to the empty string ("").

-或--or- DeliveryMethod 属性被设为 NetworkPort 为零、负数或大于 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.

在调用此方法之前,必须通过设置相关属性或通过将此信息传递到 SmtpClient(String, Int32) 构造函数来设置 HostPort 属性。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 邮件服务器不在对 EHLO 命令的响应中公布 STARTTLS,则对 SendSendAsync 方法的调用将引发 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.

适用于