Search code examples
gmailoauth-2.0imapgoogle-apps

Possible to keep a long connection with Gmail through xoauth2 with IMAP?


I am building a Gmail notifier which needs the ability to notify for new emails in realtime. Is it possible to do this with Gmail APIs? FYI, authentication is implemented using Gmail's xoauth2 protocol.


Solution

  • Gmail's IMAP servers support the IDLE command which is the best way to implemnt email notifications because it results in very low traffic and immediate push notifications of new emails. IDLE is documented at:

    https://www.rfc-editor.org/rfc/rfc2177

    note the part that you'll need to end the IDLE session and restart it every 29 minutes (possible Google has set it lower also, you should experiment) in order to prevent the Gmail IMAP server from terminating the connection.

    All of this should work without issue when doing OAUTH 2.0 authentication.