Get email message in Xamarin

GemBox.Email provides support for .NET Standard 2.0, a cross-platform specification of .NET APIs, which is why it can be used on non-Windows platforms like Linux and macOS, and with the help of Xamarin and Mono, on Android and iOS mobile devices.

With GemBox.Email for .NET Standard you can process your email messages (send, receive, load, save, convert, and edit EML and MSG files) from the native mobile apps.

The following example shows how you can get the first email message from INBOX using ImapClient in Xamarin.Forms application.

Get email message on a native Android mobile app with Xamarin.Forms
Screenshot of Xamarin Android app that gets the first email message from inbox.
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="GemBox.GetMessage.MainPage">

    <StackLayout Padding="50" 
                 Spacing="20" >

        <Label Text="GemBox.Email Example"
               HorizontalOptions="Center"
               FontSize="Large"
               Margin="0,0,0,30"/>

        <Entry Placeholder="Imap host"
               Text="{Binding Host}" />

        <Entry Placeholder="User name"
               Text="{Binding Username}" />

        <Entry Placeholder="Password" 
               IsPassword="True"
               Text="{Binding Password}" />

        <ActivityIndicator x:Name="activity" />

        <Button x:Name="button"
                VerticalOptions="End"
                Text="Get message" 
                Clicked="Button_Clicked" HorizontalOptions="Fill" />

    </StackLayout>
</ContentPage>
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Forms;
using GemBox.Email;
using GemBox.Email.Imap;

public partial class MainPage : ContentPage
{
    private readonly DataModel model;

    public MainPage()
    {
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        InitializeComponent();

        model = new DataModel();
        BindingContext = model;
    }

    private MailMessage GetMessage()
    {
        using (var imap = new ImapClient(model.Host))
        {
            imap.Connect();
            imap.Authenticate(model.Username, model.Password);

            imap.SelectInbox();
            return imap.GetMessage(1);
        }
    }

    private async void Button_Clicked(object sender, EventArgs e)
    {
        button.IsEnabled = false;
        activity.IsRunning = true;

        try
        {
            var message = await Task.Run(() => GetMessage());
            await DisplayAlert("Message",
                "\nSUBJECT: " + message.Subject +
                "\nFROM: " + message.From.ToString() +
                "\nDATE: " + message.Date.ToString(),
                "Close");
        }
        catch (Exception ex)
        {
            await DisplayAlert("Error", ex.Message, "Close");
        }

        activity.IsRunning = false;
        button.IsEnabled = true;
    }
}

public class DataModel
{
    public string Host { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}
Imports System
Imports System.ComponentModel
Imports System.Threading.Tasks
Imports Xamarin.Forms
Imports GemBox.Email
Imports GemBox.Email.Imap

Partial Public Class MainPage
    Inherits ContentPage

    Private ReadOnly model As DataModel

    Public Sub New()
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        InitializeComponent()

        model = New DataModel()
        BindingContext = model
    End Sub


    Private Function GetMessage() As MailMessage
        Using imap As New ImapClient(model.Host)
            imap.Connect()
            imap.Authenticate(model.Username, model.Password)

            imap.SelectInbox()
            Return imap.GetMessage(1)
        End Using
    End Function

    Private Async Sub Button_Clicked(sender As Object, e As EventArgs)
        button.IsEnabled = False
        activity.IsRunning = True

        Try
            Dim message = Await Task.Run(Function() GetMessage())
            Await DisplayAlert("Message",
                "\nSUBJECT: " + message.Subject &
                "\nFROM: " + message.From.ToString() &
                "\nDATE: " + message.Date.ToString(),
                "Close")
        Catch ex As Exception
            Await DisplayAlert("Error", ex.Message, "Close")
        End Try

        activity.IsRunning = False
        button.IsEnabled = True
    End Sub
End Class

Public Class DataModel
    Public Property Host As String
    Public Property Username As String
    Public Property Password As String
End Class

Want more?

GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.


Like it?

Download Buy

If you want to try the GemBox.Email yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.