.NET Core Edition

MailBee.NET library is also available in the version which is compatible with .NET Standard 1.3 and higher (.NET Core 1.0/1.1) and .NET Standard 2.0 and higher (.NET Core 2.0). It can be used with Xamarin, ASP.NET Core, .NET Core console apps and supersedes Mono edition from previous versions of MailBee.NET Objects. Tested with .NET Core 1.0/1.1/2.0.

.NET Core editions mostly has .NET 4.5 feature set (including async/await methods). Still, a number of limitations do exist (although most of them are no longer a concern for .NET Core 2.0 edition):

OAuth 2.0 for Microsoft is not available. DotNetOpenAuth library from Microsoft was never ported to .NET Core. UWP/UAP is also not supported by Microsoft OAuth 2.0 libraries.

Notes regarding OAuth 2.0 don't directly affect MailBee, in MailBee itself XOAUTH2 mechanism is OK with .NET Core. It's just about Microsoft libraries for generating OAuth 2.0 tokens. If you find another implementation and can create a token, you can still successfully pass it to MailBee and complete OAuth 2.0 process in a .NET Core application.

What you need to run MailBee with .NET Core

The MailBee.NET.dll for .NET Core resides in C:\Program Files (x86)\MailBee.NET Objects\.netstandard1.3 folder (...\.netstandard2.0 for .NET Core 2.0). Or, via NuGet, the required .DLL will be installed automatically. Just run this command:

Install-Package MailBee.NET

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance) must be called before accessing any MailBee classes. This initializes codepages support in .NET Core runtime.

Filesystem access (paths to locations accessible for reading or writing files) is also platform-specific and can depend on your application settings and permissions. If you just need some place where to save the log file for debugging, System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) folder can be used.

Xamarin examples

The below are some samples of using MailBee.NET Objects in Xamarin. They feature using IMAP component in async manner as well as writing into the log file on the device's filesystem. POP3 and SMTP components can be used in the same manner.

For iOS, the code below is MasterViewController.cs from MasterDetail template. For Android, it's MainActivity.cs from Android Application template. The Xamarin for Visual Studio version used to produce these samples was 4.2.

using System;
using System.Drawing;
using System.Collections.Generic;

using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Net.Security;
using System.Security.Authentication;
using System.Threading.Tasks;

using CoreGraphics;
using Foundation;
using UIKit;

using MailBee;
using MailBee.ImapMail;

namespace iOsTest
{
	public partial class MasterViewController : UITableViewController
	{
		DataSource dataSource;

		public MasterViewController (IntPtr handle) : base (handle)
		{
			Title = NSBundle.MainBundle.LocalizedString ("Master", "Master");
			
			if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) {
				PreferredContentSize = new CGSize (320f, 600f);
				ClearsSelectionOnViewWillAppear = false;
			}
			
			// Custom initialization
		}

		public DetailViewController DetailViewController {
			get;
			set;
		}

		async void AddNewItem (object sender, EventArgs args)
		{
			string text;
			try
			{
				// See LicenseKey setting in ViewDidLoad.

				Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
				Imap imp = new Imap ();

				// How to work with filesystem (writing the log file).
				imp.Log.Filename = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "log.txt");
				imp.Log.Enabled = true;
				await imp.Log.ClearAsync();

				// See Application Output window in Xamarin for System.Diagnostics.Debug results.
				await imp.ConnectAsync ("mail.server.com");
				await imp.LoginAsync ("user@domain.com", "secret");
				await imp.SelectFolderAsync("Inbox");
				text = imp.MessageCount.ToString();
				await imp.DisconnectAsync();
			}
			catch (MailBeeException e)
			{
				System.Diagnostics.Debug.Print (e.ToString());
				text = e.GetType ().Name;
			}

			dataSource.Objects.Insert (0, text);
			using (var indexPath = NSIndexPath.FromRowSection (0, 0))
				TableView.InsertRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Automatic);
		}

		public override void DidReceiveMemoryWarning ()
		{
			// Releases the view if it doesn't have a superview.
			base.DidReceiveMemoryWarning ();
			
			// Release any cached data, images, etc that aren't in use.
		}

		public override void ViewDidLoad ()
		{
			base.ViewDidLoad ();

			// Perform any additional setup after loading the view, typically from a nib.
			NavigationItem.LeftBarButtonItem = EditButtonItem;

			var addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add, AddNewItem);
			NavigationItem.RightBarButtonItem = addButton;

			TableView.Source = dataSource = new DataSource (this);

			try
			{
				// See Application Output window in Xamarin for System.Diagnostics.Debug results.
				MailBee.Global.LicenseKey = "MNXXX-XXXXXXXX-XXXX";
			}
			catch (MailBeeException e)
			{
				System.Diagnostics.Debug.Print (e.ToString());
			}
		}

		class DataSource : UITableViewSource
		{
			static readonly NSString CellIdentifier = new NSString ("Cell");
			readonly List<object> objects = new List<object> ();
			readonly MasterViewController controller;

			public DataSource (MasterViewController controller)
			{
				this.controller = controller;
			}

			public IList<object> Objects {
				get { return objects; }
			}

			// Customize the number of sections in the table view.
			public override nint NumberOfSections (UITableView tableView)
			{
				return 1;
			}

			public override nint RowsInSection (UITableView tableview, nint section)
			{
				return objects.Count;
			}

			// Customize the appearance of table view cells.
			public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
			{
				var cell = (UITableViewCell)tableView.DequeueReusableCell (CellIdentifier, indexPath);

				cell.TextLabel.Text = objects [indexPath.Row].ToString ();

				return cell;
			}

			public override bool CanEditRow (UITableView tableView, NSIndexPath indexPath)
			{
				// Return false if you do not want the specified item to be editable.
				return true;
			}

			public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
			{
				if (editingStyle == UITableViewCellEditingStyle.Delete) {
					// Delete the row from the data source.
					objects.RemoveAt (indexPath.Row);
					controller.TableView.DeleteRows (new NSIndexPath[] { indexPath }, UITableViewRowAnimation.Fade);
				} else if (editingStyle == UITableViewCellEditingStyle.Insert) {
					// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
				}
			}

			/*
			// Override to support rearranging the table view.
			public override void MoveRow (UITableView tableView, NSIndexPath sourceIndexPath, NSIndexPath destinationIndexPath)
			{
			}
			*/

			/*
			// Override to support conditional rearranging of the table view.
			public override bool CanMoveRow (UITableView tableView, NSIndexPath indexPath)
			{
				// Return false if you do not want the item to be re-orderable.
				return true;
			}
			*/

			public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
			{
				if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
					controller.DetailViewController.SetDetailItem (objects [indexPath.Row]);
			}
		}

		public override void PrepareForSegue (UIStoryboardSegue segue, NSObject sender)
		{
			if (segue.Identifier == "showDetail") {
				var indexPath = TableView.IndexPathForSelectedRow;
				var item = dataSource.Objects [indexPath.Row];

				((DetailViewController)segue.DestinationViewController).SetDetailItem (item);
			}
		}
	}
}
using System;
using System.IO;
using System.Text;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using MailBee;
using MailBee.ImapMail;

namespace AndroidTest
{
	[Activity (Label = "AndroidTest", MainLauncher = true, Icon = "@drawable/icon")]
	public class MainActivity : Activity
	{
		Imap imp = null;

		protected override void OnCreate (Bundle bundle)
		{
			base.OnCreate (bundle);

			try
			{
				// See Application Output window in Xamarin for System.Diagnostics.Debug results.		
				MailBee.Global.LicenseKey = "MNXXX-XXXXXXXX-XXXX";
			}
			catch (MailBeeException e)
			{
				System.Diagnostics.Debug.Print(e.ToString());
			}

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById<Button> (Resource.Id.myButton);

			button.Click += async delegate {
				try
				{
					Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
					Imap imp = new Imap ();

					// How to work with filesystem (writing the log file).
					imp.Log.Filename = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "log.txt");
					imp.Log.Enabled = true;
					await imp.Log.ClearAsync();

					// See Application Output window in Xamarin for System.Diagnostics.Debug results.
					await imp.ConnectAsync ("mail.server.com");
					await imp.LoginAsync ("user@domain.com", "secret");
					await imp.SelectFolderAsync ("Inbox");
					int msgCount = imp.MessageCount;
					await imp.DisconnectAsync ();
					button.Text = string.Format ("{0} messages!", msgCount);
				}
				catch (MailBeeException e)
				{
					System.Diagnostics.Debug.Print(e.ToString());
					button.Text = e.GetType().Name;
				}
			};
		}
	}
}

Send feedback to AfterLogic

Copyright © 2006-2017 AfterLogic Corporation. All rights reserved.