Search code examples
pythonsmtplib

Send email in Python


Now, as the Lesser secure apps feature in Gmail has been disabled, I am trying to find alternatives for email sending. I am trying freemail.hu as an alternative which supports SMTP protocol, but any other suggestion is highly welcome.

According to the web page, the data for SMTP are the following:

  • Server name: smtp.freemail.hu
  • Port: 587 (with STARTTLS)
  • Username: email address
  • Password: the same as used on the web

My code looks like this:

import smtplib
import ssl

try:
    server = smtplib.SMTP('smtp.freemail.hu', 587)
    server.starttls(context=ssl.create_default_context())
    server.login('[myuser]@freemail.hu', '[mypassword]')
    server.sendmail('[myuser]@freemail.hu', ['[myprivatemail]@gmail.com'], 'Test mail.')
except Exception as e:
    print(e)
finally:
    server.quit()

The username is password is correct: I checked them several times + it works on the web interface. However, I am getting the following error message:

(535, b'5.7.8 Error: authentication failed: [encoded value]')

Does anyone has an idea what the problem could be?

I tried two email providers (freemail.hu, mail.com), tried to log in with and without server name, tried to enter the password from command prompt, checked the settings looking for the feature similar to Lesser secure apps in Google, but nothing helped.


Solution

  • For Gmail the App Passwords as described on page https://support.google.com/mail/answer/185833 works well. The 2 step verification should be turned on, and then a 16 character long random password can be generated on the App passwords section.