'idle.ClientInbox' threw an exception of type 'System.ObjectDisposedException'
I implemented the MailKit SDK successfully in a console application!
But now I am trying to implement it in a windows service but facing one issue.
When I am implementing 'idle.Client.Idle(timeout.Token, idle.CancellationToken); ' in the IdleLoop method I am getting the exception Cannot access a disposed object. Object name: 'ImapClient'.
And also now I am unable to access Client.Inbox , it gives me below exception
idle.Client.Inbox' threw an exception of type 'System.ObjectDisposedException' | MailKit.IMailFolder {System.ObjectDisposedException}
The below is the stack trace of the error Cannot access a disposed object. Object name: 'ImapClient'..
at MailKit.Net.Imap.ImapClient.CheckDisposed()
at MailKit.Net.Imap.ImapClient.<IdleAsync>d__90.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at MailKit.Net.Imap.ImapClient.Idle(CancellationToken doneToken, CancellationToken cancellationToken)
at MailIt.ImapNotification.IdleLoop(Object state)
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
Sounds like you've disposed the ImapClient object.
Are you creating the ImapClient in a using statement like this?
using (var client = new ImapClient ()) {
}
If so, the client object gets disposed at the end of that using block.
Hi @jstedfast ! I just encountered same behavior, while not using using statement` Here is my pseudocode - I disconnect in desctructor:
namespace teststudio
{
class EmailManager
{
protected static MailKit.IMailFolder inbox;
protected static ImapClient client;
public EmailManager(string baseUrl)
{
client = new ImapClient();
client.Connect(host, port, ssl);
client.Authenticate(username, password);
inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
}
~EmailManager()
{
client.Disconnect(true);
}
public int getEmailCount()
{
return inbox.Count;
}
}
}
namespace mystart
{
using teststudio;
class Program
{
static void Main(string[] args)
{
EmailManager emailM = new EmailManager("https://imap.com");
Console.WriteLine(emailM.getEmailCount().ToString());
Console.ReadLine();
}
}
}
You are trying to use the ImapClient to connect to a https URL. That won't work.
Wow, thx for such fast reply. It all connects properly (this is just snippet of pseudocode). Only thing failing is at the end, when destructor is called. Then I get Cannot access a disposed object. error.
You'll have to give me a real test case, then. But I guarantee that you are disposing it (or the GC is disposing it because the object went out of scope).
Ha! This is so interesting. Digged little bit more. It only happens with this https://github.com/djfarrelly/MailDev (SSL disabled, port 143). When using outlook.office.com (SSL, 993) no error. Magic! Sorry, I cannot give you anything, as this happens on internal server behind VPN :(
More funny is, that Visual Studio GC clears the connection, without explicit closing. But not Test Studio... And error does not exists on outlook.office in Visual Studio , but exists in Test Studio. .NET version??