NMapi

About openmapi.org

OpenMapi.org provides a cross-language, multi-platform implementation of the MAPI API.

NMapi - Samples

Grid Sample

The following example demonstrates the use of Smart C# MAPI objects, LINQ and DataBinding to display a simple list of MAPI Task in a Windows.Forms Application.

The object will automatically update if an external program (like Microsoft Outlook ®) makes any changes to any of its properties on the mapi server. The changes will also be automatically immediately visible in the GUI.

Smart Mapi-Object XML Definition

<mapimetal class="MyTask" namespace="Test">
    <property type="Property.Subject" load="prefetch">Subject</property>
    <property type="Property.MessageClass" 
      databind="false" load="prefetch">MessageClass</property>
    <property type="Property.Priority" load="prefetch">Priority</property>
    <property type="Property.Body" load="lazy">Body</property>
</mapimetal>

C# code

//
// openmapi.org - NMapi - Grid.cs
//

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Linq;

using NMapi;
using NMapi.Flags;
using NMapi.Table;
using NMapi.Linq;
using NMapi.Properties;
using NMapi.Properties.Special;

namespace Test {
    class TaskView : Form
    {
        private bool loggedOn;
        private IMapiFactory factory;
        private IMapiSession session;
        private IMapiFolder taskFolder;
        private MapiContext context;

        private TextBox host, user, password, pathBox;
        private Button button;
        private DataGridView grid;

        public TaskView ()
        {
            factory = new AutoConfigurationFactory ();

            Text = "openmapi.org NMapi - Mini Linq/DataBinding Demo";
            Width = Height = 600;

            var layout = new TableLayoutPanel { Width = this.Width, Height = 50,
                ColumnCount = 1, RowCount = 1, Padding = new Padding (1, 1, 1, 1), 
                GrowStyle = TableLayoutPanelGrowStyle.AddColumns, 
                Location = new Point (0, 0), };
            Controls.Add (layout);

            host = new TextBox {Text = "10.0.2.2"};
            user = new TextBox {Text = "jroith"};
            password = new TextBox { PasswordChar = '*'};
            pathBox = new TextBox {Text = "Mailbox - jroith/Tasks", Width = 150};
            button =  new Button { Text = "Ok" };
            layout.Controls.AddRange (new Control [] {host, user, 
                password, pathBox, button});

            grid = new DataGridView {Location = new Point (10, 60), 
                    Width = this.Width-25, Height = 500 };
            Controls.Add (grid);

            button.Click += (sender, args) => FillGrid ();
        }

        public void Logon ()
        {
            try {
                session = factory.CreateMapiSession ();
            } catch (MapiException e) {
                MessageBox.Show ("Error: Can't open Mapi-Session! " + 
                    "Application will now quit.\n\n" + e.Message);
                Application.Exit ();
            }

            try {
                session.Logon  (host.Text, user.Text, password.Text);
            } catch (MapiException e) {
                if (e.HResult == Error.NetworkError)
                    MessageBox.Show ("Host nicht gefunden.");
                else if (e.HResult == Error.NoAccess)
                    MessageBox.Show ("Kein Zugriff. Passwort " + 
                        "und Benutzername korrekt?");
                else
                    throw;
                return;
            }
            loggedOn = true;
        }

        public void FillGrid ()
        {
            if (!loggedOn)
                Logon ();
            if (loggedOn) {
                var store = session.PrivateStore;
                taskFolder = store.HrOpenIPMFolder (pathBox.Text, Mapi.Modify);

                context = new MapiContext (session);
                MapiQuery<MyTask> tasks = context.GetQuery<MyTask> (taskFolder);
    
                var query = from t in tasks
                        where t.MessageClass == "IPM.Task" && t.Subject != "task1"
                        orderby t.Priority descending, t.Subject
                        select t;
    
                grid.DataSource = query;
            }
        }

        protected override void OnClosing (CancelEventArgs ea)
        {
            if (context != null)
                context.Dispose ();
            if (taskFolder != null)
                taskFolder.Dispose ();
            if (session != null)
                session.Dispose ();
            base.OnClosing (ea);
        }

        public static void Main (string[] args)
        {
            Application.Run (new TaskView ());
        }
    }
}

Screenshot