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

React使用react-share/mailto传递邮件Body失败,求无邮件服务替代方案

解决React项目中mailto传递邮件正文失效的问题

首先,你当前代码的问题出在mailto URL里的换行符——URL中不能存在换行,浏览器解析时会把换行当成无效字符,导致body参数被忽略。修正后的代码如下:

const handleEmailClick = () => {
  const subject = encodeURIComponent('Email Subject');
  const body = encodeURIComponent('Email body content.');
  // 去掉URL中的换行,确保参数连在一起
  window.location.href = `mailto:recipient@example.com?subject=${subject}&body=${body}`;
};

另外,既然你在用react-share,直接用它内置的EmailShareButton和EmailIcon组件会更简洁,也能避免手动拼接URL的错误:

import { EmailShareButton, EmailIcon } from 'react-share';

// 在组件中使用
<EmailShareButton
  url="" // 可选,如需在正文中附带链接可填写
  subject="Email Subject"
  body="Email body content."
>
  <EmailIcon size={32} round />
</EmailShareButton>

额外注意事项

  • 部分邮件客户端(如Outlook桌面版)对mailto的body参数支持有细微差异,若正文需要换行,要用%0D%0A(对应回车+换行)替代普通换行符,示例:
    const body = encodeURIComponent('第一行内容%0D%0A第二行内容');
    
  • 确保encodeURIComponent正确转义特殊字符(如空格、&、@等),避免参数被截断。

内容的提问来源于stack exchange,提问作者jiyo jolly

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 12:01:00