Wrapper class for loading Email Messages Async using Aspose.Email

Wrapper class for loading Email Messages

There are a number of occurrences where it is desirable to have Time out functionality in order to abort an action that is taking unnecessary long time. This article provides a sample class to achieve the functionality of Time out functionality while loading EML/MSG files that could lead to very long delays or fail to load. Since Time out is not something directly related to disk or network read/write operation, it’s of little use to implement this feature within the API than have it implemented at the user end by writing a wrapper class around Aspose.

Canceling a long running thread can be achieved with the use of a wrapped delegate that passes out the thread, to be killed, in a local variable inside the method that initiated it. The long running thread is cancelled by aborting it and control is returned to the main application. The following code sample provides a sample wrapper class around the Aspose.Email library. The code follows sample usage of the wrapper class as well.

Programming Sample with .NET 3.5 and above

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
public class WrapperMailMessage
{
private MailMessage msg;
public string strFileName;
public WrapperMailMessage(string strMessageFile)
{
msg = null;
strFileName = strMessageFile;
// Now load the EML file with a timout of 4 seconds
CallWithTimeout(LoadEmlFile, 4000);
}
// Function to load the provided EML file
public void LoadEmlFile()
{
msg = MailMessage.Load(strFileName);
}
// TimeOut function that calls the load EML function with Timeout
public void CallWithTimeout(Action action, int timeoutMilliseconds)
{
Thread threadToKill = null;
Action wrappedAction = () =>
{
threadToKill = Thread.CurrentThread;
action();
};
IAsyncResult result = wrappedAction.BeginInvoke(null, null);
if (result.AsyncWaitHandle.WaitOne(timeoutMilliseconds))
{
wrappedAction.EndInvoke(result);
}
else
{
threadToKill.Abort();
}
}
// This function returns the loaded EML message or NULL in case of unsuccessful load
public MailMessage GetMailMessage()
{
return msg;
}
}

Programming Sample with .NET 2.0

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
public class WrapperMailMessage_2_0
{
private MailMessage msg;
public string strFileName;
public WrapperMailMessage_2_0(string strMessageFile)
{
msg = null;
strFileName = strMessageFile;
// Now load the EML file with a timout of 4 seconds
CallWithTimeout(new Action(LoadEmlFile), 4000);
}
// Function to load the provided EML file
public void LoadEmlFile()
{
msg = MailMessage.Load(strFileName);
}
private static void CallWithTimeout(Delegate dlgt, int timeout, params object[] args)
{
ManualResetEvent waitHandle = new ManualResetEvent(false);
Thread t = new Thread(new ThreadStart(delegate
{
dlgt.Method.Invoke(dlgt.Target, args);
waitHandle.Set();
}));
t.Start();
bool bExec = waitHandle.WaitOne(timeout);
if (!bExec)
t.Abort();
}
// This needs to be here as .NET 2.0 doesn't allow argument less calls for this
public delegate void Action();
// This function returns the loaded EML message or NULL in case of unsuccessful load
public MailMessage GetMailMessage()
{
return msg;
}
}

Example Usage

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Define a wrapper class object and pass the EML file name to load
WrapperMailMessage wrapperObj = new WrapperMailMessage(dataDir + "Message.eml");
// Check if the MailMessage is loaded or NULL
if (wrapperObj.GetMailMessage() != null)
{
MailMessage msg = wrapperObj.GetMailMessage();
Console.WriteLine("Message loaded succesfully::: {0}", msg.Subject);
}
else
Console.WriteLine("Message could not be loaded...");