I have a couple of methods already to send an email message syncronously.
If an email fails, I use this fairly standard code:
static void CheckExceptionAndResend(SmtpFailedRecipientsException ex, SmtpClient client, MailMessage message)
{
for (int i = 0; i < ex.InnerExceptions.Length -1; i++)
{
var status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable ||
status == SmtpStatusCode.TransactionFailed)
{
System.Threading.Thread.Sleep(3000);
client.Send(message);
}
}
}
However, I'm trying to achieve the same using SendAsync(). This is the code I have so far:
public static void SendAsync(this MailMessage message)
{
message.ThrowNull("message");
var client = new SmtpClient();
// Set the methods that is called once the event ends
client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
// Unique identifier for this send operation
string userState = Guid.NewGuid().ToString();
client.SendAsync(message, userState);
// Clean up
message.Dispose();
}
static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
{
// Get the unique identifier for this operation.
String token = (string)e.UserState;
if (e.Error.IsNotNull())
{
// Do somtheing
}
}
The question is using token and/or e.Error how do I get the exception so that I can make the necessary checks for StatusCode and then resend?
I've been Googling all afternoon but havn't found anything positive.
Any advice appreciated.