Mail and SMS in Flutter (original) (raw)

Last Updated : 15 Jul, 2025

The world works in text. From advertisements to conversations, text is everywhere. The most popular modes of official textual communication are Mail followed by SMS. Companies use these modes to communicate not only with their employees but also with their customers. This has led app developers to include mailing and SMS services in their apps. Flutter uses special plugins to bring these features to life and work into mobile apps.

Steps to Implement Mail and SMS in Flutter

Step 1 : Create a New Flutter Application

Create a new Flutter application using the command Prompt. To create a new app, write the below command and run it.

flutter create app_name

To know more about it refer this article: Creating a Simple Application in Flutter

Step 2 : Adding the Dependency

To add the dependency to the **pubspec.yaml file, add **url_launcher as a dependency in the dependencies part of the **pubspec.yaml file__,_ as shown below:

Dart `

dependencies: flutter: sdk: flutter url_launcher: ^6.3.1

`

- Now run the below command in the terminal.

flutter pub get

Step 3 : Importing the Dependency

Use the below line of code in the **main.dart file, to import the **url_launcher dependency :

import 'package:url_launcher/url_launcher.dart';

Step 4 : Code to implement Mail and SMS

**- **Sending a Mail in Flutter

Now, let’s create a function that can be called whenever the user clicks a button that’s linked to a particular Mail ID, to which the user can send a mail.

Dart `

Future _sendingMails() async { var _url = Uri.parse("mailto:feedback@geeksforgeeks.org"); if (!await launchUrl(_url, mode: LaunchMode.externalApplication)) { throw Exception('Could not launch $_url'); } }

`

**- **Sending an SMS in Flutter

Now, let’s create a function that can be called whenever the user clicks a button that’s linked to a phone number, to which the user can send an SMS.

Dart `

Future _sendingSMS() async { var _url = Uri.parse("sms:1234567890"); if (!await launchUrl(_url, mode: LaunchMode.externalApplication)) { throw Exception('Could not launch $_url'); } }

`

**- **Calling the functions

The above functions can be called whenever needed in code, by calling the name of the functions as such. The examples are as follows:

**Complete Source Code

**main.dart:

Dart `

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

// app build process is triggered here void main(){ WidgetsFlutterBinding.ensureInitialized(); runApp(const MyApp()); }

Future _sendingMails() async { var _url = Uri.parse("mailto:feedback@geeksforgeeks.org");

if(!await launchUrl(_url, mode: LaunchMode.externalApplication)) { throw Exception('Could not launch $_url'); } }

Future _sendingSMS() async { var _url = Uri.parse("sms:1234567890");

if (!await launchUrl(_url, mode: LaunchMode.externalApplication)) { throw Exception('Could not launch $_url'); } }

class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);

@override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: const Text('Geeks for Geeks'), backgroundColor: Colors.green, foregroundColor: Colors.white, ), body: SafeArea( child: Center( child: Column( children: [ Container( height: 200.0, ), const Text( 'Welcome to GFG!', style: TextStyle( fontSize: 35.0, color: Colors.green, fontWeight: FontWeight.bold, ), ), Container( height: 20.0, ), const Text( 'For any Queries, Mail us', style: TextStyle( fontSize: 18.0, color: Colors.green, ), ), Container( height: 10.0, ), ElevatedButton( onPressed: _sendingMails, style: ElevatedButton.styleFrom( backgroundColor: Colors.green, foregroundColor: Colors.white ), child: const Text('Here'), ), // ElevatedButton

            Container(
              height: 20.0,
            ),
            const Text(
              'Or Send SMS',
              style: TextStyle(
                fontSize: 18.0,
                color: Colors.green,
              ),
            ),
            Container(
              height: 10.0,
            ),
            ElevatedButton(
              onPressed: _sendingSMS,
              style:ElevatedButton.styleFrom(
                backgroundColor: Colors.green,
                foregroundColor: Colors.white
              ),
              child: const Text('Here'),
            ), // ElevatedButton

          ],
        ),
      ),
    ),
  ),
);

} }

`

To know more about ElevatedButton in flutter refer this article: Flutter – ElevatedButton Widget

**Output: