使用fastapi_mail发送邮箱时,邮箱收到了,但是程序却报错了
这是代码部分
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig, MessageType
from app.core.setting import settings
from typing import List
from pydantic import EmailStr
_conf = ConnectionConfig(
MAIL_USERNAME=settings.mail_username,
MAIL_PASSWORD=settings.mail_password,
MAIL_FROM=settings.mail_from,
MAIL_PORT=settings.mail_port,
MAIL_SERVER=settings.mail_server,
MAIL_STARTTLS=settings.mail_starttls,
MAIL_SSL_TLS=settings.mail_ssl,
)
async def send_email(subject: str, recipients: List[EmailStr], body: str, subtype: MessageType = MessageType.plain):
message = MessageSchema(
subject=subject,
recipients=recipients,
body=body,
subtype=subtype
)
fm = FastMail(_conf)
await fm.send_message(message)
报错如下
File "D:\WebProjects\留言板\后端\app\api\v1\endpoints\verify.py", line 25, in verify_email
await send_email(subject="验证码", recipients=[request.email], body=f"你的验证码是:{verification_code}")
File "D:\WebProjects\留言板\后端\app\services\email.py", line 28, in send_email
await fm.send_message(message)
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\fastapi_mail\fastmail.py", line 151, in send_message
async with Connection(self.config) as session:
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\fastapi_mail\connection.py", line 31, in __aexit__
await self.session.quit()
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\aiosmtplib\smtp.py", line 780, in quit
response = await self.execute_command(b"QUIT", timeout=timeout)
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\aiosmtplib\smtp.py", line 548, in execute_command
response = await self.protocol.execute_command(
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\aiosmtplib\protocol.py", line 291, in execute_command
response = await self.read_response(timeout=timeout)
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\aiosmtplib\protocol.py", line 252, in read_response
result = await asyncio.wait_for(self._response_waiter, timeout)
File "D:\Python310\lib\asyncio\tasks.py", line 447, in wait_for
return fut.result()
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\aiosmtplib\protocol.py", line 162, in data_received
response = self._read_response_from_buffer()
File "D:\WebProjects\留言板\后端\venv\Lib\site-packages\aiosmtplib\protocol.py", line 214, in _read_response_from_buffer
raise SMTPResponseException(
aiosmtplib.errors.SMTPResponseException: (-1, "Malformed SMTP response line: b'\\x00\\x00\\x00\\x1a\\x00\\x00\\x00\\n'")
我直接用aiosmtplib是没问题的
import asyncio
from email.mime.text import MIMEText
from email.header import Header
from app.core.setting import settings
# 收件人和邮件内容
mail_to = "xkgt@qq.com"
subject = "测试邮件 - 来自 Python 脚本(异步)"
body = "这是一个使用 aiosmtplib 发送的测试邮件正文。"
# 创建邮件对象
msg = MIMEText(body, 'plain', 'utf-8')
msg['From'] = settings.mail_from
msg['To'] = mail_to
msg['Subject'] = Header(subject, 'utf-8')
# 异步发送邮件
async def send_email_async():
try:
from aiosmtplib import SMTP
smtp = SMTP(
hostname=settings.mail_server,
port=settings.mail_port,
username=settings.mail_username,
password=settings.mail_password,
use_tls=settings.mail_ssl,
start_tls=settings.mail_starttls,
)
async with smtp:
await smtp.send_message(msg, sender=settings.mail_from, recipients=[mail_to])
print(" 邮件发送成功(异步)!")
except Exception as e:
print(" 邮件发送失败(异步):", str(e))
# 运行异步任务
if __name__ == "__main__":
asyncio.run(send_email_async())
发送成功
但是换fastapi_mail就出错了
import asyncio
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from pydantic import EmailStr
from app.core.setting import settings
# 配置邮件连接
conf = ConnectionConfig(
MAIL_USERNAME=settings.mail_username,
MAIL_PASSWORD=settings.mail_password,
MAIL_FROM=settings.mail_from,
MAIL_PORT=settings.mail_port,
MAIL_SERVER=settings.mail_server,
MAIL_STARTTLS=settings.mail_starttls,
MAIL_SSL_TLS=settings.mail_ssl, # 如果使用 SSL/TLS
)
async def send_email_async():
message = MessageSchema(
subject="测试邮件",
recipients=["xkgt@qq.com"], # 收件人列表
body="这是一个使用 FastAPI-Mail 发送的测试邮件正文。",
subtype="plain"
)
fm = FastMail(conf)
try:
await fm.send_message(message)
print(" 邮件发送成功(FastAPI-Mail)!")
except Exception as e:
print(" 邮件发送失败(FastAPI-Mail):", str(e))
if __name__ == "__main__":
asyncio.run(send_email_async())
邮件发送失败(FastAPI-Mail): (-1, "Malformed SMTP response line: b'\\x00\\x00\\x00\\x1a\\x00\\x00\\x00\\n'")