Repository navigation
The COM API and Scripting
This page began as a chapter of the 6.2.10 manual and has been corrected for 6.2.24. The Control Panel's pages are grouped differently now, so the paths below use today's groups; the TLS ports 465, 993 and 995 exist only after you create them on the TCP/IP ports page (a fresh install seeds 25, 587, 110 and 143); and everything added since 6.2.10 is in Changes Since 6.2.10. Where a value here disagrees with the Settings Reference, which is generated from the code, the reference is right.
hMailServer exposes a full COM API. Anything the Control Panel can do, a script can do — it is the same interface.
$app = New-Object -ComObject 'hMailServer.Application'
$app.Authenticate('Administrator', 'your-admin-password') | Out-Null
# Add a domain
$domain = $app.Domains.Add()
$domain.Name = 'example.com'
$domain.Active = $true
$domain.Save()
# Add an account
$account = $domain.Accounts.Add()
$account.Address = 'alice@example.com'
$account.Password = 'a-strong-password'
$account.Active = $true
$account.MaxSize = 1000 # MB, 0 = unlimited
$account.Save()
# Report on every mailbox
foreach ($d in $app.Domains) {
foreach ($a in $d.Accounts) {
'{0,-40} {1,8} MB used' -f $a.Address, [math]::Round($a.Size, 1) # Size is already in MB
}
}If a second factor is enrolled on the administrator credential (
Settings.EnrolAdministratorTOTP, 6.2.27),Authenticaterefuses the administrator; call$app.AuthenticateWithCode('Administrator', $password, $code)instead, and read$app.AdministratorTOTPEnabledbefore authenticating to know whether a code is needed.
Authorization note. In 6.2.10 a real defect was fixed here: fifteen COM methods returned
S_OKwhen they had actually refused the call, because they returnedfalsefrom a function whose return type isHRESULT— andfalseis0, which isS_OK. Five of them returned before writing their out-parameter, so an unauthorized caller got "success" plus uninitialized memory. If you script against the COM API, upgrade to 6.2.10 and make sure your scripts check return values.
Maintenance → Event scripts runs VBScript or JScript at defined points in the mail flow:
| Event | Fires when |
|---|---|
OnClientConnect |
A client connects |
OnHELO |
The SMTP client has sent HELO/EHLO |
OnClientLogon / OnClientValidatePassword
|
A client authenticates / a password is validated |
OnSMTPData |
The SMTP DATA phase begins |
OnRecipientUnknown |
A recipient is not known |
OnAcceptMessage |
A message is accepted |
OnDeliverMessage |
A message is about to be delivered |
OnDeliveryFailed / OnDeliveryStart
|
Delivery outcome |
OnError |
An error is logged |
OnExternalAccountDownload |
Mail is fetched from an external account |
OnBackupCompleted / OnBackupFailed
|
A backup finished / failed |
OnTooManyInvalidCommands |
A client has sent too many invalid commands |

Sub OnAcceptMessage(oClient, oMessage)
If InStr(LCase(oMessage.Subject), "urgent invoice") > 0 Then
oMessage.HeaderValue("X-Suspicious") = "possible-invoice-fraud"
oMessage.Save
End If
End SubEvent scripts run inside the mail flow. A slow script slows every message; a script that throws can stop delivery. Keep them short, and test on a non-production server.
hMailServer 6.3.8, the latest release (2 November 2026, schema 6108) · AGPL-3.0-or-later · Repository · Mirror on GitHub · Report a documentation error (a GitLab issue naming the page)
Home — full index
Start here
1. Install and run
- Before You Install
- Installing hMailServer
- Installing on Linux
- Running in a Container
- Profiles and Environment
- The Control Panel
- The Control Deck
- Your First Domain and Mailbox
- Connecting a Mail Client
- The Webmail
- DNS for Your Domain
- Migration
2. Secure it
- Encryption and Certificates
- Database TLS
- Encryption for People
- Stopping Spam
- Stopping Viruses
- Security Hardening
- Secure Defaults
- The Service Account
- Secrets at Rest
- Sign-in and Identity
- Security Policy
3. Operate it
- Monitoring and Health
- The SNMP Agent
- Backup and Restore
- Message Storage
- Compliance and eDiscovery
- Troubleshooting
- Diagnosing Stalled Mail
- Relocating an Installation
- Upgrading hMailServer
- Upgrading Guide
- The Update Feed
- Migrating the Database Backend
- High Availability Runbook
- Warm Standby
- Runbooks Digest
4. Extend it
- Rules and Sieve
- Aliases Lists and Public Folders
- Rooms and Equipment
- Mailing Lists
- Routes and Relays
- Mail In to a Webhook
- Outbound Shaping
- The COM API and Scripting
- Event Scripts
- The REST API
- Command Line
- APIs Reference
5. Contribute to it
- Project Handbook
- Architecture
- Contributing
- Release Process
- Governance
- Assurance Case
- Regression Test Environment
- Fuzzing
- Regulatory Scope
- Third-Party Binaries
Look it up — from any journey
- Settings Reference
- Settings Overview
- Capabilities and Configuration
- Control Panel Reference
- Control Panel Pages
- Ports Reference
- Changes-Since-6210
- Version-History-to-6210
- Glossary
About this wiki
- hMailServer 6.3.8 is the latest release (2 November 2026, schema 6108)
- Pages say since 6.3.4 where a feature is new in it
- AGPL-3.0-or-later
- Repository, the one place the project is worked on
- Its mirror on GitHub, since 18 October 2026, with no Issues
- Questions: a GitLab issue with the Question template, GitHub Discussions Q&A, or the forum
- Report a documentation error: a GitLab issue naming the page