Xamarin.Essentials: Email

The Email class enables an application to open the default email application with a specified information including subject, body, and recipients (TO, CC, BCC).

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

Tip

To use the Email API on iOS you must run it on a physical device, else an exception will be thrown.

Using Email

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

The Email functionality works by calling the ComposeAsync method an EmailMessage that contains information about the email:

public class EmailTest
{
    public async Task SendEmail(string subject, string body, List<string> recipients)
    {
        try
        {
            var message = new EmailMessage
            {
                Subject = subject,
                Body = body,
                To = recipients,
                //Cc = ccRecipients,
                //Bcc = bccRecipients
            };
            await Email.ComposeAsync(message);
        }
        catch (FeatureNotSupportedException fbsEx)
        {
            // Email is not supported on this device
        }
        catch (Exception ex)
        {
            // Some other exception occurred
        }
    }
}

File Attachments

Preview feature

Emailing files is available as an experimental preview in Xamarin.Essentials version 1.1.0. This features enables an app to emails files in email clients on the device. To enable this feature set the following property in your app's startup code:

ExperimentalFeatures.Enable(ExperimentalFeatures.EmailAttachments);

After the feature enabled any file can be emailed. Xamarin.Essentials will automatically detect the file type (MIME) and 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);

Platform Differences

Not all email clients for Android support Html, since there is no way to detect this we recommend using PlainText when sending emails.

API