April 10th, 2019
0 reactions

Sharing Files & Email Attachments with Xamarin.Essentials Preview Features

James Montemagno
Principal Manager, Tech PM

In the recent release of Xamarin.Essentials (1.1.0), we introduced several new stable features including detect shake, browser customization, and a plethora of platform helpers. The team has also been working hard on one of the top requested features around files. So to get feedback from developers, we snuck a few preview features into 1.1.0. Including the ability to share a file or add a file as an email attachment! It is extremely easy to get started using these new preview features with just a few lines of code.

Enabling Previews

The Xamarin.Essentials team has adopted one of their favorite features from Xamarin.Forms. The “feature flag” approach that enables developers to try out new features before they are officially released. This is a great addition for developers who want early access because special build installation is not needed. Above all, it allows the team to rapidly work on the API and make feedback-based changes if needed. To enable preview features in Xamarin.Essentials there is a new ExperimentalFeature class with a single method on it, Enable. This method takes in string arguments that represent the feature to enable. Here we can enable both email attachments and sharing files:

ExperimentalFeatures.Enable(ExperimentalFeatures.EmailAttachments, ExperimentalFeatures.ShareFileRequest);

This can be done at any point in the application lifecycle but must be called before using the new APIs.

Email Attachments

This feature enables an app to emails files in email clients on the device. After the feature enabled any file can be emailed. Xamarin.Essentials will automatically detect the file type (MIME), then request the file to be added as an attachment. Every email client is different a may only support specific file extensions or none at all.

Here is a sample of writing text to disk and adding it as an email attachment:

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);

Sharing a File

This feature also enables an app to share files with other applications on the device. Xamarin.Essentials will automatically detect the file type (MIME) while requesting a share. Each platform may only support specific file extensions.

Here is a sample of writing text to disk and sharing it to other apps:

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

await Share.RequestAsync(new ShareFileRequest
{
    Title = Title,
    File = new ShareFile(file)
});

As you can see that the Xamarin.Essentials APIs work really well together for the ability to get file directories, save files to disk, and share them with the world!

Learn More

Be sure to check out all of the great Xamarin.Essentials documentation for full setup guides. Additionally, we have also updated our documentation for both Share and Email with the new preview features. If you have any feedback please open up an issue on the Xamarin.Essentials GitHub repo.

Author

James Montemagno
Principal Manager, Tech PM

James Montemagno is a Principal Lead Program Manager for Developer Community at Microsoft. He has been a .NET developer since 2005, working in a wide range of industries including game development, printer software, and web services. Prior to becoming a Principal Program Manager, James was a professional mobile developer and has now been crafting apps since 2011 with Xamarin. In his spare time, he is most likely cycling around Seattle or guzzling gallons of coffee at a local coffee shop. He ...

More about author

4 comments

Discussion is closed. Login to edit/delete existing comments.

Sort by :
  • Rikudou En Sof

    Thank you James

  • 원진 정

    I Love It!

  • Wong Wei Loon

    Thank you James, that was greatly appreciated. And just a question, can we use this plugin to share images?

  • SERKAN ÇAKMAK

    Thank you James we are proud of you