ARTICLE

Microsoft CRM customization: MS Exchange Transport SMTP Event Sink in VB.NET

Posted by BorisM Articles | Visual Basic 2010 October 18, 2004
Microsoft CRM has variety of customizations options and tools. The official and the most popular is Microsoft CRM SDK: collection of C#.Net and partially VB.Net classes, methods and code samples.
 
Reader Level:

Microsoft CRM has variety of customizations options and tools. The official and the most popular is Microsoft CRM SDK: collection of C#.Net and partially VB.Net classes, methods and code samples. Here we would like to give you more complex case, when you call CRM SDK customization from custom MS Exchange event handler - we are improving the functionality of MS Exchange - MS CRM connector.

Imagine the case when you want outgoing email to be captured and placed into CRM, attached to Contact, Account or Lead they should belong to. If this is realized - your salespeople can use any email tool to send their messages, they do not have to do it in CRM or Outlook Client for CRM.

MS Exchange OnSyncSave database event can't work with Sent folder - it doesn't fire when message goes to Sent folder. The reason is described here:

PRB: Store Events Do Not Fire on the Outbox or Sent Item Folders
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q297274

Please, see SMTP Event Sink example in this article:
 
http://support.microsoft.com/default.aspx?scid=kb;en-us;317327.

Event handler works OnArrival event:

Private Sub OnArrival(ByVal msg As CDO.IMessage, ByRef EventStatus As CDO.CdoEventStatus) Implements ISMTPOnArrival.OnArrival
log = LogManager.GetLogger(
GetType
(ShieldsUp))
DOMConfigurator.Configure(
New
FileInfo(Environment.SystemDirectory & "/CustomerApp/log.config"))
Try
ProcessMessage(msg)
Catch ex As
Exception
log.Debug(ex.Message + Constants.vbLf + ex.StackTrace)
Finally
LogManager.Shutdown()
End
Try
End
Sub

The class:

' ComVisible enables COM visibility of this class. The default is true.
' Explicitly setting this attribute to true, as shown below, is useful
' if ComVisible is set to false for the namespace and you want the
' classes to be accessible individually.

<ComVisible(True)> _
Public Class ShieldsUp : Inherits
CDO.ISMTPOnArrival
Next the handling works similar to SyncSave handler:
Private Sub ProcessMessage(ByVal msg As
CDO.IMessage)
Dim sFrom As
String
Dim
sTo As
String
Dim
sSubject As
String
Dim
sBody As
String
Dim
sSensitivity As
String
Try
log.Debug("Firing Up ProcessMessage()")
sSubject = msg.Subject
sBody = msg.TextBody
sFrom = msg.From
sTo = msg.To
If Not msg.Fields("urn:schemas:mailheader:sensitivity").Value Is Nothing
Then
sSensitivity = msg.Fields("urn:schemas:mailheader:sensitivity").Value.ToString()
Else
sSensitivity = "Normal"
End
If
log.Debug("Message From: " & sFrom)
log.Debug("Message To: " & sTo)
log.Debug("Subject: " & sSubject)
log.Debug("Sensitivity: " & sSensitivity)
log.Debug("Body: " & sBody)

In deployment you should consider the following - the handler will work only in the case of SMTP protocol delivery. If you use Outlook or Outlook Web Access, then delivery uses MAPI and OnArrival doesn't fire. Please see this article:
 
http://support.microsoft.com/default.aspx?scid=kb;en-us;273233 The elegant fix is two SMTP gateways, find it here http://support.microsoft.com/default.aspx?scid=kb;en-us;Q288756 

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).

Login to add your contents and source code to this article
share this article :
post comment
 

To all who need more information on writing managed event sinks (for IIS SMTP or Exchange), please take a look at my blog. http://blog.rednael.com/ There is a very in depth article on How to write a custom authentication sink (using the AUTH command). It addresses various problems that are solved when writing event sinks. For example, how do you read input lines after the AUTH command is processed. And how do you use the property bags within a session. And more... Also, there is a list of registry settings for IIS SMTP (as well as Exchange 2000/2003), with which you can influence the behavior of the SMTP server. Enjoy...

Posted by sjaak banaan Aug 29, 2008
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor