Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing an application that processes user emails. On start the application connects to Gmail and then once per minute checks for new emails.
After approximately 12 - 20 hours the application stops processing new emails. Using logs I found, that line
C#
Mailbox mailBox = _gmailClient.SelectMailbox("inbox");
is the last one executed in this code.
Because the code is not in try / catch block I know that this is not a crash.

Please, advise.
Thank you.

Below is a part of code that retrieves new emails, where _gmailClient is Imap4Client() connected to Gmail using ConnectSsl("imap.gmail.com", 993)

C#
OutputLog($"RetrieveAndProcessNewEmails - In locked code.");
if (!_gmailClient.IsConnected)
{
    OutputLog($"IMap client is not connected. Reconnecting...");
    if (!(ConnectIMap4Client("imap.gmail.com", 993, true) && LoginIMap4Client("email", "pass")))
        return;
}
OutputLog($"RetrieveAndProcessNewEmails - connected to GMail");
Mailbox mailBox = _gmailClient.SelectMailbox("inbox");
if (mailBox == null)
{
    OutputLog("Mailbox is null! Processing incoming emails is disabled");
    return;
}
OutputLog($"RetrieveAndProcessNewEmails - mailbox found: {MailBox2String(mailBox)}");
messages = mailBox.SearchParse("UNSEEN");
if (messages.Count == 0)
{
    OutputLog($"Currently there is no new messages...");
    return;
}
else
    OutputLog($"New {messages.Count} found.");


What I have tried:

I already asked this question on GitHub, but still there was no answer.
Posted
Updated 7-Jun-20 23:25pm
Comments
Richard Deeming 3-Jun-20 7:17am    
If you're keeping a single connection alive for that many hours, maybe it's timing out. Have you tried disconnecting after each run?
Haifovchanin 3-Jun-20 7:47am    
As I saw in log-file, the connection was broken, then my application automatically reconnected to Gmail. Approximately 10 minutes after SelectMailbox function hung.
When reconnecting, I create a new instance of Imap4Client.
By the way, time between re-connection and function hanging differs from run to run.
Richard Deeming 3-Jun-20 7:48am    
As I said, I suspect it would be better to open the connection, process the new messages, and then close the connection each time, rather than trying to keep the connection open for many hours.
Haifovchanin 3-Jun-20 7:51am    
Understand. I'll try to reconnect after each request.
Thank you.
Haifovchanin 7-Jun-20 7:50am    
Thank you, Richard. The issue is solved.

1 solution

As discussed in the comments, it's better to open the connection, process the new messages, and close the connection each time, rather than trying to keep a single connection open for many hours.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900