前端组件:src/app/unitygame.tsx
当我尝试通过到
"https://localhost:3000/api/send-mail"
// src/app/api/send-mail/route.ts
import { NextResponse } from 'next/server';
import nodemailer from 'nodemailer';
export async function POST(req: Request) {
const { recipient, body } = await req.json();
if (!recipient || !body) {
return NextResponse.json({ error: 'Recipient and body are required' }, { status: 400 });
}
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: parseInt(process.env.SMTP_PORT || '587'),
secure: process.env.SMTP_PORT === '465',
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,
},
});
const mailOptions = {
from: process.env.SMTP_USER,
to: recipient,
subject: 'Test Email',
text: body,
};
try {
await transporter.sendMail(mailOptions);
return NextResponse.json({ message: 'Email sent successfully' });
} catch (error) {
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
}
当您尝试从浏览器访问路由时,您会得到
404 Not Found
,因为您没有从
GET
文件导出
route.ts
函数。另外,从应用程序的前端点击API端点时,您需要在请求选项中添加
method: "POST"
。
阅读有关