NiMail 临时邮箱开放 API 接口文档
第一步:申请邮箱接口
curl 'https://www.nimail.cn/api/applymail' \
-H 'accept: application/json' \
-H 'accept-language: zh-CN' \
-H 'origin: https://www.nimail.cn' \
--data-raw 'mail=8m72837g%40nimail.cn'
可以随机生成一个字母和数字的邮箱,通过参数进行请求
PYTHON请求示例:
import requests
url = 'https://www.nimail.cn/api/applymail'
headers = {
'accept': 'application/json',
'accept-language': 'zh-CN',
'origin': 'https://www.nimail.cn'
}
data = {'mail': '8m72837g%40nimail.cn'}
response = requests.post(url, headers=headers, data=data)
print(f"状态码: {response.status_code}")
print(response.text)
第二步:获取邮件接口
根据邮箱获取临时邮件,可以根据自己的业务邮件来正则匹配,获取对于的数据,比如获取验证码:
curl 'https://www.nimail.cn/api/getmails' \
-H 'accept: application/json' \
-H 'accept-language: zh-CN,zh' \
-H 'origin: https://www.nimail.cn' \
--data-raw 'mail=8m72837g%40nimail.cn&time=0&_=1761398769762'
PYTHON请求示例:
import requests
import time
url = 'https://www.nimail.cn/api/getmails'
headers = {
'accept': 'application/json',
'accept-language': 'zh-CN,zh',
'origin': 'https://www.nimail.cn'
}
data = {
'mail': '8m72837g@nimail.cn',
'time': '0',
'_': str(int(time.time() * 1000))
}
response = requests.post(url, headers=headers, data=data)
print(f"状态码: {response.status_code}")
print(response.text)
🧠 一、临时邮箱是怎么实现的?
1. 📩 邮件接收服务(SMTP)
临时邮箱服务商通常会搭建自己的邮件接收服务器(SMTP Server),配置多个临时域名(如 @nimail.cn)。这些域名被用于接收外部发送过来的邮件。
用户访问网站后,自动生成一个临时邮箱地址。
邮箱服务器监听这个地址的收件箱。
邮件到达后,存入内存或数据库(通常是缓存系统,如 Redis)。
用户在网页前端实时查看邮件内容(WebSocket/Polling 刷新邮件列表)。
2. ⌛ 邮件与地址的生命周期
每个邮箱地址的生命周期是临时的(例如10分钟,可延长)。
到期后,地址和邮件一并被删除。
有的服务支持用户手动清空或延长邮箱时间。
3. 🕸️ 前端实现
页面基于 JavaScript 实时轮询/长连接,展示最新的邮件。
邮件内容常以 HTML 的形式展示,支持验证码或注册链接的点击。
🔐 二、安全性需要注意什么?
临时邮箱虽然方便,但也有不少安全风险需要注意:
✅ 对使用者的风险
风险 描述 🕵️♂️ 信息泄露 邮件通常 不加密 ,任何人知道地址就能看内容。 🗑️ 内容消失 邮件生命周期短,错过保存就无法找回 。 📛 非独占地址 某些服务允许同名地址被重复使用,可能被他人读取邮件 。
📄 示例代码:最小可用临时邮箱 API
from flask import Flask, request, jsonify from uuid import uuid4 import time import threading app = Flask(__name__) # 存储结构:邮箱地址 -> {'created': 时间戳, 'emails': [邮件列表]} mailboxes = {} EXPIRY_SECONDS = 600 # 邮箱10分钟过期 def cleanup_mailboxes(): while True: now = time.time() expired = [addr for addr, box in mailboxes.items() if now - box['created'] > EXPIRY_SECONDS] for addr in expired: del mailboxes[addr] time.sleep(60) @app.route('/create', methods=['POST']) def create_mailbox(): mailbox = f"{uuid4().hex[:8]}@tempmail.dev" mailboxes[mailbox] = {'created': time.time(), 'emails': []} return jsonify({"mailbox": mailbox}) @app.route('/send', methods=['POST']) def send_mail(): data = request.json to = data.get("to") if to in mailboxes: mailboxes[to]['emails'].append({ "from": data.get("from", "anonymous@web"), "subject": data.get("subject", "(No Subject)"), "body": data.get("body", ""), "time": time.time() }) return jsonify({"status": "sent"}) return jsonify({"error": "Mailbox not found"}), 404 @app.route('/inbox/<mailbox>', methods=['GET']) def inbox(mailbox): if mailbox in mailboxes: return jsonify(mailboxes[mailbox]['emails']) return jsonify({"error": "Mailbox not found"}), 404 if __name__ == '__main__': threading.Thread(target=cleanup_mailboxes, daemon=True).start() app.run(debug=True)
🚀 使用示例(Postman 或 curl)
创建邮箱 :
curl -X POST http://127.0.0.1:5000/create
发送邮件到临时邮箱 :
curl -X POST http://127.0.0.1:5000/send -H "Content-Type: application/json" -d '{ "to": "a1b2c3d4@tempmail.dev", "from": "test@site.com", "subject": "Hello", "body": "This is a test message" }'
查看收件箱 :
curl http://127.0.0.1:5000/inbox/a1b2c3d4@tempmail.dev