K
K
Kirill2021-08-18 11:30:58
Python
Kirill, 2021-08-18 11:30:58

Why is mail not sent via python smtplib?

The code:

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart()

from_email = '[email protected]'
to_email = '[email protected]'

login = 'MyLogin'
password = 'MyPassWord'

message = 'Test Message'

msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP('smtp.yandex.ru', 465)
server.starttls()
server.login(login, password)
server.sendmail(from_email, to_email, msg.as_string())
server.quit()


Mistake:
Traceback (most recent call last):
  File "C:\Users\krlls\Desktop\letter.py", line 17, in <module>
    server = smtplib.SMTP('smtp.yandex.ru', 465)
  File "C:\Users\krlls\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\krlls\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 343, in connect
    (code, msg) = self.getreply()
  File "C:\Users\krlls\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 400, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-08-18
@krllssrgv

465 - port for SSL connections. So that

with smtplib.SMTP_SSL('smtp.yandex.ru', 465) as server:

    server.login(login, password)
    server.sendmail(from_email, to_email, msg.as_string())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question