如何启动和打开电子邮件客户端React-native?

jas*_*san 42 react-native react-native-android react-native-ios

我不想撰写电子邮件.我只是希望能够从反应原生的应用程序在用户的设备(iOS和Android)上启动主电子邮件应用程序.

场景:我会在注册时向用户发送验证电子邮件.

Vis*_*iya 59

React Native Open Mail功能

<Button onPress={() => Linking.openURL('mailto:support@example.com') }
      title="support@example.com" />
Run Code Online (Sandbox Code Playgroud)

使用主体和主体来反应原生开放邮件功能

<Button onPress={() => Linking.openURL('mailto:support@example.com?subject=SendMail&body=Description') }
      title="support@example.com" />
Run Code Online (Sandbox Code Playgroud)

React Native Open URL

<Button onPress={() => Linking.openURL('https://www.google.co.in/') }
      title="www.google.co.in" />
Run Code Online (Sandbox Code Playgroud)

  • **注意:** iOS 模拟器不支持,因此您必须在设备上进行测试。 (23认同)
  • `openURL('mailto:...')`打开一封电子邮件来撰写,而不是电子邮件客户端,jasan明确表示他不想这样做. (10认同)

Dav*_*tte 11

不幸的是,没有一个答案是正确的

我不想撰写电子邮件。我只希望能够启动主电子邮件应用程序

我希望有相同的行为:

  1. 带按钮的登录屏幕 Open Email App
  2. 用户打开他的电子邮件应用程序
  3. 他可以单击魔术链接以返回应用程序

与带有魔术链接的Slack Onboarding大致相同。

在此处输入图片说明

我找到了带有库react-native-email-link的解决方案。您可以从React Native打开电子邮件客户端(用于“魔术链接”类型功能)。

  • 适用于Android。
  • 如果要在iOS上尝试,则需要具有真实的设备,因为mail.appiOS Simulator上没有。

  • 这是一个比线程中其他所有内容都更正确的答案。iOS 上的一个简单解决方案是调用“Linking.openURL('message://')”,这将打开 Mail.app。上面提到的库负责处理跨两个移动操作系统的多个邮件应用程序。 (4认同)

小智 10

Expo.io纯js/typescript解决方案:

import * as IntentLauncher from 'expo-intent-launcher';

// ...

  public openMailClientIOS() {
    Linking.canOpenURL('message:0')
      .then(supported => {
        if (!supported) {
          console.log('Cant handle url')
        } else {
          return Linking.openURL('message:0')
            .catch(this.handleOpenMailClientErrors)
        }
      })
      .catch(this.handleOpenMailClientErrors)
  }

  public openMailClientAndroid() {

    const activityAction = 'android.intent.action.MAIN'; // Intent.ACTION_MAIN
    const intentParams: IntentLauncher.IntentLauncherParams = {
      flags: 268435456, // Intent.FLAG_ACTIVITY_NEW_TASK
      category: 'android.intent.category.APP_EMAIL' // Intent.CATEGORY_APP_EMAIL
    };

    IntentLauncher.startActivityAsync(activityAction, intentParams)
      .catch(this.handleOpenMailClientErrors);
  }
Run Code Online (Sandbox Code Playgroud)

适用于 iOS 邮件,适用于 Android

Android 意图文档:https://developer.android.com/reference/android/content/Intent#ACTION_MAIN

世博会 IntentLauncher 文档:https ://docs.expo.io/versions/latest/sdk/intent-launcher/


tny*_*nyN 9

为此,您可以使用react natives链接模块。这是指向模块https://facebook.github.io/react-native/docs/linking.html的链接。

例: Linking.openURL('mailto:example@gmail.com?subject=example&body=example')

  • 但是我不想发送邮件。我只想启动邮件应用供用户阅读邮件。特别是我刚刚发送的验证电子邮件 (7认同)
  • 请注意,这将在模拟器中引发错误,因为未安装邮件应用程序 (5认同)

Dan*_*iel 7

在 iOS 上打开电子邮件应用程序

 Linking.canOpenURL('message:')
    .then(supported => {
        if (!supported) {
          console.log('Cant handle url')
        } else {
          return Linking.openURL('message:')
        }
      })
      .catch(err => {
        console.error('An error occurred', err)
      })
Run Code Online (Sandbox Code Playgroud)


cba*_*ock -1

我认为以下 npm 模块应该有您正在寻找的内容。不幸的是,它使用本机库,因此您必须运行一些反应本机链接。

https://www.npmjs.com/package/react-native-mail