SmtpClient.OnSendingDataStream Event


Occurs when the client is sending the e-mail data to the smtp server.

[Visual Basic]
Public Event OnSendingDataStream As OnSendingDataStreamEventHandler
Public Delegate Sub OnSendingDataStreamEventHandler( _
    ByVal sender As Object, _
    ByVal sent As Integer, _
    ByVal total As Integer, _
    ByRef cancel As Boolean _
)
[C#]
public event OnSendingDataStreamEventHandler OnSendingDataStream;
public delegate void OnSendingDataStreamEventHandler( 
     object sender, 
     int sent,
     int total,
     ref bool cancel 
);
[C++]
public: __event OnSendingDataStreamEventHandler^ OnSendingDataStream;
public __gc __delegate void OnSendingDataStreamEventHandler( 
     Object^ sender, 
     int sent,
     int total,
     Boolean % cancel 
);
[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

sender
The source (SmtpClient instance) of the event.
sent
The number of bytes sent to the smtp server.
total
The number of bytes in email content.
cancel
Gets or sets a value indicating whether the task (sends email or tests email) should be canceled.

Remarks

If cancel parameter is set to true in the event, the client terminates the sending immediately and a SmtpTerminatedException exception will be thrown.

Example

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

[VB - Send Email with Event Handler]

Imports EASendMail

Module Module1

    Sub OnQuit(
        ByVal sender As Object,
        ByRef cancel As Boolean
        )
        Console.WriteLine("Disconnecting ... ")
    End Sub

    Sub OnAuthorized(
        ByVal sender As Object,
        ByRef cancel As Boolean
        )
        Console.WriteLine("Authorized")
    End Sub

    Sub OnIdle(
        ByVal sender As Object,
        ByRef cancel As Boolean
        )
        ' Application.DoEvents()
    End Sub

    Sub OnSendingDataStream(
        ByVal sender As Object,
        ByVal sent As Integer,
        ByVal total As Integer,
        ByRef cancel As Boolean
    )
        Console.WriteLine(String.Format("{0}/{1} sent", sent, total))
    End Sub

    Sub OnConnected(
        ByVal sender As Object,
        ByRef cancel As Boolean
    )
        Console.Write("Connected")
    End Sub

    Sub SendMail()

        Try
            Dim oServer As SmtpServer = New SmtpServer("myserveraddress")
            ' SMTP user authentication
            oServer.User = "myusername"
            oServer.Password = "mypassword"

            ' Most mordern SMTP servers require SSL/TLS connection now.
            ' ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
            oServer.ConnectType = SmtpConnectType.ConnectTryTLS

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

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

            Dim oSmtp As SmtpClient = New SmtpClient

            AddHandler oSmtp.OnIdle, AddressOf OnIdle
            AddHandler oSmtp.OnAuthorized, AddressOf OnAuthorized
            AddHandler oSmtp.OnConnected, AddressOf OnConnected
            AddHandler oSmtp.OnQuit, AddressOf OnQuit
            AddHandler oSmtp.OnSendingDataStream, AddressOf OnSendingDataStream

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

        Catch exp As SmtpTerminatedException
            Console.WriteLine(exp.Message)
        Catch exp As Exception
            Console.WriteLine("Exception: {0}", exp.Message)
        End Try
    End Sub

    Sub Main()
        SendMail()
    End Sub

End Module


[C# - Send Email with Event Handler] using System; using EASendMail; namespace Test { class Class1 { public static void OnQuit( object sender, ref bool cancel ) { Console.WriteLine("Disconnecting ... "); } public static void OnAuthorized( object sender, ref bool cancel ) { Console.WriteLine("Authorized"); } public static void OnIdle( object sender, ref bool cancel ) { // Application.DoEvents(); } public static void OnSendingDataStream( object sender, int sent, int total, ref bool cancel ) { Console.WriteLine(String.Format("{0}/{1} sent", sent, total)); } public static void OnConnected( object sender, ref bool cancel ) { Console.Write("Connected\r\n"); } static void SendMail() { try { SmtpServer oServer = new SmtpServer("myserveraddress"); // SMTP user authentication oServer.User = "myusername"; oServer.Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = SmtpConnectType.ConnectTryTLS; SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = new MailAddress("from@adminsystem.com"); oMail.To.Add(new MailAddress("to@adminsystem.com")); oMail.Subject = "test subject"; oMail.TextBody = "test body"; SmtpClient oSmtp = new SmtpClient(); oSmtp.OnAuthorized += new SmtpClient.OnAuthorizedEventHandler(OnAuthorized); oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler(OnIdle); oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler(OnConnected); oSmtp.OnSendingDataStream += new SmtpClient.OnSendingDataStreamEventHandler(OnSendingDataStream); oSmtp.OnQuit += new SmtpClient.OnQuitEventHandler(OnQuit); oSmtp.SendMail(oServer, oMail); Console.WriteLine("Message was sent"); } catch (SmtpTerminatedException exp) { Console.WriteLine(exp.Message); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } } static void Main(string[] args) { SendMail(); } } }
[C++/CLI - Send Email with Event Handler] using namespace System; using namespace EASendMail; static void OnQuit( Object^ sender, Boolean % cancel ) { Console::WriteLine("Disconnecting ... "); } static void OnAuthorized( Object^ sender, Boolean % cancel ) { Console::WriteLine("Authorized"); } static void OnIdle( Object^ sender, Boolean % cancel ) { // Application::DoEvents(); } static void OnSendingDataStream( Object^ sender, int sent, int total, Boolean % cancel ) { Console::WriteLine("{0}/{1} sent", sent.ToString(), total.ToString()); } static void OnConnected( Object^ sender, Boolean % cancel ) { Console::Write("Connected\r\n"); } static void SendMail() { try { SmtpServer ^oServer = gcnew SmtpServer("myserveraddres"); // SMTP user authentication oServer->User = "myusername"; oServer->Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer->ConnectType = SmtpConnectType::ConnectTryTLS; SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew MailAddress("from@adminsystem.com"); oMail->To->Add(gcnew MailAddress("to@adminsystem.com")); oMail->Subject = "test subject"; oMail->TextBody = "test body"; SmtpClient ^oSmtp = gcnew SmtpClient(); oSmtp->OnAuthorized += gcnew SmtpClient::OnAuthorizedEventHandler(&OnAuthorized); oSmtp->OnIdle += gcnew SmtpClient::OnIdleEventHandler(&OnIdle); oSmtp->OnConnected += gcnew SmtpClient::OnConnectedEventHandler(&OnConnected); oSmtp->OnSendingDataStream += gcnew SmtpClient::OnSendingDataStreamEventHandler(&OnSendingDataStream); oSmtp->OnQuit += gcnew SmtpClient::OnQuitEventHandler(&OnQuit); oSmtp->SendMail(oServer, oMail); Console::WriteLine("message was sent"); } catch (SmtpTerminatedException ^exp) { Console::WriteLine(exp->Message); } catch (Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } } int main(array<String ^> ^args) { SendMail(); return 0; }

See Also

SmtpClient.SendMail Method
SmtpClient.BeginSendMail Method

Online Tutorials

Send Email with Event Handler in C# - Tutorial
Send Email with Event Handler in VB - Tutorial
Send Email with Event Handler in Managed C++/CLI - Tutorial