在URL发送器中发送电子邮件时出现问题

时间:2019-09-18 09:49:15

标签: flutter

您好,我尝试启动带有配方的电子邮件页面。我尝试了颤抖的电子邮件发件人,我可以在Android上使用它,但不能在ios上使用。因此,我尝试使用url启动器执行相同的操作,但不能与iOS一起使用。我使用iOS模拟器,可能是这个问题吗?

我使用此网址启动器示例

mailto:xxxxx@xxxxx.com?subject=News&body=New%20plugin

我有这个错误

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: Could not launch mailto:xxxx@xxxx.com?subject=News&body=New%20plugin
#0      _menuscreenState._launchURL (package:xxxx/bottom.dart:8285:7)
<asynchronous suspension>
#1      _menuscreenState.build.<anonymous closure>.<anonymous closure> (package:xxxx/bottom.dart:8705:13)

这是网址启动器的完整示例

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {


  String email="contact@ouiquit.com";
  _launchEmail() async {
    if (await canLaunch("mailto:$email")) {
      await launch("mailto:$email");
    } else {
      throw 'Could not launch';
    }
  }

  @override
  Widget build(BuildContext context) {


    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.red),
      home: Scaffold(
        appBar: AppBar(
          title: Text('test mail'),
          actions: <Widget>[
            IconButton(
              onPressed: _launchEmail,
              icon: Icon(Icons.send),
            )
          ],
        ),


      ),
    );
  }


}

这是错误

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: MissingPluginException(No implementation found for method canLaunch on channel plugins.flutter.io/url_launcher)
#0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:7)
<asynchronous suspension>
#1      canLaunch (package:url_launcher/url_launcher.dart:112:25)
<asynchronous suspension>
#2      _MyAppState._launchEmail (package:testmail/main.dart:20:15)
<asynchronous suspension>
#3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
#4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
#5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
#6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
#7      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
#8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/<…>

8 个答案:

答案 0 :(得分:6)

这对我在Android和iOS设备上均有效:

如果您同时具有querysubject,则添加body参数。

final Uri params = Uri(
  scheme: 'mailto',
  path: 'email@example.com',
  query: 'subject=App Feedback&body=App Version 3.23', //add subject and body here
);

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

答案 1 :(得分:4)

以上给出的解决方案适用于 with open(datetime.today + "Fact about" + typeOfAnimal , "a+", encoding="UTF-8") as file: ,但对于 API < 30,需要将以下内容添加到您的 API >= 30 文件中,以便电子邮件发送工作。

AndroidManifest.xml

source

答案 2 :(得分:1)

这是我使用url_launcher发送邮件的功能:

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');
    }
  }

答案 3 :(得分:1)

尝试mailto插件

https://pub.dev/packages/mailto#-readme-tab-

它可以在设备上的android和ios上运行,效果很好。

void funcOpenMailComposer() async{

    final mailtoLink = Mailto(
      to: ['test@gmail.com'],
      cc: ['test12@gmail.com','test13@gmail.com'],
      subject: '',
      body: '',
    );
  await launch('$mailtoLink');
}

答案 4 :(得分:0)

请尝试一下,我已经用过了,对我来说很好用。

 _launchEmail() async {
      if (await canLaunch("mailto:$email")) {
        await launch("mailto:$email");
      } else {
        throw 'Could not launch';
      }
    }

答案 5 :(得分:0)

对于iOS,我添加了这样的内容

**

_launchEmail() async {
    // ios specification
    final String subject = "Subject:";
    final String stringText = "Same Message:";
    String uri = 'mailto:administrator@gmail.com?subject=${Uri.encodeComponent(subject)}&body=${Uri.encodeComponent(stringText)}';
    if (await canLaunch(uri)) {
      await launch(uri);
    } else {
      print("No email client found");
    }
  }

**

答案 6 :(得分:0)

我有同样的错误,但是重新启动应用程序后它消失了。

答案 7 :(得分:0)

对于所有仍然想知道为什么它没有像我一样起作用的人。请记住,模拟器无法处理mailto,因为邮件应用程序不可用。如果您在调用启动之前使用canLaunch,将会获得更多信息。