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 = "smtp.qq.com"
    message = MIMEMultipart("alternative")
    message["Subject"] = subject
    message["From"] = sender_email
    message["To"] = receiver_email
    text = content
    part = MIMEText(text, "plain")
    message.attach(part)
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL("smtp.qq.com", port, context=context) as server:
        server.login(sender_email, password)
        server.sendmail(sender_email, receiver_email, message.as_string())

Call the sendemail function:

sendemail(sender_email='x1@qq.com',receiver_email='x2@qq.com',password='xxxx')

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

notify:
  - name: "send_email"
    platform: smtp
    server: "smtp.qq.com"
    port: 465
    timeout: 15
    sender: "x1@qq.com"
    encryption: starttls
    username: "x1@qq.com"
    password: "xxxx"
    recipient:
      - "x2@qq.com"
    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/smtplib.py", line 398, in getreply
    line = self.file.readline(_MAXLINE + 1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/socket.py", 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/legacy.py", line 93, in async_setup_platform
    notify_service = await hass.async_add_executor_job(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/notify.py", line 100, in get_service
    if mail_service.connection_is_valid():
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/notify.py", line 162, in connection_is_valid
    server = self.connect()
             ^^^^^^^^^^^^^^
  File "/srv/homeassistant/lib/python3.11/site-packages/homeassistant/components/smtp/notify.py", line 148, in connect
    mail = smtplib.SMTP(self._server, self._port, timeout=self._timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
                  ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/smtplib.py", line 343, in connect
    (code, msg) = self.getreply()
                  ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/smtplib.py", 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:

notify:
  - name: "send_email"
    platform: smtp
    server: "smtp.qq.com"
    port: 587
    encryption: starttls

Or:

notify:
  - name: "send_email"
    platform: smtp
    server: "smtp.qq.com"
    port: 465
    encryption: tls