Home > Uncategorized > Add and List mailboxes set up in #MailEnable

Add and List mailboxes set up in #MailEnable

pOr_pt4o

MailEnable is a popular mail server for windows, that allows you manage your own email accounts on your server, but instead of managing it via remote desktop, they also offer an API to adminsister it. If your email needs are quite simple, but repetitive, then this could be a great way to pass on mail administration from developers to admin staff, by creating a web interface for email management.

My particular need for this was for printfromIpad.com , migrating from Linux (Dovecot) to Windows (MailEnable) – The source code for this article is available on GitHub here; https://github.com/infiniteloopltd/MailEnable/

First, you have to add a reference to MailEnable.Adminstration, which is available here;

C:\Program Files (x86)\Mail Enable\Bin\NETWebAdmin\bin\MailEnable.Administration.dll

(Assuming a default installation path)

The most simple thing is to list all mailboxes in a PostOffice, as follows

private static IEnumerable<string> ListMailboxes(string postOffice)
{
var oMailbox = new Mailbox
{
Postoffice = postOffice
};
if (oMailbox.FindFirstMailbox() != 1)
{
throw new Exception(“Failed to find mailboxes”);
}
var mailboxes = new List<string>();
do
{
var sMailboxName = oMailbox.MailboxName;
mailboxes.Add(sMailboxName);
}
while (oMailbox.FindNextMailbox() == 1);
return mailboxes;
}

This code needs to be run as an administrator, or it hangs unhelpfully on FindFirstMailbox()

Now, it’s more complex code to actually add an account, which would be as follows;

private static void AddMailbox(string postOffice, string email, string password)
{
var user = email.Split(‘@’)[0];
var domain = email.Split(‘@’)[1];
var oMailbox = new MailEnable.Administration.Mailbox();
var oLogin = new Administration.Login();
var sMailboxName = user;
var sPassword = password;
const string sRights = “USER”; // USER – User, ADMIN – Administrator;
oLogin.Account = postOffice;
oLogin.LastAttempt = -1;
oLogin.LastSuccessfulLogin = -1;
oLogin.LoginAttempts = -1;
oLogin.Password = “”;
oLogin.Rights = “”;
oLogin.Status = -1;
oLogin.UserName = sMailboxName + “@” + domain;
// If the login does not exist we need to create it
if (oLogin.GetLogin() == 0)
{
oLogin.Account = postOffice;
oLogin.LastAttempt = 0;
oLogin.LastSuccessfulLogin = 0;
oLogin.LoginAttempts = 0;
oLogin.Password = sPassword;
oLogin.Rights = sRights;
oLogin.Status = 1; // 0 – Disabled, 1 – Enabled
oLogin.UserName = sMailboxName + “@” + domain;
if (oLogin.AddLogin() != 1)
{
// Error adding the Login
throw new Exception(“Failed to add Login”);
}
}
// Now we create the mailbox
oMailbox.Postoffice = postOffice;
oMailbox.MailboxName = sMailboxName;
oMailbox.Size = 0;
oMailbox.Limit = -1; // -1 – Unlimited OR size value (in KB)
oMailbox.Status = 1;
if (oMailbox.AddMailbox() != 1)
// Failed to add mailbox
throw new Exception(“Failed to add mailbox”);
// Now we need to add the Address Map entries for the Account
var oAddressMap = new MailEnable.Administration.AddressMap
{
Account = postOffice,
DestinationAddress = “[SF:” + postOffice + “/” + sMailboxName + “]”,
SourceAddress = “[SMTP:” + sMailboxName + “@” + domain + “]”,
Scope = “0” // ?
};
if (oAddressMap.AddAddressMap() != 1)
// Failed to add Address Map for some reason!
throw new Exception(“Failed to add AddressMap”);

}

For a full reference for this code, refer to the MailEnable .NET reference here;

https://www.mailenable.com/developers/NET%20Reference.pdf

 

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: