I encountered the same problem with Gmail since its 'Less Secure Apps' feature was deprecated in 2025. To resolve this, I enabled two-factor authentication (2FA) on my account and generated an app password through my Gmail account. I named the app 'Python Email Script' in Gmail, and the code worked successfully.