flutter用url_launcher uri发送电子邮件

问题描述

我正在使用url_launcher在我的应用中发送带有系统电子邮件的电子邮件。我在下面使用代码,这个家伙做得很好。

void launchEmailSubmission() async {
    final Uri params = Uri(
      scheme: 'mailto',path: '[email protected]',);
    String url = params.toString();
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('Could not launch $url');
    }
  }

但是现在我想在邮件正文框中为其指定“认”主题和hintText(如果不可能使用hintText,则为普通文本)。

有什么办法吗?

解决方法

尝试在queryParameters中使用Uri。您可以按照以下显示的方式实现此目标:

void launchEmailSubmission() async {
    final Uri params = Uri(
      scheme: 'mailto',path: '[email protected]',queryParameters: {
        'subject': 'Default Subject','body': 'Default body'
      }
    );
    String url = params.toString();
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('Could not launch $url');
    }
  }

它将打开,将显示默认的正文和主题。

,

正如@tsitixe 指出的那样,您可以使用 Piyushs 答案并更改 queryParameters 来进行这样的查询,以避免电子邮件中的单词之间出现“+”符号:

void launchEmailSubmission() async {
    final Uri params = Uri(
    scheme: 'mailto',query: 'subject=Default Subject&body=Default body'
);

String url = params.toString();
    if (await canLaunch(url)) {
    await launch(url);
} else {
    print('Could not launch $url');
}

}

,

尝试一下!

void _launchURL() async {
    final Uri params = Uri(
      scheme: 'mailto',path: '[email protected]',);
    String  url = params.toString();
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print( 'Could not launch $url');
    }
  }
,

不要忘记在您的 AndroidManifest.xml 中添加这些:

<queries>
  <!-- If your app opens https URLs -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
  <!-- If your app makes calls -->
  <intent>
    <action android:name="android.intent.action.DIAL" />
    <data android:scheme="tel" />
  </intent>
  <!-- If your app emails -->
  <intent>
    <action android:name="android.intent.action.SEND" />
    <data android:mimeType="*/*" />
  </intent>
</queries>