mcp_email_send
Send emails through SMTP protocol by providing server credentials, recipient information, subject, and message content.
Instructions
发送邮件
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | SMTP服务器主机名 | |
| port | Yes | SMTP服务器端口号 | |
| subject | Yes | 邮件主题 | |
| to | Yes | 收件人邮箱地址 | |
| body | Yes | 邮件正文 | |
| from | Yes | 发件人邮箱地址 | |
| fromPassword | Yes | 发件人邮箱密码 |
Implementation Reference
- src/server.ts:39-53 (handler)The MCP tool handler function for 'mcp_email_send'. It logs the input parameters, instantiates EmailService, calls sendEmail with the parameters, logs the result, and returns a formatted text content response.server.tool('mcp_email_send', "发送邮件", param, async (param) => { log(JSON.stringify(param)) // 调用EmailService发送邮件 const emailService = new EmailService(); const result = await emailService.sendEmail(param); log(JSON.stringify(result)) return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; })
- src/server.ts:30-38 (schema)Zod schema defining the input parameters for the mcp_email_send tool, including validation and descriptions.const param = { host: z.string().describe('SMTP服务器主机名'), port: z.number().int().positive().describe('SMTP服务器端口号'), subject: z.string().describe('邮件主题'), to: z.string().email("无效的邮箱地址").describe('收件人邮箱地址'), body: z.string().min(1, "邮件正文不能为空").describe('邮件正文'), from: z.string().email("无效的邮箱地址").describe('发件人邮箱地址'), fromPassword: z.string().min(1, "发件人邮箱密码不能为空").describe('发件人邮箱密码') }
- src/server.ts:39-53 (registration)Registration of the 'mcp_email_send' tool with the MCP server using server.tool, providing name, description, schema, and handler.server.tool('mcp_email_send', "发送邮件", param, async (param) => { log(JSON.stringify(param)) // 调用EmailService发送邮件 const emailService = new EmailService(); const result = await emailService.sendEmail(param); log(JSON.stringify(result)) return { content: [ { type: "text", text: JSON.stringify(result, null, 2) } ] }; })
- src/tools/emailService.ts:25-67 (helper)The EmailService.sendEmail helper method that implements the actual email sending logic using nodemailer, handling SMTP configuration, sending the email, and returning success/failure status.async sendEmail(param: EmailSendParam): Promise<{success: boolean, message: string}> { try { // 创建邮件传输器 const transporter = nodemailer.createTransport({ host: param.host, port: param.port, secure: true, // 使用SSL auth: { user: param.from, pass: param.fromPassword }, tls: { rejectUnauthorized: false // 允许使用自签名证书 } }); // 邮件选项 const mailOptions = { from: param.from, to: param.to, subject: param.subject, html: param.body }; // 发送邮件 const info = await transporter.sendMail(mailOptions); console.log(`[${new Date().toISOString()}] 邮件发送成功!MessageID: ${info.messageId}`); return { success: true, message: `邮件发送成功,MessageID: ${info.messageId}` }; } catch (error) { console.error(`[${new Date().toISOString()}] 邮件发送失败: ${error instanceof Error ? error.message : String(error)}`); return { success: false, message: `邮件发送失败: ${error instanceof Error ? error.message : String(error)}` }; } }