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



The information in this article applies to:

  • Microsoft BizTalk Server 2002

This article was previously published under Q326300

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.
  1. Create the Interop DLLs for MSCSCorelib.dll and BTSComplib.dll:
    1. Open a Visual Studio .NET command prompt, and then change to the following directory:

      Program Files\Microsoft.NET\Primary Interop Assemblies

    2. 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

    3. 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

    4. 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 "C:\Program Files\Common Files\Microsoft Shared\Enterprise Servers\Commerce\PipeComplib.tlb"
      /out:Interop_PipeComplib.dll/namespace:Interop_PipeComplib
      /asmversion:1.0/keyfile:Interop_PipeComplib.snk/primary

    5. 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 "C:\Program Files\Common Files\Microsoft Shared\Enterprise Servers\Commerce\MscsCore.dll"
      /out:Interop_MscsCore.dll/namespace:Interop_MscsCore
      /asmversion:1.0/keyfile:Interop_MSCSCore.snk/primary

  2. 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

back to the top

Write the Code for the Application Integration Component

  1. 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.
  2. 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("99BB72BC-9AA7-47dc-8064-288E3607FD9A")]
    	public class SMTPAttachmentAIC : ServicedComponent, IPipelineComponent, IPipelineComponentAdmin
    	{
    		int IPipelineComponent.Execute(object dispOrder, object dispContext, int Flags)
    		{
    			IDictionary dict = (IDictionary)dispOrder;
    			m_strFileLocation+=(string)dict["Tracking_ID"]+".txt";
    			StreamWriter outFile = new StreamWriter(m_strFileLocation);
    			try
    			{
    				outFile.WriteLine((string)dict["working_data"]);
    			}
    			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["File_Location"] = m_strFileLocation;
    			dict["To"]=m_strTo;
    			dict["From"]=m_strFrom;
    			dict["Subject"]=m_strSubject;
    			dict["SMTP_Server"]=m_strSMTPServer;
    			
    			return dict;		
    		}
    		
    		void IPipelineComponentAdmin.SetConfigData(object ConfigDictionary)
    		{
    			IDictionary dict = (IDictionary)ConfigDictionary;
    
    			if(dict["File_Location"] is System.DBNull)
    				m_strFileLocation = Path.GetTempPath();
    			else
    			{
    				if(((string)dict["File_Location"]).Length == 0)
    					m_strFileLocation = Path.GetTempPath();
    				else
    					m_strFileLocation = (string)dict["File_Location"];
    			}
    			m_strFileLocation=m_strFileLocation+"smtp";//+(string)dict["Tracking_ID"]+".txt";
    			if(dict["To"] is System.DBNull)
    				m_strTo="";
    			else
    			{
    				if(((string)dict["To"]).Length==0)
    					m_strTo="";
    				else
    					m_strTo=(string)dict["To"];
    			}
    			if(dict["From"] is System.DBNull)
    				m_strFrom="";
    			else
    			{
    				if(((string)dict["From"]).Length==0)
    					m_strFrom="";
    				else
    					m_strFrom=(string)dict["From"];
    			}
    			if(dict["SMTP_Server"] is System.DBNull)
    				m_strSMTPServer="";
    			else
    			{
    				if(((string)dict["SMTP_Server"]).Length==0)
    					m_strSMTPServer="";
    				else
    					m_strSMTPServer=(string)dict["SMTP_Server"];
    			}
    			if(dict["Subject"] is System.DBNull)
    				m_strSubject="";
    			else
    			{
    				if(((string)dict["Subject"]).Length==0)
    					m_strSubject="";
    				else
    					m_strSubject=(string)dict["Subject"];
    			}
    		}
    
    		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("CLSID\\\\{" + t.GUID.ToString() + 
    "}\\\\Implemented Categories\\\\{5c6c30e7-c66d-40e3-889d-08c5c3099e52}");
    			Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("CLSID\\\\{" + t.GUID.ToString() +
     "}\\\\Implemented Categories\\\\{bd193e1d-d7dc-4b7c-b9d2-92ae0344c836}");
    		}
    		[ComUnregisterFunctionAttribute]
    		public static void UnregisterFunction(Type t)
    		{
    			Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey("CLSID\\\\{" + t.GUID.ToString() +
     "}\\\\Implemented Categories\\\\{5c6c30e7-c66d-40e3-889d-08c5c3099e52}");
    			Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey("CLSID\\\\{" + t.GUID.ToString() +
     "}\\\\Implemented Categories\\\\{bd193e1d-d7dc-4b7c-b9d2-92ae0344c836}");
    		}
    	}	
    }
    					
  3. To add references for Interop_MscsCore.dll and Interop_PipeComplib.dll to the project, follow these steps:
    1. Right-click the SMTPAttach project in Solution Explorer, and then click Add Reference.
    2. Click Browse, and then locate the Program Files\Microsoft.NET\Primary Interop Assemblies folder.
    3. Select Interop_MscsCore.dll and Interop_PipeComplib.dll, and then click Open.
    4. Under Component Name, click System.EnterpiseServices, and then click Select.
    5. Under Component Name, select System.Web.dll, and then click Select. Click OK.
  4. 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.
  5. Right-click AssemblyInfo.cs, and then click View Code. Modify the existing assembly: AssemblyKeyFile entry to the following code:
    [assembly: AssemblyKeyFile("C:\\AIC\\SMTPAttach\\smtpaic.snk")]
  6. Save your changes, and then compile the project.
back to the top

Register the Assembly

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

regasm/tlb "C:\AIC\SMTPAttach\bin\debug\SMTPAttach.dll"

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 "C:\AIC\SMTPAttach\bin\debug\SMTPAttach.dll"

back to the top

Create and Install the Pipeline Configuration Pages

Use Notepad to create the following files:

  • SMTPAttachment_SMTPAttachmentAIC_post.asp
    <%@ LANGUAGE = VBScript %>
    <%
    '---------------------------------------------------------------------
    ' Microsoft BizTalk Server 2002
    '
    ' Visual Basic Pipeline Component Sample
    '
    ' (C) 1996-2001 Microsoft Corporation. All rights reserved.
    '
    '---------------------------------------------------------------------
    %>
    
    <!--#INCLUDE FILE="pe_global_edit.asp" -->
    <%
    call GetInputText("File_Location", 0, bufsize_medium)
    call GetInputText("To", 0, bufsize_medium)
    call GetInputText("From", 0, bufsize_medium)
    call GetInputText("Subject", 0, bufsize_medium)
    call GetInputText("SMTP_Server", 0, bufsize_medium)
    
    %>
    
    
    <!--#INCLUDE FILE="pe_post_footer.asp" -->
    						
  • SMTPAttachment_SMTPAttachmentAIC.asp
    <%@ LANGUAGE = VBScript %>
    <%
    '---------------------------------------------------------------------
    ' Microsoft BizTalk Server 2002
    '
    ' Visual Basic Pipeline Component Sample
    '
    ' (C) 1996-2001 Microsoft Corporation. All rights reserved.
    '
    '---------------------------------------------------------------------
    %>
    
    <!--#INCLUDE FILE="pe_edit_header.asp" -->
    <% 	
    	call InputText("Temp_Directory") 	
    	call InputText("To") 			
    	call InputText("From") 			
    	call InputText("Subject") 		
    	call InputText("SMTP_Server") 		
    %>
    
    
    <!--#INCLUDE FILE="pe_edit_footer.asp" -->
    						
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

  1. 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.
  2. To create a messaging port, click Application Integration Component in the drop-down combo box, click Browse, select SMTPAttachment SMTPAttachmentAIC, and then click OK.
  3. 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

  4. 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.
back to the top

Modification Type:MajorLast Reviewed:6/6/2003
Keywords:kbhowto kbHOWTOmaster KB326300 kbAudDeveloper