Click here to Skip to main content
15,885,278 members
Articles / Programming Languages / C#

Sending original CSV file, name and extension using SMTP Adapter

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
2 Oct 2013CPOL3 min read 22.7K   1   7
Configure a custom pipeline component to be used in sending original flat file name and extension using Microsoft BizTalk 2010 SMTP adapter.

 Introduction 

Using BizTalk 2010 SMTP adapter is a handy way to send an incomming message to a list of email users, but it has some limitation. The file will be sent with body name and no extension. Below pipeline component will enhance this functionality. 

Using the Code

In order to create a custom pipeline component use VS 2010 and create a new Class Library project. I named mine AttachFile. Rename the default cs file to AttachFile as well.  

Add a reference to Microsoft.BizTalk.Pipeline. The DLL is located on C:\Program Files (x86)\Microsoft BizTalk Server 2010\Microsoft.BizTalk.Pipeline.dll

Image 1 
Add following declarations to your AttachFile.cs:

using System.IO;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;
    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
    [ComponentCategory(CategoryTypes.CATID_Encoder)]
    [System.Runtime.InteropServices.Guid("9d0e4103-4cce-4536-83fa-4a5040674ad6")] 

Change Execute method to look like this:

public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
 string filename = pInMsg.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties").ToString();
 filename = filename.Substring(filename.LastIndexOf("\\") + 1);
 pInMsg.BodyPart.PartProperties.Write("FileName", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", filename);
 return pInMsg;
}

Open Project Properties and on Signing tab  add a stong key to sign the assembly. 

Save and Build

Your solution should look like this:

Image 2
and the whole AttachFile.cs is here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;


namespace AttachFile
{
    [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
    [ComponentCategory(CategoryTypes.CATID_Encoder)]
    [System.Runtime.InteropServices.Guid("9d0e4103-4cce-4536-83fa-4a5040674ad6")]
    public class AttachFile : IBaseComponent,
                              IComponentUI,
                              IComponent,
                              IPersistPropertyBag
    {
        #region IBaseComponent Members

        public string Description
        {
            get
            {
                return "Pipeline component used to attach file name";
            }
        }
        public string Name
        {
            get
            {
                return "AttachFile";
            }
        }
        public string Version
        {
            get
            {
                return "1.0.0.0";
            }
        }
        #endregion

        #region IComponentUI Members

        public IntPtr Icon
        {
            get
            {
                return new System.IntPtr();
            }
        }

        public System.Collections.IEnumerator Validate(object projectSystem)
        {
            return null;
        }

        #endregion

        #region IPersistPropertyBag Members

        public void GetClassID(out Guid classID)
        {
            classID = new Guid("655B591F-8994-4e52-8ECD-2D7E8E78B25C");
        }

        public void InitNew()
        {

        }

        public void Load(IPropertyBag propertyBag, int errorLog)
        {

        }

        public void Save(IPropertyBag propertyBag, bool clearDirty, bool saveAllProperties)
        {

        }

        #endregion

        #region IComponent Members

        public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
        {
         string filename = pInMsg.Context.Read("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties").ToString();
         filename = filename.Substring(filename.LastIndexOf("\\") + 1);
         pInMsg.BodyPart.PartProperties.Write("FileName", "http://schemas.microsoft.com/BizTalk/2003/mime-properties", filename);
         return pInMsg;
        }

        #endregion
    }
}

 Search for your AttachFile.dll. This should be located under  "your project folder"/bin/Debug/. Copy the dll to C:\Program Files (x86)\Microsoft BizTalk Server 2010\Pipeline Components.

Add dll to GAC. For this open Visual Studio Command prompt and use gacutil /i AttachFile.dll:

Image 3

Now open your  BizTalk Server Administration Console and if you didnt create an SMTP handler yet, go to Adapters section and select SMTP. In SMTP - Adapter Handles Proprieties click on Proprieties and add SMTP server name From email and select Authentication type:

Image 4 

You'll have to ask your company sys admin for proper values.

Then open your BizTalk project (the one you want to send emails) and use Add/NewItem to add a Send Pipeline. I named mine SendFile.btp, but that doesn't matter. Add your custom pipeline component to VS ToolBox. To do this go to Tools/Choose Toolbox Items, select BizTalk Pipeline Components and locate AttachFile one. Tick the checkbox in front of the name:

Image 5

 Once this is done you can add this Pipeline Component to your Encode section in SendFile.btp using drag and drop. Your SendFile.btp should look like:

Image 6 

Build and Deploy your project.

Go back to your BizTalk Server Administration Console and in your project create a send port. For Type selct SMTP from the available drop down list. Set To and Subject on General tab, select Text on Compose tab and type in a text and select Attachonly body part in Attachement tab.

For Send pipeline choose SendFile from the drop down list. If required use Filters to set the filter. (In my production case I used a send group port to deliver the incomming csv file to a BizTalk orchestration for processing and, in the same time, send it as an email to end user. By using a send port group I left the filter empty on my SendEmail port. The filter is set once only for the whole send group.)

Create a Receive Location and use default pipeline (PassThruReceive) to pick up the csv file from a file location. Set the file mask to *.csv. Than set the filter in your new Send Email port to this Receive Location. See below how this port looks:

Image 7 

Image 8


Next time an csv file is dropped in the selected location you'll receive an email from BizTalk having the csv file attached to this email with it's original file name and file extension.

Points of Interest

This being my first CodeProject post I made it very detailed. It might look too detailed for an asvanced user but a beginner will be happy to find more details.

History

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIs this working with BizTalk 2013 R2? Pin
Joel Linderos14-Jun-18 3:00
Joel Linderos14-Jun-18 3:00 
Hi!

I'm trying to get this to work with BizTalk 2013 R2, but when the receive port have consumed the file I end up with the following error message: "The requested body part was not found in this message." Do you by any chance know what have changed from BizTalk 2010 and what additional steps you need to add?
QuestionWorks like expected on any kind of attachment Pin
Member 821361214-Aug-17 4:26
Member 821361214-Aug-17 4:26 
QuestionGreat Explanation Pin
Anand SB5-Mar-17 20:10
Anand SB5-Mar-17 20:10 
QuestionNot working here Pin
AssebRic6-Jan-14 6:40
AssebRic6-Jan-14 6:40 
QuestionWhat is BizTalk and what it is used for Pin
Tridip Bhattacharjee2-Oct-13 21:02
professionalTridip Bhattacharjee2-Oct-13 21:02 
AnswerRe: What is BizTalk and what it is used for Pin
Eduard Gruber3-Oct-13 2:54
Eduard Gruber3-Oct-13 2:54 
GeneralRe: What is BizTalk and what it is used for Pin
Tridip Bhattacharjee3-Oct-13 21:20
professionalTridip Bhattacharjee3-Oct-13 21:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.