iCloud not working with app password

I’m trying to set up an iCloud account but I’m getting an error even when using an app password?

connect <mailcore::IMAPSession:004FF954>
* OK [CAPABILITY XAPPLEPUSHSERVICE IMAP4 IMAP4rev1 SASL-IR AUTH=ATOKEN AUTH=PLAIN AUTH=ATOKEN2] (2412B71-032347a9ddb8) st43p00im-tygg10071001.me.com
ssl connect imap.mail.me.com 993 2
OpenSSL version: OpenSSL 1.1.0f  25 May 2017
connect ok
login
1 LOGIN "email" *********
1 NO [AUTHENTICATIONFAILED] Authentication Failed