Converting VBS code to C#

2019-06-21 22:36发布

问题:

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

回答1:

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();


回答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();
}


标签: c# com vbscript