【问题标题】:How to get url launcher to send an email?如何让 url 启动器发送电子邮件?
【发布时间】:2020-06-30 19:38:44
【问题描述】:

我希望有人能够单击“联系我”按钮,并希望此按钮能够打开包含我的电子邮件的电子邮件客户端。我目前只是使用他们在包 url_launcher 5.4.11 上给我的虚拟数据。

当我的 url 是“https://flutter.dev”时,这可以正常工作;但当我尝试输入自定义支持的 URL 方案以发送电子邮件时不起作用。

我收到的错误是“未处理的异常:无法启动 smith@example.org?subject=Terra%20Tarot%20Question%20or%20Feedback #0 _launchURL (package:tarotcards/screens/support.dart:11:5) "

我在某处读到无法在 iOS 模拟器上调用邮件应用程序。这是真的吗?

问题在于我的 _launchURL() 方法中的 url。 如果你能帮我解决这个问题,那将不胜感激。非常感谢!

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

_launchURL() async {
  const url = 'mailto:smith@example.org?subject=News&body=New%20plugin';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

class Support extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Image.asset('images/skull.png'),
            Container(
              padding: EdgeInsets.fromLTRB(0, 20.0, 0, 10.0),
              child: Text(
                'Need some help?',
                style: TextStyle(
                    color: Colors.white,
                    fontFamily: 'Opensans',
                    fontSize: 25.0,
                    fontWeight: FontWeight.bold),
              ),
            ),
            Container(
              padding: EdgeInsets.fromLTRB(20.0, 10.0, 20.0, 0.0),
              child: Text(
                'Experiencing bugs? Want to request a feature?'
                'Feel free to get in touch with me.'
                '???? ~Adriana',
                style: TextStyle(
                    color: Colors.white,
                    letterSpacing: 0.5,
                    height: 1.5,
                    fontFamily: 'Opensans',
                    fontSize: 16.0,
                    fontWeight: FontWeight.normal),
              ),
            ),
            SizedBox(
              height: 40.0,
            ),
            ButtonTheme(
              //TODO:// make button #1 work!
              minWidth: 300.0,
              child: RaisedButton(
                child: Text('CONTACT ME'),
                padding: EdgeInsets.all(10.0),
                textColor: Colors.white,
                color: Color(0XFFeb1555),
                onPressed: _launchURL,
              ),
            ),
            SizedBox(
              height: 40.0,
            ),
            ButtonTheme(
              //TODO:// make button #2 work!
              minWidth: 300.0,
              child: RaisedButton(
                child: Text('FREQUENTLY ASKED QUESTIONS'),
                padding: EdgeInsets.all(10.0),
                textColor: Colors.black,
                color: Colors.white,
                onPressed: () {
                  print('second button tapped');
                },
              ),
            ),
            SizedBox(
              height: 150.0,
            ),
            FlatButton(
              child: Text(
                'Terms of Service & Privacy Policy →',
                style: TextStyle(
                  color: Color(0XFFeb1555),
                ),
              ),
              onPressed: () {
                print('privacy policy tapped');
              },
            ),
          ],
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: flutter dart flutter-dependencies


    【解决方案1】:

    我无法在我的机器上为真实设备重现错误!

    你在用模拟器吗?请记住,模拟器无法处理 mailto,因为邮件应用程序不可用。

    试试:

    _launchURL() async {
      final url =
          Uri.encodeFull('mailto:smith@example.org?subject=News&body=New plugin');
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }
    

    您可以尝试这个包,而不是在真实设备上: flutter_email_sender

    【讨论】:

    • 你好。是的,我正在使用模拟器。很高兴知道模拟器不适用于 mailto。我会试试你的解决方案。谢谢你:)
    • 快速提问:如果我有两个不同的按钮和两个不同的 url,我该怎么办?
    • 究竟如何?是否希望每个按钮发送到不同的电子邮件?
    • 是的。正确的。我想要一个按钮来打开电子邮件,另一个按钮来打开网站。非常感谢您的帮助!
    • 我想通了!我刚刚为另一个按钮创建了一个新的方法调用。 _launchSupportWebsite() 异步 { 常量字符串 url = 'flutter.dev'; if (await canLaunch(url)) { 等待启动(url); } else { throw '无法启动 $url'; } }
    【解决方案2】:

    我想为API &gt;= 30 添加以下内容,需要将以下内容添加到您的AndroidManifest.xml 文件中,以便电子邮件发送工作。

    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
        </intent>
    </queries>
    

    source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-23
      • 2016-03-15
      • 1970-01-01
      • 2020-01-19
      • 1970-01-01
      • 2011-05-12
      • 1970-01-01
      相关资源
      最近更新 更多