'Can't send an email through url_launcher

I try to send an email whenever a user clicks on an individual's email address. However, I ran into an error. I am testing through the Android emulator. Doesn't it work since there is no mail app on the emulator?

This is the error I get:

PlatformException (PlatformException(ACTIVITY_NOT_FOUND, No Activity found to handle intent { mailto:[email protected]?subject=Default%20subject&body=Default%20body }, null, null))

Here is the code:

sendEmail(String email) async {
    String? encodeQueryParameters(Map<String, String> params) {
      return params.entries
          .map((e) =>
              '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
          .join('&');
    }

    final Uri emailLaunchUri = Uri(
        scheme: 'mailto',
        path: '$email',
        query: encodeQueryParameters(<String, String>{
          'subject': 'Default subject',
          'body': 'Default body',
        }));

    await launch(emailLaunchUri.toString());
  }

I call it like this:

onTap: () {
   setState(() {
     sendEmail('[email protected]');
   });
},

I have configured Android and IOS.



Solution 1:[1]

For API >=30, you need to add in 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>

You can find more info from Configuration section.

Solution 2:[2]

dependencies: flutter_email_sender: ^5.0.2


Example:

final Email email = Email(
  body: 'Email body',
  subject: 'Email subject',
  recipients: ['[email protected]'],
  cc: ['[email protected]'],
  bcc: ['[email protected]'],
  attachmentPaths: ['/path/to/attachment.zip'],
  isHTML: false,
);

await FlutterEmailSender.send(email);


Setup:

With Android 11, package visibility is introduced that alters the ability to query installed applications and packages on a user’s device. To enable your application to get visibility into the packages you will need to add a list of queries into your AndroidManifest.xml.

<manifest package="com.mycompany.myapp">
  <queries>
    <intent>
      <action android:name="android.intent.action.SENDTO" />
      <data android:scheme="mailto" />
    </intent>
  </queries>
</manifest>

Solution 3:[3]

I use below function for handling email try using it, for sending email through android/ ios using flutter and launch function

launchMail(String toMailId, String subject, String body) async {
  final Uri _emailLaunchUri = Uri(
      scheme: 'mailto', path: toMailId, query: "subject=$subject&body=$body");

  String a = _emailLaunchUri
      .toString()
      .replaceAll("+", "%20")
      .replaceAll("%2520", "%20");

  if (await canLaunch(a)) {
    await launch(a);
  } else {
    throw 'Could not launch $a';
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Tasnuva Tavasum oshin
Solution 3 Vicky Salunkhe