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! 💪
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.
No setup is required to make calls, send SMS or emails.
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>
- To Send Emails
Go to the following files and make double-click to open:
And add the LSApplicationQueriesSchemes scheme.
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.
.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.
First, let’s know the most important aspects of PhoneDialer.Default:
Now, let’s see a code example:
if (PhoneDialer.Default.IsSupported)
PhoneDialer.Default.Open("809-555-5454");
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.
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" };
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);
}
}
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.
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:
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" })
};
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);
}
}
Windows
Android
iOS / macOS
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:
Use a component library in sync with .NET MAUI’s release cadence. Try Telerik UI for .NET MAUI for free.
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.