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

React-Native-Share邮件分享报错:[Error: User did not share]

问题:邮件分享点击发送后报错[Error: User did not share]

在实现邮件分享功能时遇到异常:Gmail表单可正常打开并填充所有数据,但点击发送按钮后控制台报错[Error: User did not share],随后自动返回应用。

原代码

let imagePath = null;

RNFetchBlob.config({
    fileCache: true
})
.fetch("GET", imagesUrls[0])
.then(resp => {
    imagePath = resp.path();
    return resp.readFile("base64");
})
.then(async (base64Data) => {
    var imageUrl = 'data:image/png;base64' + base64Data;

    let shareImage = {
        subject: `Shared Job from Group ${groupID}`,
        email: shareEmail,
        message: `Group ID ${groupID} | License Number: ${data.LicenseNumber}\n 
Model: ${data.Manufacturer} ${data.Model} ${data.ProductionYear}\n
Images:`,
        url: imageUrl,
        // urls: [imageUrl, imageUrl]

    };

    const shareResponse = await Share.open(shareImage)
        .then((res) => {
            console.log(res);
        }).catch((err) => {
            err && console.log(err);
        })

    // return fs.unlink(imagePath)
})

表单截图

Gmail分享表单截图

问题原因与解决方案

原因

react-native-share的url字段传入base64格式的data URI时,Gmail无法正确处理这种格式的附件,导致分享流程中断,触发错误。邮件客户端更倾向于识别本地文件路径作为附件来源。

修改方案

  1. 保留RNFetchBlob下载的本地文件路径,无需转换为base64
  2. 使用files字段替代url,传入本地文件路径数组
  3. 分享完成后清理缓存文件

修改后的代码:

let imagePath = null;

RNFetchBlob.config({
    fileCache: true
})
.fetch("GET", imagesUrls[0])
.then(async resp => {
    imagePath = resp.path();

    let shareImage = {
        subject: `Shared Job from Group ${groupID}`,
        email: shareEmail,
        message: `Group ID ${groupID} | License Number: ${data.LicenseNumber}\n 
Model: ${data.Manufacturer} ${data.Model} ${data.ProductionYear}\n
Images:`,
        files: [imagePath] // 使用本地文件路径作为附件
    };

    try {
        const res = await Share.open(shareImage);
        console.log(res);
    } catch (err) {
        console.log(err);
    } finally {
        // 无论分享成功或失败,都清理缓存文件
        if (imagePath) {
            await RNFetchBlob.fs.unlink(imagePath);
        }
    }
})

说明

  • 直接使用本地文件路径传递附件,符合Gmail等邮件客户端的处理逻辑,避免data URI格式导致的兼容性问题
  • 新增finally块确保缓存文件被清理,防止存储空间被占用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 11:48:21