Microsoft KB Archive/326300

= HOW TO: Sample Application Integration Component for Sending Documents as Attachments in Visual C# .NET =

Article ID: 326300

Article Last Modified on 6/6/2003

-

APPLIES TO


 * Microsoft BizTalk Server 2002 Standard Edition

-



This article was previously published under Q326300



IN THIS TASK
SUMMARY
 * Create the Interop DLLs, and Then Load Them into the Global Assembly Cache
 * Write the Code for the Application Integration Component
 * Register the Assembly
 * Load the Assembly into the Global Assembly Cache
 * Create and Install the Pipeline Configuration Pages
 * Process a Document with the Application Integration Component in Microsoft BizTalk Server



SUMMARY
This article demonstrates how to use a BizTalk Application Integration Component (AIC) to send BizTalk documents as attachments to Simple Mail Transfer Protocol (SMTP) messages by means of Visual C#. The AIC writes the document that it is processing to the BizTalk Server Temp folder or to the folder that you specify, and then the AIC sends the document as an attachment by using SMTP.

back to the top

Create the Interop DLLs, and Then Load Them into the Global Assembly Cache
NOTE: In the following steps, you run several commands from the Microsoft Visual Studio .NET command prompt with the Visual Studio .NET environment variables loaded. To open the Visual Studio .NET command-prompt window, click Start, point to Programs, point to Microsoft Visual Studio .NET, point to Visual Studio .NET Tools, and then click Visual Studio .NET Command Prompt.  Create the Interop DLLs for MSCSCorelib.dll and BTSComplib.dll:  Open a Visual Studio .NET command prompt, and then change to the following directory:

Program Files\Microsoft.NET\Primary Interop Assemblies

 Create the strong name PipeCompLib.tlb for the Interop DLL by running the following command from the Microsoft .NET command prompt:

sn -k Interop_PipeComplib.snk

 Create the strong name MSCSCore.dll for the Interop DLL by running the following command from the Visual Studio .NET command prompt:

sn -k Interop_MSCSCore.snk

 Use the Type Library to .NET Assembly Converter to create a primary interop assembly wrapper for PipeCompLib.tlb by running the following command from the Visual Studio .NET command prompt:

'''tlbimp &quot;C:\Program Files\Common Files\Microsoft Shared\Enterprise Servers\Commerce\PipeComplib.tlb&quot;

/out:Interop_PipeComplib.dll/namespace:Interop_PipeComplib

/asmversion:1.0/keyfile:Interop_PipeComplib.snk/primary'''

 Use the Type Library to .NET Assembly Converter to create a primary interop assembly wrapper for MscsCore.dll by running the following command from the Visual Studio .NET command prompt:

'''tlbimp &quot;C:\Program Files\Common Files\Microsoft Shared\Enterprise Servers\Commerce\MscsCore.dll&quot;

/out:Interop_MscsCore.dll/namespace:Interop_MscsCore

/asmversion:1.0/keyfile:Interop_MSCSCore.snk/primary'''

</ol> </li> Load the Interop DLLs into the Global Assembly Cache (GAC) by running the following commands from the Visual Studio .NET command prompt:

gacutil/i Interop_MscsCore.dll

-and-

gacutil/i Interop_PipeComplib.dll

</li></ol>

back to the top

Write the Code for the Application Integration Component
<ol> Start Visual Studio .NET, and then create a new Visual C# Windows Application project by using the Class Library template. Type C:\AIC for Location. Type SMTPAttach for Name.</li>  Paste the following code into the class for the project. By default, the class for the project is class1.cs. using System; using System.IO; using System.EnterpriseServices; using System.Runtime.InteropServices; using System.Web.Mail; using Interop_PipeComplib; using Interop_MscsCore;

namespace SMTPAttachment {   [Guid(&quot;99BB72BC-9AA7-47dc-8064-288E3607FD9A&quot;)] public class SMTPAttachmentAIC : ServicedComponent, IPipelineComponent, IPipelineComponentAdmin {       int IPipelineComponent.Execute(object dispOrder, object dispContext, int Flags) {           IDictionary dict = (IDictionary)dispOrder; m_strFileLocation+=(string)dict[&quot;Tracking_ID&quot;]+&quot;.txt&quot;; StreamWriter outFile = new StreamWriter(m_strFileLocation); try {               outFile.WriteLine((string)dict[&quot;working_data&quot;]); }           catch (Exception e)            { System.Diagnostics.Debug.WriteLine (e.Message); return 2; //Serious Error Occurred }           finally {               outFile.Close; }           try {                              MailMessage messageObj=new MailMessage; messageObj.To=m_strTo; messageObj.From=m_strFrom; messageObj.Subject=m_strSubject; MailAttachment attachmentObj=new MailAttachment(m_strFileLocation,MailEncoding.UUEncode); messageObj.Attachments.Add(attachmentObj); SmtpMail.SmtpServer=m_strSMTPServer; SmtpMail.Send(messageObj); }           catch (Exception e)            { System.Diagnostics.Debug.WriteLine (e.Message); return 2; //Serious Error Occurred }           finally {           }            try {               FileInfo fileObj=new FileInfo(m_strFileLocation); fileObj.Delete; }           catch (Exception e)            { System.Diagnostics.Debug.WriteLine(e.Message); return 2; //Serious Error Occurred }           finally {           }

return 0; //Return Success }       void IPipelineComponent.EnableDesign(int Enable) {       }

object IPipelineComponentAdmin.GetConfigData {              IDictionary dict = new Interop_MscsCore.CDictionaryClass;

dict[&quot;File_Location&quot;] = m_strFileLocation; dict[&quot;To&quot;]=m_strTo; dict[&quot;From&quot;]=m_strFrom; dict[&quot;Subject&quot;]=m_strSubject; dict[&quot;SMTP_Server&quot;]=m_strSMTPServer; return dict; }       void IPipelineComponentAdmin.SetConfigData(object ConfigDictionary) {           IDictionary dict = (IDictionary)ConfigDictionary;

if(dict[&quot;File_Location&quot;] is System.DBNull) m_strFileLocation = Path.GetTempPath; else {               if(((string)dict[&quot;File_Location&quot;]).Length == 0) m_strFileLocation = Path.GetTempPath; else m_strFileLocation = (string)dict[&quot;File_Location&quot;]; }           m_strFileLocation=m_strFileLocation+&quot;smtp&quot;;//+(string)dict[&quot;Tracking_ID&quot;]+&quot;.txt&quot;; if(dict[&quot;To&quot;] is System.DBNull) m_strTo=&quot;&quot;; else {               if(((string)dict[&quot;To&quot;]).Length==0) m_strTo=&quot;&quot;; else m_strTo=(string)dict[&quot;To&quot;]; }           if(dict[&quot;From&quot;] is System.DBNull) m_strFrom=&quot;&quot;; else {               if(((string)dict[&quot;From&quot;]).Length==0) m_strFrom=&quot;&quot;; else m_strFrom=(string)dict[&quot;From&quot;]; }           if(dict[&quot;SMTP_Server&quot;] is System.DBNull) m_strSMTPServer=&quot;&quot;; else {               if(((string)dict[&quot;SMTP_Server&quot;]).Length==0) m_strSMTPServer=&quot;&quot;; else m_strSMTPServer=(string)dict[&quot;SMTP_Server&quot;]; }           if(dict[&quot;Subject&quot;] is System.DBNull) m_strSubject=&quot;&quot;; else {               if(((string)dict[&quot;Subject&quot;]).Length==0) m_strSubject=&quot;&quot;; else m_strSubject=(string)dict[&quot;Subject&quot;]; }       }

string m_strFileLocation; string m_strTo; string m_strFrom; string m_strSMTPServer; string m_strSubject; [ComRegisterFunctionAttribute] public static void RegisterFunction(Type t)       { Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(&quot;CLSID\\\\{&quot; + t.GUID.ToString + &quot;}\\\\Implemented Categories\\\\{5c6c30e7-c66d-40e3-889d-08c5c3099e52}&quot;); Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(&quot;CLSID\\\\{&quot; + t.GUID.ToString + &quot;}\\\\Implemented Categories\\\\{bd193e1d-d7dc-4b7c-b9d2-92ae0344c836}&quot;); }       [ComUnregisterFunctionAttribute] public static void UnregisterFunction(Type t)       { Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey(&quot;CLSID\\\\{&quot; + t.GUID.ToString + &quot;}\\\\Implemented Categories\\\\{5c6c30e7-c66d-40e3-889d-08c5c3099e52}&quot;); Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey(&quot;CLSID\\\\{&quot; + t.GUID.ToString + &quot;}\\\\Implemented Categories\\\\{bd193e1d-d7dc-4b7c-b9d2-92ae0344c836}&quot;); }   }   }                    </li> To add references for Interop_MscsCore.dll and Interop_PipeComplib.dll to the project, follow these steps: <ol style="list-style-type: lower-alpha;"> Right-click the SMTPAttach project in Solution Explorer, and then click Add Reference.</li> Click Browse, and then locate the Program Files\Microsoft.NET\Primary Interop Assemblies folder.</li> Select Interop_MscsCore.dll and Interop_PipeComplib.dll, and then click Open.</li> Under Component Name, click System.EnterpiseServices, and then click Select.</li> Under Component Name, select System.Web.dll, and then click Select. Click OK.</li></ol> </li> To create the strong name for the AIC Assembly, run the following command from the Visual Studio .NET command prompt:

sn -k SMTPAIC.snk

NOTE: To create the .snk file in the C:\AIC\SMTPAttach folder, change to the C:\AIC\SMTPAttach folder before you run this command.</li>  Right-click AssemblyInfo.cs, and then click View Code. Modify the existing assembly: AssemblyKeyFile entry to the following code: [assembly: AssemblyKeyFile(&quot;C:\\AIC\\SMTPAttach\\smtpaic.snk&quot;)] </li> Save your changes, and then compile the project.</li></ol>

back to the top

Register the Assembly
Run the following command from a Visual Studio .NET command prompt:

regasm/tlb &quot;C:\AIC\SMTPAttach\bin\debug\SMTPAttach.dll&quot;

back to the top

Load the Assembly into the Global Assembly Cache
Run the following command from a Visual Studio .NET command prompt:

gacutil/i &quot;C:\AIC\SMTPAttach\bin\debug\SMTPAttach.dll&quot;

back to the top

Create and Install the Pipeline Configuration Pages
Use Notepad to create the following files:

<ul>  SMTPAttachment_SMTPAttachmentAIC_post.asp <%@ LANGUAGE = VBScript %> <% '- ' Microsoft BizTalk Server 2002 ' ' Visual Basic Pipeline Component Sample ' ' (C) 1996-2001 Microsoft Corporation. All rights reserved. ' '- %>

<% call GetInputText(&quot;File_Location&quot;, 0, bufsize_medium) call GetInputText(&quot;To&quot;, 0, bufsize_medium) call GetInputText(&quot;From&quot;, 0, bufsize_medium) call GetInputText(&quot;Subject&quot;, 0, bufsize_medium) call GetInputText(&quot;SMTP_Server&quot;, 0, bufsize_medium)

%>

</li>  SMTPAttachment_SMTPAttachmentAIC.asp <%@ LANGUAGE = VBScript %> <% '- ' Microsoft BizTalk Server 2002 ' ' Visual Basic Pipeline Component Sample ' ' (C) 1996-2001 Microsoft Corporation. All rights reserved. ' '- %>

<%      call InputText(&quot;Temp_Directory&quot;) call InputText(&quot;To&quot;) call InputText(&quot;From&quot;) call InputText(&quot;Subject&quot;) call InputText(&quot;SMTP_Server&quot;) %>

</li></ul>

Copy the SMTPAttach_SMTPAttach_post.asp and SMTPAttach_SMTPAttach.asp pages to the Program Files\Microsoft BizTalk Server\MessagingManager\pipeline folder.

back to the top

Process a Document with the Application Integration Component in BizTalk Server
<ol> Select Application Integration Component as the transport type when you configure a messaging port. The AIC component is displayed in the Select a Component dialog box.</li> To create a messaging port, click Application Integration Component in the drop-down combo box, click Browse, select SMTPAttachment SMTPAttachmentAIC, and then click OK.</li> <li>Bind a channel to the messaging port, and then modify the configuration settings for the AIC on the advanced configuration page of the channel. Enter data in the To, From, and SMTP Server fields. You may also enter configuration information into the Temp_Directory and Subject fields. When you complete the configuration, you can submit a document to this channel. To submit a document to a channel, use the DirectIntegration Software Development Kit (SDK) sample, which you can run from the following location:

Program Files\Microsoft BizTalk Server\SDK\Messaging Samples\DirectIntegration\EXE\DirectIntegration.exe

</li> <li>After you run the AIC, send your document as an e-mail attachment to the recipient that you specified when you configured the advanced properties of the channel.</li></ol>

back to the top

Keywords: kbhowto kbhowtomaster KB326300

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.