Solved:How can make homeassistant's notify.send_email work?

With following python code ,i can send email in python3.11 console:

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

def sendemail(subject="title",content="info",sender_email=None,receiver_email=None,password=None):
    port = 465
    smtp_server = ""
    message = MIMEMultipart("alternative")
    message["Subject"] = subject
    message["From"] = sender_email
    message["To"] = receiver_email
    text = content
    part = MIMEText(text, "plain")
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL("", port, context=context) as server:
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message.as_string())

Call the sendemail function:


I can receive an new email in my,now to set my homeassistant’s configuration.yaml:

  - name: "send_email"
    platform: smtp
    server: ""
    port: 465
    timeout: 15
    sender: ""
    encryption: starttls
    username: ""
    password: "xxxx"
      - ""
    sender_name: "send by homeassistant"

Restart my homeassistant,and call the service,some error in log:

cat  /home/homeassistant/.homeassistant/home-assistant.log

2023-08-19 09:38:39.216 ERROR (MainThread) [homeassistant.components.notify] Error setting up platform smtp
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/", line 398, in getreply
    line = self.file.readline(_MAXLINE + 1)
  File "/usr/local/lib/python3.11/", line 705, in readinto
    return self._sock.recv_into(b)
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/notify/", line 93, in async_setup_platform
    notify_service = await hass.async_add_executor_job(
  File "/usr/local/lib/python3.11/concurrent/futures/", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/", line 100, in get_service
    if mail_service.connection_is_valid():
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/", line 162, in connection_is_valid
    server = self.connect()
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/", line 148, in connect
    mail = smtplib.SMTP(self._server, self._port, timeout=self._timeout)
  File "/usr/local/lib/python3.11/", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "/usr/local/lib/python3.11/", line 343, in connect
    (code, msg) = self.getreply()
  File "/usr/local/lib/python3.11/", line 401, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed: "
smtplib.SMTPServerDisconnected: Connection unexpectedly closed: timed out

How can fix it ?

Make port and protocol matched:

  - name: "send_email"
    platform: smtp
    server: ""
    port: 587
    encryption: starttls


  - name: "send_email"
    platform: smtp
    server: ""
    port: 465
    encryption: tls