I just tried using your code on my Yahoo account, and I get the same error, but I also have a secondary email that tells me when unrecognized access happens on that account, in which case I see the below message.
Seems though you may have to also disable security on your account as well as enable POP3.
Someone attempted to sign in to your Yahoo account ({ACCOUNT_NAME}) from an app that doesn't meet Yahoo's security standards. We blocked this sign in attempt, which was made on:
{DATE} from {LOCATION}.
If you were trying to sign in, then please take one of these actions:
Option 1 (recommended): Use mail.yahoo.com or the Yahoo Mail app for
Android and iOS to more securely access your account.
Option 2 (not recommended): If you still want to use an app that uses
less secure sign in to your Yahoo account:
Click here: https://login.yahoo.com/account/security#other-apps
Turn on "Allow apps that use less secure sign in"
Go back to your existing email application and sign in to your Yahoo
account again.
Using apps that don't meet Yahoo's recommended security standards may
leave your account more vulnerable or less secure.