XamarinT2 Light_1200x303

Using Phone Dialer or sending emails or SMS in Xamarin Forms might be easier than you thought: They’re included in Xamarin Essentials!

Howdy! 🙋‍♀ It’s a pleasure to have you here! This time I just want to write about Phone Dialer, SMS and Emails. I really like these topics because they allow us to establish direct interaction with the user. Besides this, I have seen people searching for one of these topics and usually find information about individual Plugins to be able to perform all these actions at the same time. But we already have Xamarin Essentials! It’s integrated into our Xamarin Forms projects, so we can now perform these actions (and many others) without having to add any other packages! 😎

This post will be divided in the following topics which are provided by Xamarin Essentials:

  • Phone Dialer
  • Sending SMS
  • Sending Emails

📞 Phone Dialer

The good news is that it’s too easy to implement! With the PhoneDialer class, you can access the Open method that expects a string as a parameter, and then the dialer is opened. Once it does so, the API will automatically try to format the number based on the country code, if specified.

The following simple line of code does all the job!

PhoneDialer.Open(contactNumber);

📲 Sending SMS

Xamarin Essentials provides us the SMS class which allows us to open the default SMS application with a specific message and recipient to be sent.

To send a message, you have to call to the Sms class, followed by the ComposeAsync method which received a parameter SmsMessage type and it’s composed by the following:

await Sms.ComposeAsync
     (
	  new SmsMessage
	  ( 
	     messageText, 
	     new[] { recipient }
	  )
     );

SmsMessage class parameters needed:

Name Type Description
messageText string It’s the message that you will be sending in the SMS.
recipients string Add the recipients to whom you’ll be sending the SMS. (You can pass in multiple recipients with a string array (string[] recipients)).
  

And done! Your SMS our message has been sent! 🎊

📲 Sending Emails

Finally, let’s find out how to send emails!

🔧 But first of all, let’s establish some important differences per platform:

📒 On iOS: Doesn’t have platform differences. However, importantly: To use the Email API on iOS, you must run it on a physical device; otherwise an exception will be thrown.

📗 On Android: It’s recommended to use plain text for sending mail due not all Android users supporting HTML and there is no way to detect it.

📘 On UWP: Only supports plain text as the BodyFormat. Attempting to send HTML will throw a FeatureNotSupportedException. Not all email clients support sending attachments. You can read more information here.

Let’s Continue! 😎

To send the Email, you have to call the ComposeAsync method which received an EmailMessage as a parameter that contains the following information: Subject, Body, To, Cc and Bcc. You can see the implementation in the following code:

await Email.ComposeAsync
      (
          new EmailMessage
          {
               Subject = subject,
               Body = body,
               To = recipients,
               Cc = ccRecipients,
               Bcc = bccRecipients
           }
      );

You also can send attached files. Be aware that every email client is different and may only support specific file extensions, or none at all. You can see an example here about how to do it.

var message = new EmailMessage
{
    Subject = "Hello",
    Body = "World",
};

var fn = "Attachment.txt";
var file = Path.Combine(FileSystem.CacheDirectory, fn);
File.WriteAllText(file, "Hello World");

message.Attachments.Add(new EmailAttachment(file));
 
await Email.ComposeAsync(message);

You have formally sent your first email!

Wrapping Up

Thank you very much for being here, I hope you have found a lot of benefits and you can implement it in your next project! See you soon! 🙋‍♀‍

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.