Tips on hMailServer Manipulation using C#






4.63/5 (4 votes)
Tips on hMailServer API implementation using C# (code snippets)
Introduction
While working with hMailServer
APIs in my previous project, I ended up creating a class library for the hMailServer
in C#. I thought of sharing it with other developers who might not have the time required to go through tremendous research and stuff.
Hence, this article is more like a package of code snippets and intends to help C# developers while working with hMailServer APIs.
The code snippets will help developers perform the following tasks:
- Authenticate
- Create a Domain
- Create an Account
- Toggle a Domain (Active/Inactive)
- Delete a Domain
- Delete an Account
- Delete All Accounts
- Change Account Password
Background
So what's the hMailServer??
It's basically an e-mail server developed for the Windows Platform by Martin Knafve.
Features
- Open Source: Here is the link for its source code
- Supports IMAP, POP3, and SMTP email protocols
- Easy integration with many web mail systems
- Can use external database engines like MySQL, MS SQL, PostgreSQL
- Flexible spam protection
- Attachable to virus scanners for email scanning (incoming and outgoing)
- Includes administration tools for management and backup
- Features include multi-domain support, aliases, catch-all and basic mailing list
For better details, follow the hMailServer Wiki.
Using the Code
First things first: You got to add the Interop.hMailServer.dll in your project.
The DLL can be found inside the hMailServer/bin folder, wherever your hMailServer
application is installed.
Here is the bundle which includes the Interop.hMailServer.dll.
Authentication
The following code helps authenticate the hMailServer
user and returns an instance of the application.
private hMailServer.Application Authenticate(string userName,string password)
{
hMailServer.Application hMailApp = new hMailServer.Application();
if(hMailApp != null)
hMailApp.Authenticate(userName, password);
return hMailApp;
}
Creating a Domain
Creating a domain is as simple as authenticating and then passing in a meaningful domain name.
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain domain = hMailApp.Domains.Add();
domain.Name = domainName;
domain.Save();
Creating an Account
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain myDomain = hMailApp.Domains.ItemByName[domainName];
if (myDomain != null)
{
hMailServer. Account account = myDomain.Accounts.Add();
account.Address = accountAddress;
account.Password = accountPassword;
account.Active = accountActive;
account.MaxSize = maxSize;
account.Save();
return true;
}
else
return false;
Toggle Domain
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain myDomain = hMailApp.Domains.ItemByName[domainName];
myDomain.Active = activate; // <--- Expects a boolean to activate or deactivate the domain
myDomain.Save();
Delete a Domain
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain myDomain = hMailApp.Domains.ItemByName[domainName];
hMailServer.Accounts hAccounts = myDomain.Accounts;
for (var account = 0; account < hAccounts.Count; account++)
{
try
{
var accountInfo = hAccounts.get_ItemByDBID(account);
myDomain.Accounts.DeleteByDBID(accountInfo.ID);
}
catch(System.Exception)
{}
}
myDomain.Delete();
Delete an Account
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain myDomain = hMailApp.Domains.ItemByName[domainName];
hMailServer.Account account = myDomain.Accounts.ItemByAddress[accountAddress];
myDomain.Accounts.DeleteByDBID(account.ID);
Delete All Accounts
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain myDomain = hMailApp.Domains.ItemByName[domainName];
hMailServer.Accounts hAccounts = myDomain.Accounts;
for (var account = 0; account < hAccounts.Count; account++)
{
var accountInfo = hAccounts.get_ItemByDBID(account);
myDomain.Accounts.DeleteByDBID(accountInfo.ID);
}
Change Account Password
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain myDomain = hMailApp.Domains.ItemByName[domainName];
hMailServer.Account account = myDomain.Accounts.ItemByAddress[accountAddress];
account.Password = newAccountPassword;
myDomain.Save();
I have attached two set of classes which implement the above code and might help developers to speed up the work.
Also, feel free to suggest and give feedback so that I can come up with a better post next time.
Enjoy !!