Send email in Python

Question:

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.

Asked By: Csaba Faragó

||

Answers:

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.

Answered By: Csaba Faragó
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.