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

React Native使用react-native-mail-compose时邮件正文为空问题

解决react-native-mail-compose邮件正文为空的问题

我之前也碰到过类似的情况!咱们一步步来排查和解决这个问题:

1. 检查text与html的冲突/优先级

有些邮件客户端(尤其是Android端)对同时设置text和html字段的支持不太友好,可能会忽略其中一个。你可以先做个测试:

  • 暂时注释掉html字段,只保留text,看看正文是否能正常显示
  • 如果需要HTML格式的正文,就只保留html字段,去掉text

2. 修正HTML内容的格式

你的示例里html字段用了转义后的字符(<p>),这会导致邮件客户端把它当成纯文本而不是HTML标签。直接使用未转义的HTML字符串即可:

html: '<p>This is <b>html</b> body</p>'

如果是在JSX或者需要转义的场景里,可以用反引号包裹字符串来避免转义问题。

3. 针对Android端的兼容性处理

官方文档也提到了,部分Android邮件客户端/设备对HTML格式的支持不佳。如果你的用户主要是Android用户,建议优先使用text字段作为正文;如果必须用HTML,可以测试不同的Android邮件客户端(比如Gmail、Outlook)看是否能正常渲染。

4. 尝试升级包版本

有可能是旧版本的react-native-mail-compose存在正文渲染的bug,你可以升级到最新版本再测试:

npm install react-native-mail-compose@latest

修改后的测试代码

这里给你一个简化的测试版本,先确保正文能正常显示:

import MailCompose from 'react-native-mail-compose';

async sendMail() {
  try {
    await MailCompose.send({
      toRecipients: ['to1@example.com', 'to2@example.com'],
      ccRecipients: ['cc1@example.com'],
      bccRecipients: ['bcc1@example.com', 'bcc2@example.com'],
      subject: 'This is subject',
      // 先测试纯文本正文
      text: 'This is the plain text body content',
      // 如果需要HTML,替换成下面的行并注释掉text
      // html: '<p>This is <strong>HTML</strong> formatted body content</p>',
      attachments: [{
        filename: 'mytext',
        ext: '.txt',
        mimeType: 'text/plain',
        text: 'Hello my friend',
      }],
    });
  } catch (e) {
    console.error('邮件发送失败:', e.code, e.message);
  }
}

如果还是不行,建议查看包的社区issue页面,看看有没有其他用户遇到同样的问题,或者提交一个新的issue说明你的设备、客户端和包版本信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:41:23