wodImapServer ActiveX Component - First steps with wodImapServer
 

 

We encourage to you read this guide (no matter which environment you use) to understand our approach in making wodImapServer COM object, and to understand necessary steps you need to make to basic IMAP server. If you haven't read what's IMAP all about, please read it also. Once you make IMAP server using following steps, you will easily add more code to determine behavior of your IMAP server.

One of the things you must specially think about while implementing IMAP server is folder and message uniqueness. Before going deeper into your coding - please be prepared for this. There's no way COM object could take care of this, simply because it doesn't have storage capabilities to do so. Each folder when created will have its unique ID that should be the same for its lifetime. Same applies to each message. While message resides in specific folder it should have its unique ID - which cannot be same with any other message. Also, message ID's should be incrementing (not necessary by 1) so newer messages have larger numbers. Keep this in mind!

Below tutorial is made in VB - but we're sure other programmers will easily adjust it for other languages as well. We assume that each user has his own folder where his emails will be stored. In this sample we will use 'joe' as username and '1' as it's password. We will store *ALL* his emails to 'C:\imap4server\joe'

There is a sample in VB\Component\1. Basic that is created based on this guide.

Here we go. To make IMAP server using wodImapServer component, you may try following steps:

 

 

1. Put wodImapServer on VB form

 

Before you can put wodImapServer on your form you must include it in your project. I you plan to use ActiveX, please click on menu Project, Component, find 'WeOnlyDo! COM IMAP4 Server ActiveX Control' on the list and select it. If there's no such item on the list, click on 'Browse' button and find 'wodImapd.ocx' that resides in \WINDOWS\SYSTEM32 folder.

Drag wodImapServer icon and drop it on your form. Select new name for it if you want, although wodImapServer1 is just fine.

If you plan to use COM object instead ( we encourage you to do so ), click on menu Project, References, find 'WeOnlyDo! COM IMAP4 Server Component' and select it. If it's not on the list, click on 'Browse' and find 'wodImapd.dll' that resides in \WINDOWS\SYSTEM32 folder. Add code like this to your form:

 

Dim WithEvents wodImapServer1 As wodImapServerCom

Private Sub Form_Load()
Set wodImapServer1 = New wodImapServerCom
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set wodImapServer1 = Nothing
End Sub
 

 

2. Start your server

Usually, inside Form_Load or on some button click you will want to start your server. Before doing this, select appropriate (free) port (usually 143 for IMAP!) and execute Start method. After this point you will be able to start your IMAP client and try to access IMAPServer on selected port.

 

Private Sub Form_Load()
...
wodImapServer1.Start
...
End Sub
 

 

3.Decide if user can login

Once your IMAP client sends request to wodImapServer containing user's login and password, you will receive Connected event. At this point you should decide if user's provided Username and Password are valid, and set Action property to Allow or Deny.

As noted above - we will accept in this tutorial only user joe with password 1 (one). His emails will be stored in 'C:\imap4server\joe'. If you don't have such folder please create it now!

What could happen now is that client will immediately request for few subfolders (mailboxes) to be created, such as INBOX, Trash, Sent Items, Drafts... Each of them depends on client used, but make sure you do have subfolder INBOX! This is the folder where new emails will be stored, possibly coming from other source (such as local SMTP server).

Little code like this could help:

 

Private Sub wodImapServer1_Connected(ByVal User As WODIMAPSERVERCOMLib.IImapUser, ByVal Username As String, ByVal Password As String, Action As WODIMAPSERVERCOMLib.ImapActions)
  If User.Username = "joe" And Password = "1" Then
    Action = Allow
    User.Tag = "c:\imap4server\joe\"
    ' we will immidiatelly create INBOX folder - just in case
   
On Error Resume Next 'if it already exists
    MkDir User.Tag & "INBOX"
  Else
    Action = Deny
  End If
End Sub
 

WARNING: In Demo version you cannot set Action = Deny, everyone can login.

 

 

4. List folders for user

Usually after user logs in, he will request listing of folders available on your system. Your application should be able to support tree-alike folders hierarchy, each folder could contain subfolders if neccessary.

By default, we don't have any folders so clients will get confused - but not too much! They might immidiatelly create them, so don't worry. Don't add them manually because there's no need to.

At this point we are not worried about folder contents in terms of messages - we're only interested in folders. When we receive ListFolders event, we should populate information about messages. To do so, we will need one DirListBox to be added to the form. We will use it to enumerate folders for the user. Since wodImapServer is apartment threaded - it will always work by using same DirListBox. We could use code like this:

 

 

Private Sub wodImapServer1_ListFolders(ByVal User As WODIMAPSERVERCOMLib.IImapUser, ByVal ParentFolder As WODIMAPSERVERCOMLib.IImapFolder)
  Dim i As Integer
  Dim flds As ImapFolders

  If ParentFolder Is Nothing Then
    Set flds = User.Folders
  Else
    Set flds = ParentFolder.SubFolders
  End If

  Dir1.Path = User.Tag
  Dir1.Refresh
  For i = 1 To Dir1.ListCount - 1
    ' before adding this to the collection, first get only name from Dir1.List(i)
    flds.Add(Dir1.List(i)).Tag = = location & Dir1.List(i) & "\"
  Next i
End Sub

 

 

5. List messages for folder

At the point when client is interested if there's message(s) in specific folder, wodImapServer will fire ListMessages event. You should here check if there are existing messages in that folder, and if you find any add them to collection of messages for the folder.

Later on, on each subsequent ListMessages event, you should check if there are new messages arrived to the folder. If so - add them.

To enumerate messages in the folder, we will add FileListBox to the form, and use it like this:

 

Private Sub wodImapServer1_ListMessages(ByVal User As WODIMAPSERVERCOMLib.IImapUser, ByVal Folder As WODIMAPSERVERCOMLib.IImapFolder)
  Dim i As Integer
  Dim msg As ImapMessage

  File1.Path = Folder.Tag
  File1.Refresh

  For i = 0 To File1.ListCount - 1
    Set msg = Folder.Messages.Add()
    msg.FileName = Folder.Tag & File1.List(i)
  Next i
End Sub
 

That's it - now you can access your server from any client you want without errors.

 

 

5. What next?

We didn't think of UIDs in this tutorial. Check out our samples on how we store UIDs - you can do it the same way. After that, put breakpoints on other events to see what is fired on what client actions - and just implement the code. It's very easy. Our 2. Full sample will help you with that.