How do I send an email using Python?

Tell me, did you need to send a message to yandex in python.
I found the code, but an error occurs.
I read it, it is written that the error may crash, since the password may not be in utf-8 encoding.
I write in the code for password, addr_from, addr_to this: encode (encoding= 'utf-8') - zero emotions.
Can you tell me how to fix the error then?

import smtplib  # Импортируем библиотеку по работе с SMTP

# Добавляем необходимые подклассы - MIME-типы
from email.mime.multipart import MIMEMultipart  # Многокомпонентный объект
from email.mime.text import MIMEText  # Текст/HTML

addr_from = "[email protected]"
addr_to = "[email protected]"
password = "pass"  # пароль от почты addr_from

msg = MIMEMultipart()  # Создаем сообщение
msg['From'] = addr_from  # Адресат
msg['To'] = addr_to  # Получатель
msg['Subject'] = 'Тема сообщения'  # Тема сообщения

body = "Текст сообщения"
msg.attach(MIMEText(body, 'plain'))  # Добавляем в сообщение текст

server = smtplib.SMTP_SSL('smtp.yandex.ru', 465)  # Создаем объект SMTP
# server.starttls()             # Начинаем шифрованный обмен по TLS
server.login(addr_from, password)  # Получаем доступ
server.send_message(msg)  # Отправляем сообщение
server.quit()  # Выходим

Mistake:

smtplib.SMTPAuthenticationError: (535, b'5.7.8 Error: authentication failed: This user does not have access rights to this service')
Author: gil9red, 2020-11-09

2 answers

Just had an identical error with the yandex mail acc for the local gitlab. Based on Google, it is usually associated with the use of a feature that is disabled in the yandex account settings.

In my case, it helped to enable the "Portal Password" option.

enter a description of the image here

Option path:

  1. Click on the gear icon in the upper-right corner
  2. In the drop-down menu: "Other"
  3. On the new page, in the list on the left, select " Mail programs "
  4. Check the "Portal password" checkbox
 4
Author: Hi-Angel, 2020-11-11 12:34:06

There was the same problem, here the answer is https://searchengines.guru/ru/forum/1037543.

You need to configure the mailbox:

I tried everything, the solution was not obvious- the setting of the checkboxes in the settings of the mailbox from which the shipment is carried out helped: Mail->all settings->email clients

Allow access to the mailbox using email clients, section imap, I just put all the ticks there, including in the section pop3

enter a description of the image here

 4
Author: BigZmei, 2020-11-09 14:19:13