Detect Gmail’s 2-Step Authentication
Gmail urges users to use 2-step authentication. Basically this means that Gmail texts users special codes that need to be entered every time they logs-in to theirs account.
This article describes how you can detect 2-step authentication is turned on. This way you can instruct the user to either disable it or to generate and use application specific password for your program.
When 2-steps verification is turned on, Imap.Login method will throw an ServerException with following message:
“[ALERT] Application-specific password required: http://support.google.com/accounts/bin/answer.py?answer=185833 (Failure)”
You can use it to check if application-specific password is needed:
// C# try { imap.UseBestLogin("pat", "password"); } catch (ServerException ex) { bool needs2StepVerification = ex.Message.Contains("Application-specific password required"); imap.Close(); if (needs2StepVerification) Console.WriteLine("Application-specific password required"); retrun; }
' VB.NET Try imap.UseBestLogin("pat", "password") Catch ex As ServerException Dim needs2StepVerification As Boolean = ex.Message.Contains("Application-specific password required") imap.Close() If needs2StepVerification Then Console.WriteLine("Application-specific password required") End If retrun End Try