Send Email with Event Handler in C#

In previous section, I introduced how to encrypt email with digital certificate. In this section, I will introduce how to send email with event handler in C#.

Introduction

In previous examples, after SendMail method is invoked, if you want to know the progress of the email sending, you should use Event Handler. The following sample codes demonstrate how to use Event Handler to monitor the progress of email sending.

Note

Remarks: All of samples in this section are based on first section: Send email in a simple C# project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[C# - Send Email with Event Handler - Example]

The following example codes demonstrate how to send email with event handler in C#.

Note

To get the full sample projects, please refer to Samples section.

using System;
using System.Collections.Generic;
using System.Text;
using EASendMail; //add EASendMail namespace
namespace mysendemail
{
    class Program
    {
       public static void OnSecuring(
            object sender,
            ref bool cancel
        )
        {
            Console.WriteLine("Securing ... ");
        }
        public static void OnAuthorized(
            object sender,
            ref bool cancel
        )
        {
            Console.WriteLine("Authorized");
        }
        public static void OnIdle(
            object sender,
            ref bool cancel
        )
        {
            // this event is fired when the SmtpClient is wait for response from
            // smtp server, if you add Application.DoEvents in windows form application,
            // it can prevent your form has no response before SendMail is not returned.
            // Application.DoEvents();
        }
        public static void OnConnected(
            object sender,
            ref bool cancel
        )
        {
            Console.Write("Connected\r\n");
        }
        public static void OnSendingDataStream(
            object sender,
            int sent,
            int total,
            ref bool cancel
        )
        {
            Console.WriteLine(String.Format("{0}/{1} sent", sent, total));
        }

        static void Main(string[] args)
        {
            SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            // Set sender email address, please change it to yours
            oMail.From = "test@emailarchitect.net";

            // Set recipient email address, please change it to yours
            oMail.To = "support@emailarchitect.net";

            // Set email subject
            oMail.Subject = "test email from c# with event handler";

            // Set email body
            oMail.TextBody = "this is a test email sent from c# project with event handler";
            // Your smtp server address
            SmtpServer oServer = new SmtpServer("smtp.emailarchitect.net");

            // User and password for ESMTP authentication, if your server doesn't require
            // User authentication, please remove the following codes.
            oServer.User = "test@emailarchitect.net";
            oServer.Password = "testpassword";

            // If your smtp server requires SSL connection, please add this line
            // oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
            // Add event handlers to current SmtpClient instance.

            oSmtp.OnAuthorized += new SmtpClient.OnAuthorizedEventHandler( OnAuthorized );
            oSmtp.OnIdle += new SmtpClient.OnIdleEventHandler( OnIdle );
            oSmtp.OnConnected += new SmtpClient.OnConnectedEventHandler( OnConnected );
            oSmtp.OnSecuring += new SmtpClient.OnSecuringEventHandler( OnSecuring );
            oSmtp.OnSendingDataStream +=
                new SmtpClient.OnSendingDataStreamEventHandler( OnSendingDataStream );
            try
            {
                Console.WriteLine("start to send email ...");
                oSmtp.SendMail(oServer, oMail);
                Console.WriteLine("email was sent successfully!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
            }
        }
    }
}

Next Section

At next section I will introduce how to send email asynchronously in C#.

Appendix

Comments

If you have any comments or questions about above example codes, please click here to add your comments.