Sample Code

This document provides some samples written in Visual Basic that show how to achieve certain common functions.

Creating a WorkgroupMail Session and listing users
This example, shows how to log on to the WorkgroupMail session and list the WorkgroupMail users

Dim sess As Object
Dim user As Object

Dim users As Object
Dim bLoggedOn As Boolean

Set sess = CreateObject("WMAPI.WMSession")
bLoggedOn = sess.Login("adminpassword")
Set user = sess.GetFirstUser
While not user Is Nothing

MsgBox user.name
Set user = sess.GetNextUser(user)

Wend

Adding a user to WorkgroupMail
This example, shows how to add a new user to WorkgroupMail and assign the user an e-mail address.

Dim sess As Object
Dim user As Object
Dim bLoggedOn As Boolean
Dim settings As Object
Dim domain As Object

Set sess = CreateObject("WMAPI.WMSession")
bLoggedOn = sess.Login("adminpassword")
Set user = sess.AddUser("John Smith")
Set domain = sess.GetFirstDomain
If Not domain Is Nothing Then

Set settings = user.GetDomainSettings(domain)
Settings.Mailbox = "fred.smith"

End If

Assigning a personal POP account at an ISP for a user
This example follows on from the previous example. Once a user has been created, it is configured to collect mail from a personal POP account at the first ISP defined in WorkgroupMail.

Dim ispSettings As Object
Dim isp As Object

Set isp = sess.GetFirstISP
Set ispSettings = user.GetISPSettings(isp)
ispSettings.POP3LoginName = "fred123"
ispSettings.POP3Password = "fred123password"

Listing the messages in the Incoming queue for a user
This example, shows how to list the messages that are stored in the incoming queue for a particular user pending retrieval.

Dim sess As Object
Dim user As Object
Dim bLoggedOn As Boolean
Dim settings As Object
Dim domain As Object

Set sess = CreateObject("WMAPI.WMSession")
bLoggedOn = sess.Login("adminpassword")
Set user = sess.GetUserByID(5)

If Not user Is Nothing Then

Set msgs = user.GetReceivedMessages
Set msg = msgs.GetFirstMessage

While Not msg Is Nothing

MsgBox msg.subject
Set msg = msgs.GetNextMessage(msg)

Wend

End If

Listing the messages in the Incoming queue for all users
This example, shows how to list the messages that are stored in the incoming queue for all users.

Dim sess As Object
Dim user As Object
Dim bLoggedOn As Boolean
Dim settings As Object
Dim domain As Object

Set sess = CreateObject("WMAPI.WMSession")
bLoggedOn = sess.Login("adminpassword")
Set msgs = sess.GetReceivedMessages
Set msg = msgs.GetFirstMessage

While Not msg Is Nothing

MsgBox msg.subject
Set msg = msgs.GetNextMessage(msg)

Wend