Problems accessing the document context in an 'after mail has arrived' agent

The following agent fails ‘object variable not set’ when run on a server. I had assumed that the agent would have the current ‘trigger’ document available in the document context. This doesn’t seem to be the case. Am I doing something wrong, or is there a different way to get a handle on the triggering email.

Set session = New NotesSession

Set db = session.currentdatabase

Set currentDoc = session.DocumentContext

Set mailDoc = New NotesDocument( db )

mailDoc.Principal =currentDoc.from(0)

etc…

Subject: Try using notesDb.UnprocessedDocuments NotesDocumentCollection.