SmtpTerminatedException Class


Represents error that sending email is cancelled by user.

System.Object
    System.Exception
        EASendMail.SmtpTerminatedException

[Visual Basic]
Public Class SmtpTerminatedException
[C#]
public class SmtpTerminatedException
[C++]
public ref class SmtpTerminatedException
[JScript]
public class SmtpTerminatedException

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Methods and Properties

Please refer to .NET Framework SDK System.Exception.

Example

[Visual Basic, C#, C++, JScript.NET] To get the full samples of EASendMail, please refer to Samples section.

[Visual Basic]
Imports EASendMail
Module Module1
    Sub OnConnected( _
    ByVal sender As Object, _
    ByRef cancel As Boolean _
    )
        Console.Write("Connected")
        cancel = True ' sets cancel to false will throw the SmtpTerminatedException
    End Sub

    Sub Main()
        SendMail()
    End Sub

    Sub SendMail()
        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        Dim oSmtp As SmtpClient = New SmtpClient

        Try
            Dim oServer As SmtpServer = New SmtpServer("myserveraddress")

            oMail.From = New MailAddress("from@adminsystem.com")
            oMail.To.Add(New MailAddress("to@adminsystem.com"))

            oMail.Subject = "test subject"
            oMail.TextBody = "test body"

            
            AddHandler oSmtp.OnConnected, AddressOf OnConnected
            
            oSmtp.SendMail(oServer, oMail)
            Console.WriteLine("Message was sent")

        Catch exp As SmtpTerminatedException
            Console.WriteLine(exp.Message)
        Catch exp As SmtpServerException
            Console.WriteLine("Exception: Server Respond: {0}", exp.ErrorMessage)
        Catch exp As System.Net.Sockets.SocketException
            Console.WriteLine("Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message)
        Catch exp As System.ComponentModel.Win32Exception
            Console.WriteLine("Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message)
        Catch exp As System.Exception
            Console.WriteLine("Exception: Common: {0}", exp.Message)
        End Try
    End Sub
End Module

[C#]
using System;
using EASendMail;

namespace Test
{
    class Class1
    {
        public static void OnConnected( 
            object sender,  
            ref bool cancel 
        )
        {
            Console.Write( "Connected\r\n" );
            cancel = true; // sets cancel to true will throw the SmtpTerminatedException
        }

        [STAThread]
        static void Main(string[] args)
        {
            SendMail();
        }

        static void SendMail()
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            try
            {
                SmtpServer oServer = new SmtpServer("myserveraddress");

                oMail.From = new MailAddress("from@adminsystem.com" );
                oMail.To.Add( new MailAddress("to@adminsystem.com" ));
                oMail.Subject = "test subject";
                oMail.TextBody = "test body";
                
                oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler( OnConnected );
                 
                oSmtp.SendMail( oServer, oMail );

                Console.WriteLine( "Message was  sent" );
            }
            catch( SmtpTerminatedException exp )
            {
                Console.WriteLine( exp.Message );
            }
            catch( SmtpServerException exp )
            {
                Console.WriteLine( "Exception: Server Respond: {0}", exp.ErrorMessage );
            }
            catch( System.Net.Sockets.SocketException exp )
            {
                Console.WriteLine( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message );
            }
            catch( System.ComponentModel.Win32Exception exp )
            {
                Console.WriteLine( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message );            
            }
            catch( System.Exception exp )
            {
                Console.WriteLine( "Exception: Common: {0}", exp.Message );         
            }
        }
    }
}

[C++]
using namespace System;
using namespace EASendMail;

public ref class SmtpClientEventHandler
{
public:
static void OnConnected( 
                 Object^ sender,  
                 Boolean % cancel 
                 )
{
    Console::Write( "Connected\r\n" );
    cancel = true; // sets cancel to true will throw the SmtpTerminatedException   
}
};

void SendMail()
{
    SmtpMail ^oMail = gcnew SmtpMail("TryIt");
    SmtpClient ^oSmtp = gcnew SmtpClient();

    try
    {
        SmtpServer ^oServer = gcnew SmtpServer("myserveraddres");

        oMail->From = gcnew MailAddress("from@adminsystem.com" );
        oMail->To->Add( gcnew MailAddress("to@adminsystem.com" ));
        oMail->Subject = "test subject";
        oMail->TextBody = "test body";
        
        oSmtp->OnConnected += gcnew SmtpClient::OnConnectedEventHandler( nullptr, SmtpClientEventHandler::OnConnected );
        
        oSmtp->SendMail( oServer, oMail );
        Console::WriteLine( "message was sent" );
    }
    catch( EASendMail::SmtpTerminatedException ^exp )
    {
        Console::WriteLine( exp->Message );
    }
    catch( EASendMail::SmtpServerException ^exp )
    {
        Console::WriteLine( "Exception: Server Respond: {0}", exp->ErrorMessage );
    }
    catch( System::Net::Sockets::SocketException ^exp )
    {
        Console::WriteLine( "Exception: Networking Error: {0} {1}", exp->ErrorCode.ToString("d"), exp->Message );
    }
    catch( System::ComponentModel::Win32Exception ^exp )
    {
        Console::WriteLine( "Exception: System Error: {0} {1}", exp->ErrorCode.ToString("d"), exp->Message );           
    }
    catch( System::Exception ^exp )
    {
        Console::WriteLine( "Exception: Common: {0}", exp->Message );           
    }
}

int _tmain()
{
    SendMail();
    return 0;
}

See Also

SmtpClient.BeginSendMail Method
SmtpClient.SendMail Method