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 and higher). It can be used with Xamarin and ASP.NET Core as well. Tested with .NET Core 1.0/1.1/2.0/2.1/2.2/3.0/3.1 and .NET 5.0/6.0/7.0/8.0.
.NET Core 1.0/1.1 (.NET Standard 1.3) edition mostly has .NET 4.5 feature set (including async/await
methods) with the following limitations:
async/await
versions. You may get PlatformNotSupportedException when calling, let's say, Imap.Connect
instead of Imap.ConnectAsync
(this seems to be an issue with .NET Core 1.0/1.1 only)..NET Core 2.0 (.NET Standard 2.0) edition has much fewer limitations:
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 and newer). Or, via NuGet, the required .DLL will be installed automatically. Just run this command:
Install-Package MailBee.NET
Install System.Text.Encoding.CodePages package (for .NET Core 2.0+):
Install-Package System.Text.Encoding.CodePages
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.
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; } }; } } }
Copyright © 2006-2024 AfterLogic Corporation. All rights reserved.