Click here to Skip to main content
13,291,690 members (60,857 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.4K views
1 bookmarked
Posted 2 Oct 2013

Sending original CSV file, name and extension using SMTP Adapter

, 2 Oct 2013
Rate this:
Please Sign up or sign in to vote.
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

 
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:


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:

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:

 

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:

 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:

 

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:

 


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)

Share

About the Author

Eduard Gruber
Software Developer (Senior)
Australia Australia
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionWorks like expected on any kind of attachment Pin
Member 821361214-Aug-17 5:26
memberMember 821361214-Aug-17 5:26 
QuestionGreat Explanation Pin
Anand S B5-Mar-17 21:10
memberAnand S B5-Mar-17 21:10 
QuestionNot working here Pin
AssebRic6-Jan-14 7:40
memberAssebRic6-Jan-14 7:40 
QuestionWhat is BizTalk and what it is used for Pin
Tridip Bhattacharjee2-Oct-13 22:02
memberTridip Bhattacharjee2-Oct-13 22:02 
AnswerRe: What is BizTalk and what it is used for Pin
Eduard Gruber3-Oct-13 3:54
memberEduard Gruber3-Oct-13 3:54 
GeneralRe: What is BizTalk and what it is used for Pin
Tridip Bhattacharjee3-Oct-13 22:20
memberTridip Bhattacharjee3-Oct-13 22: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 3 Oct 2013
Article Copyright 2013 by Eduard Gruber
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid