Will I get mail details from my next sequence number, when I get "The server has closed the connection-ImapException"?

0 votes
asked Nov 30, 2017 by Deep (120 points)


I refer many articles and they say that this is server side issue !! But, I want to know that while retrieving details of sequence number 1 to 100, I got an error in 55th sequence number. So after moving further, would it also generate an error for retrieving email information for 56th sequence number?

I am using method,

Applies to: Rebex Secure Mail

1 Answer

0 votes
answered Nov 30, 2017 by stepantalalayev (3,940 points)


once the connection is closed, calling any method will cause an exception. The suggested solution is to dispose old client object and create new one and connect again. Then you can continue in your process.

commented Dec 7, 2017 by Deep (120 points)
Thanks for information,

What is difference between unique key and sequence number?  Both are unique identity in email? Consider 1 to 100 unseen email,  if I delete 50th unread email then would it be formatted through 1 to 99?
commented Dec 7, 2017 by stepantalalayev (3,940 points)
Unique key is persisted over sessions (more precisely until folder validity is changed, which is very rare on most servers - check https://tools.ietf.org/html/rfc4549#section-4.1 for more details).

Sequence number is unique for current session only. Moreover it can be changed at any time by the server (e.g. when messages are deleted or so) - this implies, it is better to not use it at all.
commented Feb 20, 2018 by Deep (120 points)
edited Feb 20, 2018 by Deep
That was really helpful.

- I am planning to have IMAP connection alive for 24*7, Is it possible? [Except worst case when connection crashes] Moreover, Is there any limitations for having a connection alive?

-> Currently using [Imap - client.Login],
Please provide alternative if available!!

- As I have hundreds of unseen emails, Do I have to call method every time or there is any method available where I provide bulk of unique keys and get relevant details.
commented Feb 20, 2018 by Lukas Pokorny (109,670 points)
Keeping an IMAP connection alive for long periods of time is possible (unless the server is configured not to allow this). Use Imap object's CheckForUpdates(int duration) method to enter idle state when waiting for updates (this corresponds to IMAP protocol's IDLE command).
commented Feb 20, 2018 by Lukas Pokorny (109,670 points)
To retrieve details about bulk of messages identified by their unique IDs, use Imap object's GetMessageList method. One of its overloads takes an instance of ImapMessageSet object on input, which is basically a set of IDs (or their ranges). To constract the ImapMessageSet, call it's constructor followed by Add method for each unique ID.
commented Feb 20, 2018 by Deep (120 points)
edited Feb 20, 2018 by Deep
I have a bunch of unique keys available for 5 Gmail accounts.
So I will create 5 Imap clients.
Now, I want all of their connection to be open for the infinite time. So whenever, I will get any unique key for the particular client, I will directly call method

- I don't need to recreate connection at that time.

Is there anything available for infinite time span?
commented Feb 20, 2018 by Lukas Pokorny (109,670 points)
I'm afraid I don't understand the question... What exactly does "infinite time span" refer to?
commented Feb 20, 2018 by Deep (120 points)
It means I will just create IMAP client for first time, after that I will use it for years, no need to worry about creating client again!!
commented Feb 20, 2018 by Lukas Pokorny (109,670 points)
Even though using a single instance of Imap object is possible in theory, we recommend using a new Imap object for each IMAP session. A likelihood of a bug making a specific insance of Imap object non-reusable is substantially higher that the likelihood of a bug making it impossible to use a new instance of Imap object.
commented Feb 21, 2018 by Deep (120 points)
I was thinking the same to create the singleton object of each IMAP client. Can u please describe more briefly about the possible chances of getting bugs?
commented Feb 21, 2018 by Deep (120 points)
Talking about another alternative, Is there any way that I have the unique key of IMAP client and I can make that email's status seen to unseen/unread?
commented Feb 21, 2018 by Lukas Pokorny (109,670 points)
- When an instance of Imap object is disconnected and then connected again, the session state is lost. When reconnecting, you are not restarting the failed session, you are creating a new one. The new session behaves the same as if you simply disposed the Imap object and created/connected a new one. This is due to the nature of the IMAP protocol and there is no way around it.

- No software is bug free (and Rebex Secure Mail is not an exception - see https://www.rebex.net/secure-mail.net/history.aspx), so it is a generally a good practice to write code in a way that minimizes the risk of possible unknown bugs affecting its operation. In our view, reusing a single instance of Imap object does not provide any actual benefits, while increasing the risk of failure.

- In practice, unique IDs of emails are persistent and stay the same even if you disconnect and reconnect. Even though an IMAP server is allowed to change unique IDs in a folder, this is very rare, and the server must do so in a way that makes the old IDs invalid.
commented Feb 21, 2018 by Deep (120 points)
As I have shared link of StackOverflow, What do you suggest?
commented Feb 21, 2018 by Lukas Pokorny (109,670 points)
Thanks, I'm afraid I was not quite sure what you wanted to achieve, but it's all clear now - see my reply at StackOverflow. It looks like setting Imap object's Settings.UsePeekForGetMessage to 'true' is what you need.
commented Feb 21, 2018 by Deep (120 points)
Ty so much!! U made my day.

Just last question, after I successfully get the mailing details, How can I update email status to seen?
commented Feb 21, 2018 by Lukas Pokorny (109,670 points)
imap.SetMessageFlags(unique_key, ImapFlagAction.Add, ImapMessageFlags.Seen);
commented Feb 21, 2018 by Deep (120 points)
Ty Lukas for helping me to understand rebex.