-1

So the question is: How do I get SmtpClient's errors in such a way that I can pass them on to containing object? I can get Event properties from SendCompletedEventHandler and display them in console application with single static method but I don't have so much luck in WinForms. Here is a part of code. I'm not showing the entire code because other than capturing results, the class works flawlessly. Object is instantiated in a form and sends an email. But I can't retrieve event's results.

public string EmailResult { get; private set; }
private SmtpClient client;
(...)
public void SendEmail()
        {
            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
                            client.Send(message);
            message.Dispose();            
        }
private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
        {
            string token = (String)e.UserState;

            if (e.Cancelled)
            {
                this.EmailResult = "Cancelled";
            }

            if (e.Error != null)
            {
                this.EmailResult = e.Error.ToString();
            }
            else
            {
                this.EmailResult = "Sent successfully";
            }

        }
ArtK
  • 1,157
  • 5
  • 17
  • 31

1 Answers1

1

All you have to do is provide an event handling method from the form that will contain your display controls. One way of doing this is to expose SmtpClient from the object that contains SmtpClient so any other object could access events of SmtpClient. Then add your event handling method from that object. More advanced way would be to create new event in class that contains SmtpClient and pass handling method to containing object which then would pass it to unexposed SmtpClient. But this could get messy.