You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo开发React Native应用使用EmailJS发邮件遇403错误求助

解决EmailJS在Expo React Native中403严格模式错误的方案

核心问题分析

你遇到的403错误是因为EmailJS账号开启了严格模式,此时前端调用必须传递私钥,且你之前的参数配置存在两个问题:

  1. 使用了浏览器端的@emailjs/browser包,不适用于React Native/Expo环境;
  2. 配置对象的私钥字段名错误(写成了accessToken,正确应为privateKey)。

步骤1:替换为React Native专用包

先卸载浏览器端包,安装适配React Native的版本:

npm uninstall @emailjs/browser
npm install @emailjs/react-native

步骤2:正确传递私钥(前端临时方案)

修改你的发送函数,注意使用正确的配置字段名,同时修复未定义的values变量(改用传入的data参数):

import emailjs from '@emailjs/react-native';

import {
  serviceID,
  templateID,
  publicKey,
  accessToken, // 假设这里的accessToken就是你的EmailJS私钥
} from '../../utils/email-configuration';


export const emailSend = (data) => {
  const templateParams = {
    to_name: data.name,
    to_email: data.email,
    from_name: 'Juan',
    message: data.description,
  };
  console.log('ENVIADOS: ', JSON.stringify(templateParams));

  // 严格模式下的正确配置对象
  const emailConfig = {
    publicKey: publicKey,
    privateKey: accessToken,
  };

  emailjs.send(serviceID, templateID, templateParams, emailConfig)
    .then(response => {
      console.log('SUCCESS!', response.status, response.text);
    })
    .catch(error => {
      console.log('FAILED...', error);
    });
};

也可以全局初始化EmailJS,避免每次发送都传配置:

// 在App.js或项目入口文件中初始化
import emailjs from '@emailjs/react-native';
import { publicKey, accessToken } from './utils/email-configuration';

emailjs.init({
  publicKey: publicKey,
  privateKey: accessToken,
});

之后发送时只需调用:

emailjs.send(serviceID, templateID, templateParams)

重要提醒:前端传私钥的风险

直接在前端代码中暴露私钥会导致安全问题(任何人都能通过反编译或查看源码获取你的私钥,滥用你的EmailJS服务)。严格模式的设计初衷是鼓励后端调用,推荐你搭建一个简单的后端接口处理邮件发送:

后端示例(Node.js + Express)

const express = require('express');
const emailjs = require('@emailjs/nodejs');
const app = express();

app.use(express.json());

// 替换为你的EmailJS配置
const serviceID = '你的服务ID';
const templateID = '你的模板ID';
const publicKey = '你的公钥';
const privateKey = '你的私钥';

emailjs.init({
  publicKey: publicKey,
  privateKey: privateKey,
});

app.post('/send-email', async (req, res) => {
  const { name, email, description } = req.body;
  const templateParams = {
    to_name: name,
    to_email: email,
    from_name: 'Juan',
    message: description,
  };

  try {
    const response = await emailjs.send(serviceID, templateID, templateParams);
    res.status(200).json({ success: true, response });
  } catch (error) {
    res.status(500).json({ success: false, error });
  }
});

app.listen(3000, () => console.log('服务运行在3000端口'));

前端调用后端接口

export const emailSend = async (data) => {
  try {
    const response = await fetch('http://你的服务器IP:3000/send-email', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    });
    const result = await response.json();
    if (result.success) {
      console.log('SUCCESS!', result.response);
    } else {
      console.log('FAILED...', result.error);
    }
  } catch (error) {
    console.log('FAILED...', error);
  }
};

内容的提问来源于stack exchange,提问作者Facundo Maximiliano Vergara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 07:01:18