Question

I have Exchange 2010 and I need to run a process using web services against every new email to come in to a mailbox. The process will basically add the email to an internal task list.

Can I use Powershell for this?

Having never used Powershell before I don't really have a clue on it's capabilities.

If not can anyone suggest another way of doing this other than monitoring the mailbox every X seconds. Really I'd like it event based so if no new mail then no processing.

Cheers, Mike

Was it helpful?

Solution

I have spent a few days looking into this and it seems that it's very difficult to add custom code to an incoming email event. And many of the methods of doing it will just get ignored if Exchange thinks it's slowing down the email system. (http://www.outlookcode.com/article.aspx?id=62)

The other method is to hook into the SMTP events which works but feels at bit of a hack. To do that you need to write a wscript then register that against the event of arrival of an email.

Here is an example vb script for adding a random hex reference onto every email which comes via SMTP.

<SCRIPT LANGUAGE="VBScript">

Sub IEventIsCacheable_IsCacheable()
    'To implement the interface, and return S_OK implicitly
End Sub

Sub ISMTPOnArrival_OnArrival(ByVal Msg, EventStatus)
    Dim Flds

    randomize()

    set Flds = Msg.Fields
    With Flds
        strSubject = .Item("urn:schemas:httpmail:subject")

        if instr(strSubject, "Ref=") = 0 then
            strSubject = Replace(strSubject, CHR(9), "") & " (Ref=" & hex(replace(timer(), ".", "")) & hex(rnd()) & ")"
            .Item("urn:schemas:httpmail:subject") = strSubject
            .Update
        else
            strSubject = LEFT(strSubject, instr(strSubject, "(Ref=") - 1) & MID(strSubject, instr(instr(strsubject, "(Ref="), strSubject, ")") + 1, Len(strSubject)) & " (Ref=" & hex(replace(timer(), ".", "")) & hex(rnd()) & ")"
            .Item("urn:schemas:httpmail:subject") = strSubject
            .Update
        end if
    End With

    Msg.Datasource.Save

    EventStatus = 0 'Run next sink

End Sub

</SCRIPT>

Then to register the script run this.

Cscript smtpreg.vbs /add 1 onarrival SMTPAddRef CDO.SS_SMTPOnArrivalSink "mail from=*"
Cscript smtpreg.vbs /setprop 1 onarrival SMTPAddRef Sink ScriptName "C:\ENTERPATH\SMTPRef.vbs"

To unregister the script run the follow;

cscript smtpreg.vbs /remove 1 onarrival SMTPAddRef

The most resilient method seems to be to create a timer based system to check for new mails every X minutes.

Not as slick as I was hoping for but it'll do.

Hope this helps others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top