Home > Net >  Can't send an email through url_launcher
Can't send an email through url_launcher

Time:01-04

I'm trying to send an email whenever a user clicks on an individual's e-mail address. However, I'm getting an error. I'm testing through the android emulator. Could it possibly not be working since there isn't a mail app on the emulator?

Here is my error that I receive

PlatformException (PlatformException(ACTIVITY_NOT_FOUND, No Activity found to handle intent { mailto:[email protected]?subject=Default subject&body=Default body }, 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());
  }

Here is how I'm calling it

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

I have the android and ios configured.

CodePudding user response:

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.

CodePudding user response:

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>

CodePudding user response:

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(" ", " ")
      .replaceAll("%20", " ");

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

CodePudding user response:

Please find below resources

Dependency - https://pub.dev/packages/flutter_email_sender

Example - https://maneesha-erandi.medium.com/sending-emails-with-flutter-dd5e2c182fc

  • Related