Telerik blogs

Learn how to enable sending emails and SMS and making calls in your .NET MAUI app.

Among the most used functions in our devices are sending emails and SMS and making calls. That’s why it’s important that we as developers know how to add these features to our apps! The good news is that .NET MAUI already has these functions, and all of them are contained in the Microsoft.Maui.ApplicationModel.Communication namespace.

In this article, we will learn about these three features in an easy and fast way! Let’s start learning! 💪

Settings per Platform

To be sure that these three functionalities are integrated into our app correctly, we must make sure to have the appropriate configurations. That’s why I’m providing you the set of instructions that you must apply for each platform.

Windows logo Windows

No setup is required to make calls, send SMS or emails.

Android logo Android

If the project’s target Android version is set to Android 11 (R API 30) or higher, update the AndroidManifest with queries that use the Android package visibility requirements.

Go to Platforms ➡ Android ➡ AndroidManifest.xml file and inside Manifest nodes add the following:

- To use the Phone Dialer

<queries>
	<intent>
		<action android:name="android.intent.action.DIAL" />
		<data android:scheme="tel"/>
	</intent>
</queries>

- To Send SMS

<queries>
	<intent>
		<action android:name="android.intent.action.VIEW" />
		<data android:scheme="smsto"/>
	</intent>
</queries>

- To Send Emails

<queries>
	<intent>
		<action android:name="android.intent.action.SENDTO" />
		<data android:scheme="mailto" />
	</intent>
</queries>

apple logo iOS / macOS

- To Send Emails

Go to the following files and make double-click to open:

  • Platforms ➡ iOS ➡ Info.plist
  • Platforms ➡ MacCatalyst ➡ Info.plist

And add the LSApplicationQueriesSchemes scheme.

iOS configuration LSApplicationQueriesSchemes

Once added, if you open the info.plist with Visual Studio Code, inside the <dict> tags, you will see a part of code like the following added:

<key>LSApplicationQueriesSchemes</key>
<string></string>

- No setup is required for sending SMS or making calls.

Making Calls From Our Apps

.NET MAUI allows us to open the platform’s telephone dialer to make a call, so that the user only has to press the dial button to complete their call.

All of this is thanks to the IPhoneDialer interface, which is available through the PhoneDialer.Default property.

How To Use Call Functionality

First, let’s know the most important aspects of PhoneDialer.Default:

  • IsSupported: It’s a Bool property which indicates to the user if this device supports this functionality. (Optional.)
  • Open: It’s the method responsible for opening the default call board of your device and it receives as a parameter the phone number that will be displayed on the device, and it’s a string value.

Now, let’s see a code example:

if (PhoneDialer.Default.IsSupported)
PhoneDialer.Default.Open("809-555-5454");

Sending SMS

Also, .NET MAUI allow us to open the default SMS application with preloaded data such as the body message and the sender(s). To do it, .NET MAUI has the ISms interface, which has its implementation available through the Sms.Default property.

How To Use SMS Abilities

Let’s learn how to build it step by step:

1. Check platform support.

Sms.Default.IsComposeSupported: It’s a Bool value property and allows us to identify if the device supports this functionality. (⚠ It’s not required, however it saves you a lot of time if your device won’t be able to run this functionality.)

if (Sms.Default.IsComposeSupported) 
{ 
    // Add all the following code here 
}

2. Create an SmsMessage object.

For the purposes of this example, we need two important factors:

  • The recipient(s): They are the senders to whom you want to send the SMS. Take into account that there can be one or more phone numbers.

string[] recipients = new[] { "809-555-5454" };
  • The message: It’s the body message to be displayed in the SMS.
string text = "Hi everyone, I'm a MAUI SMS!";

3. Now, let’s group this data into a variable using the SmsMessage:

var message = new SmsMessage(text, recipients)

4. Then, let’s use the ComposeAsync method to send the SMS, which receives SmsMessage as parameter (the one we created in the previous step).

await Sms.Default.ComposeAsync(message);

5. Finally, grouping all the lines of code explained above, we will have a result like the following (I added everything inside an async method as it’s required by the ComposeAsync method):

public async void SendSmsTo()
{
	if (Sms.Default.IsComposeSupported)
	{
		string[] recipients = new[] { "809-555-5454" };
		string text = "Hi everyone, I'm a MAUI SMS!";
		var message = new SmsMessage(text, recipients);
		await Sms.Default.ComposeAsync(message);
	}
}

Sending Emails

Guess what? We can also send emails through .NET MAUI, basically in the same way as the previous functionalities. It opens the mail application established by default on our device, allowing us to have a message preloaded with the sender’s data, subject and body of the message. In this way, the user only has to tap on the send button.

To make it possible, we have the IEmail interface that has its implementation available in the Email.Default property.

How To Use Email Functionality

The Email class, which contains the ComposeAsync method, receives EmailMessage as parameter. So, you have to apply the following steps:

1. Check platform support.

Email.Default.IsComposeSupported is a Bool value property that allows us to identify if the device supports this functionality. (⚠ It’s not required, however it saves you a lot of time if your device won’t be able to run this functionality.)

if (Email.Default.IsComposeSupported)
{
	// Add all the following code here
}

2. Create am EmailMessage object.

The EmailMessage object needs four important properties:

  • Subject: The email’s subject to be sent.
  • Body: The email’s body to be sent.
  • BodyFormat: Indicates whether the message is in plane text or in HTML. You can indicate it with the EmailBodyFormat enumerator which contains the PlainText or Html values.
  • To: The email’s recipient(s).

Here, I leave you an example of its implementation:

var message = new EmailMessage
{
	Subject = "Hi how are you?",
	Body = "Thanks for being here, nice to meet you!",
	BodyFormat = EmailBodyFormat.PlainText,
	To = new List<string>(new[] { "marie@outlook.com", "jose@outlook.com" , "peter@outlook.com" })
};

Attach a File 🤔

This part is optional, but if you want to attach any files, use the EmailMessage.Attachment collection. The file type (MIME) is automatically detected, so you don’t need to specify it. You can do it as I show you below:

message.Attachments.Add(new EmailAttachment(Path.Combine(FileSystem.CacheDirectory, "dotnet_bot.svg")));

Then, let’s use the ComposeAsync method to send the email. It receives EmailMessage as parameter (the one we created in the previous step):

await Email.Default.ComposeAsync(message);

Finally, grouping all the lines of code explained above, we will have a result like the following (I added everything inside an async method as it’s required by the ComposeAsync method):

public async void SendEmail()
{
	if (Email.Default.IsComposeSupported)
	{
		var message = new EmailMessage
		{
			Subject = "Hi how are you?",
			Body = "Thanks for being here, nice to meet you!",
			BodyFormat = EmailBodyFormat.PlainText,
			To = new List<string>(new[] { "marie@outlook.com", "jose@outlook.com" , "peter@outlook.com" })
		};
		message.Attachments.Add(new EmailAttachment(Path.Combine(FileSystem.CacheDirectory, "dotnet_bot.svg")))
		await Email.Default.ComposeAsync(message);
		}
}

Platform Limitations

windows logo Windows

  • Only EmailBodyFormat.PlainText is supported.
  • The EmailBodyFormat.HTML throws the FeatureNotSupportedException exception.
  • Not all email clients support sending attachments.

android logo Android

  • Not all email clients for Android support EmailBodyFormat.Html.

apple logo iOS / macOS

  • Both EmailBodyFormat.PlainText and EmailBodyFormat.HTML are supported.
  • For the correct operation of the Email API, you must run your app on a physical device, otherwise it will throw an exception.

Wrapping Up

And done! 💪 From now on you know how to send SMS and emails and how to make calls in your .NET MAUI apps! 💚💕

See you next time! 🙋‍♀️

References:


LeomarisReyes
About the Author

Leomaris Reyes

Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of  Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! 💚💕 You can follow her: Twitter, LinkedIn , AskXammy and Medium.

Related Posts

Comments

Comments are disabled in preview mode.