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";
}
}