Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting VBS code to C#

Tags:

c#

com

vbscript

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 = "[email protected]"
   obAccount.Password = "secret"
   obAccount.Active = True
   obAccount.MaxSize = 100 ' Allow max 100 megabytes

   obAccount.Save
like image 494
Mal Avatar asked Sep 14 '11 16:09

Mal


2 Answers

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 = "[email protected]";
account.Password = "secret";
account.Active = true;
account.MaxSize = 100; // Allow max 100 megabytes

account.Save();
like image 189
Daniel Hilgarth Avatar answered Oct 09 '22 15:10

Daniel Hilgarth


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();
}
like image 2
Abdul O Tairu Avatar answered Oct 09 '22 13:10

Abdul O Tairu