As Legoscia notes, this happens when a login is performed using a password and is considered risky. The best option is to avoid storing and using user passwords for Gmail and instead switch to Oauth2. Is this application a mail user interface or are you using IMAP as an API? (Maybe https://developers.google.com/gmail/api/ would be more appropriate?)
source share