I use mailkit for my ASP.Net Core application and create a SMTPClient object which I then connect to Office365 and authenticate my user with a username and password. How long can this connection be open until it expires or needs to be reauthenticated? Also, is there a way to keep the connection alive without sending an email at the expiration time?
-
Based on my search results, it looks like the default timeout is 2 minutes. You could try to increase the timeout and see whether it helps you to achieve your requirement. Ref: [SmtpClient.Timeout Property](http://www.mimekit.net/docs/html/P_MailKit_Net_Smtp_SmtpClient_Timeout.htm) – Deepak-MSFT May 11 '22 at 05:30
-
Thanks! Do you know if there is a property to renew this so when it gets close to the 2 minutes I can just refresh it. – Irish Redneck May 11 '22 at 15:15
-
@Deepak-MSFT Based on my understanding from a recent search timeout is not how long the connection is open but instead how long the server will wait to receive a response from a send request before calling it quits. I am not sure if this actually keeps the connection open for said time. – Irish Redneck May 11 '22 at 15:22
-
I set the timeout to 30 minutes however after five minutes the connection is terminated regardless and i get error: An existing connection was forcibly closed by the remote host.' – Irish Redneck May 12 '22 at 02:38
-
1You could the [NoOp command](https://csharpdoc.hotexamples.com/class/MailKit.Net.Imap/ImapClient#). It Ping the IMAP server to keep the connection alive. The NOOP command is typically used to keep the connection with the IMAP server alive. When a client goes too long (typically 30 minutes) without sending any commands to the IMAP server, the IMAP server will close the connection with the client, forcing the client to reconnect before it can send any more commands. For more information about the NOOP command, see [rfc3501](https://datatracker.ietf.org/doc/html/rfc3501#section-6.1.2). – Deepak-MSFT May 12 '22 at 05:43
-
I will try this out and see if it solves my problem. I am curious if keeping this connection for the lifetime of the session for a user will cause any problems. – Irish Redneck May 12 '22 at 13:49
-
I would like to confirm whether the suggestion to use the NoOp command worked for you or if the said issue still persists? – Deepak-MSFT May 24 '22 at 07:42
1 Answers
To keep a client connection to a server alive (whether it be using the ImapClient, Pop3Client, or the SmtpClient), you can use the NoOp() or NoOpAsync() methods to send a command to the server that does "nothing" but to let the server know that the client is still there and wants the connection to remain alive. At least in theory this will work.
That said, it's likely not considered good netiquette to keep an SMTP connection alive for any lengthy period of time.
Most SMTP servers are going to expect clients to connect, flush their outbound message queues (by sending each message that is queued up) and then immediately disconnecting and some of the bigger free mail servers may ignore NOOP commands and disconnect the client anyway after some set period of time.
For example, the IMAP specification states that servers should keep a connection alive for at least 30 minutes after the client's last command, but in practice, GMail will only keep that connection alive for less than 10 minutes.
The SMTP specification, as far as I can remember off the top of my head, makes no such suggestion as far as how long to keep the connection alive after the client's last command, so some servers may require more frequent NOOP commands than others and some may even have a max period of time that they'll allow you to keep that connection alive for regardless of whether you send those NOOP commands.
TL;DR:
The NOOP command (via the NoOp() or NoOpAsync() methods) will theoretically keep the connection alive if sent frequently enough, but I would not depend on this working reliably.

- 35,744
- 5
- 97
- 110