0 votes

It always takes at least few seconds to create new Imap/Smtp connection using following method.

using (Imap imap = new Imap())
{
    imap.Connect("imap.example.com"); 
    imap.UseBestLogin("user", "password");

//My tasks here

    imap.Close();
}

So i was curious if there is a way to save or preserve this imap connection in session so that i don't have to make a connection each time i want to do something.

I have found that "UseBestLogin" is where it takes couple of seconds or more.
I assume that if we can initialize new Imap(), connect and authenticate using UseBestLogin, and preserve that Imap instance somewhere (session) to re use it without logging in each time we want to access my folders. I have made a few research and have not found any helpful information.

Any clue would be helpful.
Thanks.

by
What do you mean by 'session'?
By session I mean Session in our web browser.
You mean 'web server'?
yes. So that it knows that i am already authenticated and don't need to log in again.

1 Answer

+1 vote

Connect establishes TCP/IP connection to the remote server. UseBestLogin performs authentication against remote server.

In most cases one of those methods performs SSL/TLS negotiation: Connect -or-
ConnectSSL in case of implict-SSL/TLS and UseBestLogin in case of explicit-SSL/TLS.

From technical point of view, you can store Imap or Smtp instance for later use.

However in reality it won't work. Remote server (or even one of the routers) is going to cut you off, if the connection is unused for some time.

You may try issuing 'NO OPeration' command (Noop method) at some interval, let say 10 seconds.

You may also try using Idle: https://www.limilabs.com/blog/imap-idle

Generally if you are creating web mail client consider synchronizing mail in some background thread/service and not directly in asp.net code.

by (301k points)
...