Exchange 2003 Add-On Guidance

  • Thread starter Thread starter localhost
  • Start date Start date
L

localhost

I would like to write a Win32 Service that watches Exchang 2003, so
when a user receives an SMTP 553 event the Service can be notified and
take an action on that particular message.

I'm not sure where to start, all ideas appreciated.

Thanks.
 
Hi localhost,

Here is some article about exchange event sink, is that what you want?
SMTP Event Sinks - A window into Exchange integration (Part 1)
http://www.msexchange.org/pages/article.asp?id=602

SMTP Event Sinks - A window into Exchange integration (Part 2)
http://www.msexchange.org/articles/SMTP_Event_Sinks_Part2.html

Writing Managed Sinks for SMTP and Transport Events
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsmtps/htm
l/writingmngsinks.asp

Hope this helps.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I followed the link below, but it appears that the smtpreg.vbs that is
part of the Exchange 2003 SDK is different from the one in the
article. In particular, the smtpreg.vbs program only appears to
handle the OnArrival event, not the OnInboundCommand event in page 4
of the article.


What I want to do is handle mail that is rejected by remote servers.
I either must capture the 553 when it is sent, or the "bad mail"
encapsulating message when Exchange puts it in the user store.

Please tell me where the proper smtpreg.vbs can be found, or what else
I can do to get the sink registered.

Thanks.



Writing Managed Sinks for SMTP and Transport Events
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsmtps/html/writingmngsinks.asp



[snip]
 
I have spent a few more hours on this. This is simply a mess.

The smtpreg.vbs referenced in the article is not in the SDK at all. I
managed to find it buried in MSDN online, so I downloaded that. I am
now using that program.

I followed the docs on building the
Microsoft.Exchange.Transport.EventWrappers.dll, but there is no
strong-name key included in the MS archive download - this produced a
compilation error when I tried to build the sample. So I then created
a new VS.NET 2003 Class Libray project, imported the Microsoft
EventWrappers .cs files by hand, added appropriate assembly
information, added my own strong name key pair (sn -k), and then
referenced the project output in my sample solution.

The documentation does not mention that regasm.exe is required on the
Microsoft.Exchange.Transport.EventWrappers.dll as well as the sample.
Without regasm, smtpreg.vbs cannot find the COM GUIDs assigned to the
..NET class.

With everything finally built and registered, I then did this:

C:\ExchangeSink\1.0>net stop smtpsvc
The Simple Mail Transfer Protocol (SMTP) service is
stopping............
The Simple Mail Transfer Protocol (SMTP) service was stopped
successfully.

C:\ExchangeSink\1.0>cscript smtpreg.vbs /add 1 OnInboundCommand
"9CDB23FC-1742-4da2-B2E8-515435B4066B" SmtpHandler_1_0.SmtpInHandler
ehlo
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Binding Display Name Specified: 9CDB23FC-1742-4da2-B2E8-515435B4066B
Assigning priority (24575 in 32767)
** SUCCESS **
Registered Binding:
Event Name :SMTP Protocol OnInboundCommand
Display Name:9CDB23FC-1742-4da2-B2E8-515435B4066B
Binding GUID:{17867820-D8DB-4787-AA50-766B03001F63}
ProgID :SmtpHandler_1_0.SmtpInHandler
Rule :ehlo
Priority :24575 (0 - 32767, default: 24575)
ComCatID :{F6628C8D-0D5E-11d2-AA68-00C04FA35B82}

C:\ExchangeSink\1.0>net start smtpsvc
The Simple Mail Transfer Protocol (SMTP) service is starting.
The Simple Mail Transfer Protocol (SMTP) service was started
successfully.


My sink code looks like this:

public void OnSmtpInCommand(object currentServer, object
currentSession, MailMsg mailMsg, ISmtpInCommandContext inContext )
{
SmtpInCommandContext mailContext = new SmtpInCommandContext(
inContext );
mailContext.Response = "OOOOOOHHHHHH!" ;
mailContext.CommandStatus =
(uint)ProtocolEventConstants.EXPE_DROP_SESSION;
throw new COMException( "Event consumed." ,
ProtocolEventConstants.EXPE_S_CONSUMED );
}

But telnet'ing to port 25 and entering "EHLO" does absolutely nothing.
Please tell me what I have missed at this point.
 
Hi Primpilus,

Did the sample work on your side?
Based on my research, after you download the sample and install it.
There will be a Microsoft.Exchange.Transport.EventInterop.snk file in the
directory below.
<Program Files>\Exchange SDK\ManagedSinksWP\Interop
Also there will be a Writing Managed Sinks for SMTP and Transport
Events.doc file in the directory below, you may try to follow the steps in
the doc file to build the wrapped assembly.
C:\Program Files\Exchange SDK\ManagedSinksWP

<quote>
To build the interop
1. From a command prompt, run \Program Files\Microsoft Visual Studio
.NET\Common7\Tools\vsvars32.bat.
2. Run nmake.exe from the directory of the interop
(\ManagedSinksWP\Interop).
//this will build a Microsoft.Exchange.Transport.EventInterop.dll in the
C:\Program Files\Exchange SDK\ManagedSinksWP\Interop\obj

To build the wrappers
1. Copy the interop DLL (Microsoft.Exchange.Transport.EventInterop.dll) to
the wrappers directory (\ManagedSinksWP\Wrappers).
2. In the same environment as the interop, run the following from the
wrappers directory:
csc /t:library /out:Microsoft.Exchange.Transport.EventWrappers.dll
/r:Microsoft.Exchange.Transport.EventInterop.dll *.cs /unsafe
</quote>

Writing managed sinks can be somewhat difficult, however, because event
sink interfaces were designed primarily for C++ programmers and are
therefore not very easy to use in a language such as C#. In addition, some
of the methods that are imported using Tlbimp.exe will not work unless they
are modified at the intermediate language (IL) level. However, these
inconsistencies in the imported assemblies have been fixed in the events'
primary interop assembly (PIA) provided, and other interfaces that were
inconvenient to use have also been wrapped so that they are easier to use
from managed code.

So we need to use the nmake command to build the
Microsoft.Exchange.Transport.EventInterop.dll.

You may have a try and let me know the result.

BTW, I have tried the sample which works well on my side by following the
steps in the doc above.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I have built the sample. The sample installs successfully.
My problem is that the example does not "do anything". The file sizes
for the files I compiled are:

81,920 Microsoft.Exchange.Transport.EventInterop.dll
40,960 Microsoft.Exchange.Transport.EventWrappers.dll
171,520 Microsoft.Exchange.Transport.EventWrappers.pdb
32,636 smtpreg.vbs

Below is a re-post of the log showing that while a success is shows
for the install (and I used MetaEdit to double-check all values),
nothing actually happens when an EHLO is sent.


Thanks.




C:\ExchangeSink\1.0>net stop smtpsvc
The Simple Mail Transfer Protocol (SMTP) service is
stopping............
The Simple Mail Transfer Protocol (SMTP) service was stopped
successfully.

C:\ExchangeSink\1.0>cscript smtpreg.vbs /add 1 OnInboundCommand
"9CDB23FC-1742-4da2-B2E8-515435B4066B" SmtpHandler_1_0.SmtpInHandler
ehlo
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

Binding Display Name Specified: 9CDB23FC-1742-4da2-B2E8-515435B4066B
Assigning priority (24575 in 32767)
** SUCCESS **
Registered Binding:
Event Name :SMTP Protocol OnInboundCommand
Display Name:9CDB23FC-1742-4da2-B2E8-515435B4066B
Binding GUID:{17867820-D8DB-4787-AA50-766B03001F63}
ProgID :SmtpHandler_1_0.SmtpInHandler
Rule :ehlo
Priority :24575 (0 - 32767, default: 24575)
ComCatID :{F6628C8D-0D5E-11d2-AA68-00C04FA35B82}

C:\ExchangeSink\1.0>net start smtpsvc
The Simple Mail Transfer Protocol (SMTP) service is starting.
The Simple Mail Transfer Protocol (SMTP) service was started
successfully.


My sink code looks like this:

public void OnSmtpInCommand(object currentServer, object
currentSession, MailMsg mailMsg, ISmtpInCommandContext inContext )
{
SmtpInCommandContext mailContext = new SmtpInCommandContext(
inContext );
mailContext.Response = "OOOOOOHHHHHH!" ;
mailContext.CommandStatus =
(uint)ProtocolEventConstants.EXPE_DROP_SESSION;
throw new COMException( "Event consumed." ,
ProtocolEventConstants.EXPE_S_CONSUMED );
}

But telnet'ing to port 25 and entering "EHLO" does absolutely nothing.
Please tell me what I have missed at this point.









snip
 
Hi primpilus,

I have made a test with your code and it works on my side, the problem is
somewhat strange, I suggest you create a new smtp virtual server to see if
the problem persists.

Also the files ,Microsoft.Exchange.Transport.EventInterop.dll and
Microsoft.Exchange.Transport.EventWrappers.dll is of the same zise as mine.

Here is my test code.
using System;
using System.Runtime.InteropServices;
using Microsoft.Exchange.Transport.EventInterop;
using Microsoft.Exchange.Transport.EventWrappers;

namespace ManagedSinks
{
[Guid("F9732925-B603-4a39-8BEE-1C20E10273C0")]
public class SampleInboundSink : ISmtpInCommandSink
{
public void OnSmtpInCommand(object currentServer, object
currentSession, MailMsg mailMsg, ISmtpInCommandContext inContext )
{
SmtpInCommandContext mailContext = new SmtpInCommandContext(
inContext );
mailContext.Response = "OOOOOOHHHHHH!" ;
mailContext.CommandStatus =
(uint)ProtocolEventConstants.EXPE_DROP_SESSION;
throw new COMException( "Event consumed." ,
ProtocolEventConstants.EXPE_S_CONSUMED );
}
}
}

My test result:

220 SHA-VPHUANG-XP.fareast.corp.microsoft.com Microsoft ESMTP MAIL Service,
Vers
ion: 6.0.2600.1106 ready at Tue, 13 Jul 2004 11:19:04 +0800
ehlo
OOOOOOHHHHHH!

Connection to host lost.

Or you may try to Debugging Managed Sinks steps in the doc file to see if
the OnSmtpInCommand has been called when the event occur.
e.g. you can set a breakpoint at the first line of the OnSmtpInCommand
method.


To isolate the problem, you may try to decare the OnSmtpInCommand as below.

[Guid("F9732925-B603-4a39-8BEE-1C20E10273C0")]
[ComVisible(true)]
public class SampleInboundSink : ISmtpInCommandSink

void ISmtpInCommandSink.OnSmtpInCommand(object server,object
session,MailMsg msg,ISmtpInCommandContext context)



Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top