SmtpException 类

定义

表示当 SmtpClient 无法完成 SendSendAsync 操作时引发的异常。

public ref class SmtpException : Exception
public class SmtpException : Exception
[System.Serializable]
public class SmtpException : Exception
type SmtpException = class
    inherit Exception
    interface ISerializable
[<System.Serializable>]
type SmtpException = class
    inherit Exception
    interface ISerializable
Public Class SmtpException
Inherits Exception
继承
SmtpException
派生
属性
实现

示例

下面的代码示例在引发异常时 SmtpException 显示错误消息。

static void RetryIfBusy( 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 = "Using the SmtpClient class.";
   message->Subject = L"Using the SmtpClient class.";
   message->Body = L"Using this feature, you can send an email message from an application very easily.";
   
   // Add a carbon copy recipient.
   MailAddress^ copy = gcnew MailAddress( L"Notifications@contoso.com" );
   message->CC->Add( copy );
   SmtpClient^ client = gcnew SmtpClient( server );
   
   // Include credentials if the server requires them.
   client->Credentials = dynamic_cast<ICredentialsByHost^>(CredentialCache::DefaultNetworkCredentials);
   Console::WriteLine( L"Sending an email message to {0} using the SMTP host {1}.", to->Address, client->Host );
   try
   {
      client->Send( message );
   }
   catch ( SmtpFailedRecipientsException^ ex ) 
   {
      for ( int i = 0; i < ex->InnerExceptions->Length; i++ )
      {
         SmtpStatusCode status = ex->InnerExceptions[ i ]->StatusCode;
         if ( status == SmtpStatusCode::MailboxBusy || status == SmtpStatusCode::MailboxUnavailable )
         {
            Console::WriteLine( L"Delivery failed - retrying in 5 seconds." );
            System::Threading::Thread::Sleep( 5000 );
            client->Send( message );
         }
         else
         {
            Console::WriteLine( L"Failed to deliver message to {0}", ex->InnerExceptions[ i ] );
         }

      }
   }
   catch ( Exception^ ex )
   {
       Console::WriteLine(L"Exception caught in RetryIfBusy(): {0}", 
                     ex->ToString() );
   }
   finally
   {
      client->~SmtpClient();
   }
}
public static void RetryIfBusy(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.Subject = "Using the SmtpClient class.";
    message.Body = @"Using this feature, you can send an email message from an application very easily.";
    // Add a carbon copy recipient.
    MailAddress copy = new MailAddress("Notifications@contoso.com");
    message.CC.Add(copy);
    SmtpClient client = new SmtpClient(server);
    // Include credentials if the server requires them.
    client.Credentials = (ICredentialsByHost)CredentialCache.DefaultNetworkCredentials;
    Console.WriteLine("Sending an email message to {0} using the SMTP host {1}.",
         to.Address, client.Host);
    try
    {
        client.Send(message);
    }
    catch (SmtpFailedRecipientsException ex)
    {
        for (int i = 0; i < ex.InnerExceptions.Length; i++)
        {
            SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
            if (status == SmtpStatusCode.MailboxBusy ||
                status == SmtpStatusCode.MailboxUnavailable)
            {
                Console.WriteLine("Delivery failed - retrying in 5 seconds.");
                System.Threading.Thread.Sleep(5000);
                client.Send(message);
            }
            else
            {
                Console.WriteLine("Failed to deliver message to {0}",
                    ex.InnerExceptions[i].FailedRecipient);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Exception caught in RetryIfBusy(): {0}",
                ex.ToString());
    }
}

注解

属性 StatusCode 包含 SMTP 服务器返回的状态代码。

构造函数

SmtpException()

初始化 SmtpException 类的新实例。

SmtpException(SerializationInfo, StreamingContext)

SmtpExceptionSerializationInfo 类的指定实例初始化 StreamingContext 类的新实例。

SmtpException(SmtpStatusCode)

用指定的状态代码初始化 SmtpException 类的新实例。

SmtpException(SmtpStatusCode, String)

用指定的状态代码和错误信息初始化 SmtpException 类的新实例。

SmtpException(String)

使用指定的错误消息初始化 SmtpException 类的新实例。

SmtpException(String, Exception)

使用指定的错误消息和内部异常初始化 SmtpException 类的新实例。

属性

Data

获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。

(继承自 Exception)
Message

获取描述当前异常的消息。

(继承自 Exception)
Source

获取或设置导致错误的应用程序或对象的名称。

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。

(继承自 Exception)
StatusCode

获取传输电子邮件时由 SMTP 服务器返回的状态代码。

TargetSite

获取引发当前异常的方法。

(继承自 Exception)

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。

(继承自 Exception)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

用序列化 SerializationInfo 所需的数据填充 SmtpException 实例。

GetType()

获取当前实例的运行时类型。

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。

(继承自 Exception)

事件

SerializeObjectState
已过时.

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。

(继承自 Exception)

显式接口实现

ISerializable.GetObjectData(SerializationInfo, StreamingContext)

用序列化 SerializationInfo 所需的数据填充 SmtpException 实例。

适用于