G
Guest
Hi,
I have a sink class which is registered on the server correctly. I know this
because in my class I put in some ex handling that writes to the event log
and I do get entries. Wat I want to do is get my class to see the content of
the message. Once I get this far I will be able to handle the rest. The COM
exception I am getting in event viewer is:
ExchangeSinks: Unable to cast COM object of type
'Microsoft.Exchange.Transport.EventInterop.MailMsgClass' to class type
'Microsoft.Exchange.Transport.EventWrappers.Message'. Instances of types that
represent COM components cannot be cast to types that do not represent COM
components; however they can be cast to interfaces as long as the underlying
COM component supports QueryInterface calls for the IID of the interface.
Class Code is:
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Microsoft.Exchange.Transport.EventInterop
Imports Microsoft.Exchange.Transport.EventWrappers
Namespace SampleManagedSink
<Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")> Public Class
ExchSinkClass
Implements IMailTransportSubmission, IEventIsCacheable
Public Sub IsCacheable() _
Implements IEventIsCacheable.IsCacheable
End Sub
Public Sub OnMessageSubmission(ByVal pIMailMsg As
Microsoft.Exchange.Transport.EventInterop.MailMsg, ByVal pINotify As
Microsoft.Exchange.Transport.EventInterop.IMailTransportNotify, ByVal
pvNotifyContext As System.IntPtr) _
Implements IMailTransportSubmission.OnMessageSubmission
Try
Dim buffer As Byte() = CopyContentToStream(pIMailMsg)
Dim oFileStream As System.IO.FileStream
Dim nw As DateTime = Now
oFileStream = New System.IO.FileStream("C:\Logs\" &
nw.ToShortTimeString & " email.txt", System.IO.FileMode.OpenOrCreate)
oFileStream.Write(buffer, 0, buffer.Length - 1)
oFileStream.Close()
Catch cex As COMException
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = cex.Source & ": " & cex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
Catch ex As Exception
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = ex.Source & ": " & ex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
End Try
End Sub
Function CopyContentToStream(ByVal msg As Message)
Dim buffer As Byte()
Dim Offset As Integer = 0
Try
Const BYTES_TO_READ As Integer = 2048
buffer = msg.ReadContent(Offset, BYTES_TO_READ)
Catch cex As COMException
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = "In Function, " & cex.Source & ": " &
cex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
Catch ex As Exception
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = "In Function, " & ex.Source & ": " &
ex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
End Try
If buffer.Length > 0 Then
Return buffer
Else
Return Nothing
End If
End Function
End Class
End Namespace
if I edit this code to simply create an entry in event log every time the
sink class is run, it does and I get no errors.
I don't know how to pass the same interface to my function. Please help.
I have a sink class which is registered on the server correctly. I know this
because in my class I put in some ex handling that writes to the event log
and I do get entries. Wat I want to do is get my class to see the content of
the message. Once I get this far I will be able to handle the rest. The COM
exception I am getting in event viewer is:
ExchangeSinks: Unable to cast COM object of type
'Microsoft.Exchange.Transport.EventInterop.MailMsgClass' to class type
'Microsoft.Exchange.Transport.EventWrappers.Message'. Instances of types that
represent COM components cannot be cast to types that do not represent COM
components; however they can be cast to interfaces as long as the underlying
COM component supports QueryInterface calls for the IID of the interface.
Class Code is:
Imports System
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports Microsoft.Exchange.Transport.EventInterop
Imports Microsoft.Exchange.Transport.EventWrappers
Namespace SampleManagedSink
<Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")> Public Class
ExchSinkClass
Implements IMailTransportSubmission, IEventIsCacheable
Public Sub IsCacheable() _
Implements IEventIsCacheable.IsCacheable
End Sub
Public Sub OnMessageSubmission(ByVal pIMailMsg As
Microsoft.Exchange.Transport.EventInterop.MailMsg, ByVal pINotify As
Microsoft.Exchange.Transport.EventInterop.IMailTransportNotify, ByVal
pvNotifyContext As System.IntPtr) _
Implements IMailTransportSubmission.OnMessageSubmission
Try
Dim buffer As Byte() = CopyContentToStream(pIMailMsg)
Dim oFileStream As System.IO.FileStream
Dim nw As DateTime = Now
oFileStream = New System.IO.FileStream("C:\Logs\" &
nw.ToShortTimeString & " email.txt", System.IO.FileMode.OpenOrCreate)
oFileStream.Write(buffer, 0, buffer.Length - 1)
oFileStream.Close()
Catch cex As COMException
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = cex.Source & ": " & cex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
Catch ex As Exception
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = ex.Source & ": " & ex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
End Try
End Sub
Function CopyContentToStream(ByVal msg As Message)
Dim buffer As Byte()
Dim Offset As Integer = 0
Try
Const BYTES_TO_READ As Integer = 2048
buffer = msg.ReadContent(Offset, BYTES_TO_READ)
Catch cex As COMException
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = "In Function, " & cex.Source & ": " &
cex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
Catch ex As Exception
Dim sSource As String = "Exchange Sink Library"
Dim sLog As String = "Application"
Dim sEvent As String = "In Function, " & ex.Source & ": " &
ex.Message
If (Not EventLog.SourceExists(sSource)) Then
EventLog.CreateEventSource(sSource, sLog)
EventLog.WriteEntry(sSource, sEvent)
End Try
If buffer.Length > 0 Then
Return buffer
Else
Return Nothing
End If
End Function
End Class
End Namespace
if I edit this code to simply create an entry in event log every time the
sink class is run, it does and I get no errors.
I don't know how to pass the same interface to my function. Please help.