I'm developing an application to archive emails and I'm using the following code to get the messages from Exchange online. The code works fine until I thread the application and impersonate more than two users. It works doing multiple threads impersonating one user. Each thread has it's own instance of IEWSclient.
This is the Error:
Cannot get message item and Mime Content ErrorAccessDenied
Access is denied. Check credentials and try again., Cannot get ID from name. validUser@validSite.com
Is this possible or is there a setting or something that I'm missing to allow impersonation of multiple users on different threads?
// Get document content
public bool getDocumentContent(object oclient, string eid, Stream save,string impersonatedUser, bool impersonate, StringBuilder err)
{
IEWSClient client = (IEWSClient)oclient;
if (impersonate)
{
client.ImpersonateUser(ItemChoice.PrimarySmtpAddress, impersonatedUser);
}
string id = suUtil.parseElementShortId(eid);
try
{
MailMessage msg = client.FetchMessage(id);
msg.Save(save, SaveOptions.DefaultMsg);
return true;
}
catch (Exception ex)
{
err.Append(ex.Message + " " + impersonatedUser);
}
return false;
}