Phase 8 / Ep 38: AgentMail —— 让 Agent 收发邮件

⏱ 预计阅读 5 分钟 更新于 2026/4/13

🎯 学习目标:配置 AgentMail MCP Server 并实现邮件自动化。

1. AgentMail 是什么?

AgentMail 是一个 MCP (Model Context Protocol) Server,为 AI Agent 提供专属的邮箱地址。Agent 可以像人类一样收发邮件——不再需要你手动转发。

graph LR
    Agent["🤖 Agent"] -->|"MCP 协议"| AM["📧 AgentMail\nMCP Server"]
    AM -->|"SMTP/IMAP"| Email["📬 邮件系统"]
    Email --> Inbox["收件箱"]
    Email --> Sent["已发送"]

2. 安装与配置

# 安装 AgentMail
npx clawhub install agentmail

# 配置(需要 AgentMail API Key)
openclaw config set mcp.agentmail.apiKey "am_xxx"
openclaw config set mcp.agentmail.email "[email protected]"

或者在 openclaw.json 中配置:

{
  "mcp": {
    "servers": {
      "agentmail": {
        "command": "npx",
        "args": ["agentmail-mcp-server"],
        "env": {
          "AGENTMAIL_API_KEY": "${AGENTMAIL_API_KEY}"
        }
      }
    }
  }
}

3. 邮件操作能力

安装后,Agent 获得以下工具:

工具 功能 示例
send_email 发送邮件 发送周报到 [email protected]
read_emails 读取收件箱 检查是否有新邮件
reply_email 回复邮件 回复客户咨询
search_emails 搜索邮件 搜索含 "发票" 的邮件

4. 实战场景

场景一:自动发送日报

sequenceDiagram
    actor User as 🧑 你
    participant TG as 📱 Telegram
    participant Agent as 🤖 Agent
    participant AM as 📧 AgentMail
    participant Boss as 📬 [email protected]

    User->>TG: "帮我给老板发今天的工作日报"
    TG->>Agent: 收到指令
    Agent->>Agent: 整理 memory/ 中今天的日记
    Agent->>Agent: 撰写邮件正文
    Agent->>AM: send_email(to: [email protected],\nsubject: "4/6 工作日报",\nbody: "...")
    AM->>Boss: SMTP 发送
    AM->>Agent: ✅ 发送成功
    Agent->>TG: "✅ 日报已发送给 [email protected]"

场景二:读取邮件提取任务

你: "检查一下邮箱,有没有新的任务?"

Agent(内部): read_emails(unread: true)
→ 发现 3 封新邮件

Agent: "你有 3 封新邮件:
1. 📧 Alice (10:30) - 请求审核 PR #42
2. 📧 Bob (11:15) - 询问 API 文档链接
3. 📧 Newsletter (12:00) - OpenClaw v3.1 发布公告

需要我帮你回复哪一封?"

场景三:Cron 自动回复

openclaw cron add "邮件检查" --schedule "*/30 * * * *" \
  --action "检查收件箱,对不含 newsletter 标签的新邮件生成回复草稿,通过 Telegram 通知我审核"

5. 安全控制

{
  "mcp": {
    "agentmail": {
      "restrictions": {
        "allowedRecipients": ["*@company.com", "[email protected]"],
        "maxEmailsPerDay": 50,
        "requireApproval": true
      }
    }
  }
}
安全策略 说明
收件人白名单 只能发送给指定域名
每日上限 防止 Agent 滥发
人工审批 发送前通过 Telegram 请求确认

下节预告: Ep 39,MCP 生态巡礼——除了 AgentMail,还有 Browser、Database、GitHub、Calendar 四大必备 MCP Server。