3

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

void launchEmailSubmission() async {
    final Uri params = Uri(
      scheme: 'mailto',
      path: 'myOwnEmailAddress@gmail.com',
    );
    String url = params.toString();
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('Could not launch $url');
    }
  }

但现在我想在邮件正文框中给它“默认”主题和提示文本(如果提示文本不可能,则为普通文本)。

有没有办法做到这一点?

4

4 回答 4

10

尝试queryParametersUri. 您可以通过以下方式实现此目的:

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

它将打开默认正文和主题。

于 2020-09-22T05:05:07.680 回答
2

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

void launchEmailSubmission() async {
    final Uri params = Uri(
    scheme: 'mailto',
    path: 'myOwnEmailAddress@gmail.com',
    query: 'subject=Default Subject&body=Default body'
);

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

}

于 2021-04-20T18:12:07.423 回答
1

试试这个!

void _launchURL() async {
    final Uri params = Uri(
      scheme: 'mailto',
      path: 'my.mail@example.com',
    );
    String  url = params.toString();
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print( 'Could not launch $url');
    }
  }
于 2020-09-22T06:01:15.780 回答
0

不要忘记将这些添加到您的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>
于 2021-07-05T14:08:59.347 回答