Click here to Skip to main content
15,884,472 members
Articles / Programming Languages / C#

Tips on hMailServer Manipulation using C#

Rate me:
Please Sign up or sign in to vote.
4.63/5 (4 votes)
22 May 2016CPOL2 min read 28.6K   418   8   13
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.

C#
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.

C#
hMailServer.Application hMailApp = Authenticate(userName,password);
hMailServer.Domain domain = hMailApp.Domains.Add();
domain.Name = domainName;
domain.Save();

Creating an Account

C#
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

C#
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

C#
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

C#
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

C#
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

C#
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 !!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Assurance IQ
Nepal Nepal
I am a Software Engineer and a developer by profession.

The following things describes me:
1. I am a real time/run time developer : Basically learn and implement things on the fly.
2. Definitely not a technology racist : Love to work and explore any technology that i come across.
3. Prefer Beer over H2O : Code with passion and for fun

I am interested in Research & Development based tasks, exploring, experimenting and trying out new things.
Technologies i have been using up until now are C#, ASP.NET, Win Services, Web Services, Restful Web API, Windows Application, Windows Phone Application, Store Apps, couple of JavaScript frameworks, Xamarin Forms, NodeJS, React, ReactNative, AngularJS, SQL Server, MongoDB, Postgres etc.

Comments and Discussions

 
QuestionThanks ! This was really helpfull Pin
davidejoshua14-Oct-21 2:50
davidejoshua14-Oct-21 2:50 
BugChange pasword not work! Pin
Bui Tan Duoc20-May-20 6:35
professionalBui Tan Duoc20-May-20 6:35 
GeneralEmails issue Pin
Member 1481293825-Apr-20 0:30
Member 1481293825-Apr-20 0:30 
QuestionQuery about user login Pin
khans_omer29-May-16 20:31
khans_omer29-May-16 20:31 
AnswerRe: Query about user login Pin
Ozesh Thapa3-Jun-16 8:16
professionalOzesh Thapa3-Jun-16 8:16 
AnswerRe: Query about user login Pin
Mike Marynowski6-May-17 13:36
professionalMike Marynowski6-May-17 13:36 
QuestionQuery about delete all accounts Pin
khans_omer25-May-16 2:25
khans_omer25-May-16 2:25 
AnswerRe: Query about delete all accounts Pin
Ozesh Thapa25-May-16 23:54
professionalOzesh Thapa25-May-16 23:54 
Yes Sir, You are exactly right.
The code will definitely hit an exception in such cases. That is why, we are not doing anything in the try-catch (System.Exception){} block and just skipping over to the next one.

A better solution would have been to first fetch only those accounts present in that particular domain and then performing the delete operation. That would have been easier, but i was unable to get my hands on any APIs to do such stuffs. On top of that, the result returned in hMailServer.Accounts was not of much help.

So until we find a better way,i guess all we can do is

try
{
   //Try Deleting the account
}
catch(System.Exception)
{
    //Do nothing and skip to next
}

SuggestionRe: Query about delete all accounts Pin
khans_omer29-May-16 20:22
khans_omer29-May-16 20:22 
GeneralRe: Query about delete all accounts Pin
Ozesh Thapa3-Jun-16 7:46
professionalOzesh Thapa3-Jun-16 7:46 
AnswerRe: Query about delete all accounts Pin
Holger Boskugel29-Jan-18 0:38
Holger Boskugel29-Jan-18 0:38 
Suggestiontry catch throw Pin
John Brett24-May-16 4:16
John Brett24-May-16 4:16 
GeneralRe: try catch throw Pin
Ozesh Thapa24-May-16 9:10
professionalOzesh Thapa24-May-16 9:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.