5

I just have the below code that was provides as hMailServer's DCOM API at http://www.hmailserver.com/documentation/latest/?page=com_example_account_create The below script works fine. It has no reference nothing. Just after installing hMailServer, running the below code can create an account. Now, I need the same thing in C#. They didn't provide me with any library for C# I googled for it but no relevant results all I have is the below code but in according to hMailServer API they said you can convert the below script to any language that you want. But how? I can't even understand how to start to write even the first line. Anybody please help me.

Dim obApp
   Set obApp = CreateObject("hMailServer.Application")

   ' Authenticate. Without doing this, we won't have permission
   ' to change any server settings or add any objects to the
   ' installation.   
   Call obApp.Authenticate("Administrator", "your-main-hmailserver-password")

   ' Locate the domain we want to add the account to
   Dim obDomain
   Set obDomain = obApp.Domains.ItemByName("example.com")

   Dim obAccount
   Set obAccount = obDomain.Accounts.Add

   ' Set the account properties
   obAccount.Address = "account@example.com"
   obAccount.Password = "secret"
   obAccount.Active = True
   obAccount.MaxSize = 100 ' Allow max 100 megabytes

   obAccount.Save
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
Mal
  • 533
  • 1
  • 12
  • 27

2 Answers2

8

Add the COM object (hMailServer) to your C# project as a reference and translate the rest of the code to C#.

It will look something like this:

var app = new hMailServer.Application();

// Authenticate. Without doing this, we won't have permission
// to change any server settings or add any objects to the
// installation.   
app.Authenticate("Administrator", "your-main-hmailserver-password");

// Locate the domain we want to add the account to
var domain = app.Domains["example.com"];

var account = domain.Accounts.Add();

// Set the account properties
account.Address = "account@example.com";
account.Password = "secret";
account.Active = true;
account.MaxSize = 100; // Allow max 100 megabytes

account.Save();
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • I can't find any for such reference file. Can you please show me the download link? – Mal Sep 14 '11 at 16:24
  • No need to download anything. Just add the already existing COM object as a reference. See [here](http://msdn.microsoft.com/en-us/library/wkze6zky(v=VS.100).aspx) for more info on how to add a reference to a C# project. Have a look especially at the topic "To add a reference in Visual C#". – Daniel Hilgarth Sep 14 '11 at 16:26
  • Well. I am getting the following error Error `Non-invocable member 'hMailServer.IInterfaceDomains.ItemByName' cannot be used like a method.` – Mal Sep 14 '11 at 16:31
  • As I am not going to install that COM object, you need to figure this out yourself. Maybe `ItemByName` is an indexer? `app.Domains["example.com"];`. If not, double click the reference to the COM object to open it in the object browser. There you can see what `ItemByName` really is. – Daniel Hilgarth Sep 14 '11 at 16:32
  • Daniel your code works fine in .NET 4.0 but my server does not support 4.0 when I run this code in 2.0 I am getting a error at `var` as `var` is not supported in .NET 2.0 may I use any other type instead of `var` such that is supports in .NET 2.0 also. Please help – Mal Sep 17 '11 at 22:58
  • 1
    `var` is supported since C# 3.0. However, you can specify the types explicitly, just change them to what is required at the lines. First line: `hMailServer.Application app = new hMailServer.Application();`. The other lines I don't know, you need to change `var` to the type of the expression on the right hand side, i.e. what ever `app.Domains["example.com"]` and `domain.Accounts.Add()` return. Possibly: `hMailServer.Domain` and `hMailServer.Account`. – Daniel Hilgarth Sep 19 '11 at 06:14
2

I hope this is still relevant and can help someone. Here I simply used the get item by name property to pull the domain name configured in the hMailServer.

protected void Page_Load(object sender, EventArgs e)
{
    var app = new hMailServer.Application();

    // Authenticate. Without doing this, we won't have permission
    // to change any server settings or add any objects to the
    // installation.   
    app.Authenticate("Administrator", "your.admin.password.here");

    // Locate the domain we want to add the account to
    var domain = app.Domains.get_ItemByName("your.configured.domain.name.here");

    var account = domain.Accounts.Add();

    // Set the account properties
    account.Address = "account.name.here";
    account.Password = "pass.word.here";
    account.Active = true;
    account.MaxSize = 100; // Allow max 100 megabytes

    account.Save();
}