Debatching excel sheet attachments using POP3 adapter

Hi,

I have tried implementing link : BizTalk Server: How to Extract Email Attachments By Pipeline

for debatching the excel files as an attachment through POP3 adapter. It works fine for the text files but I receive an corrupted file while testing for excel files. It says "excel cannot open file.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and the file extension matches the format of the file".

Please suggest.

Thanks in advance.

Ranjana

September 3rd, 2015 6:54am

Hi Ranjana,

I Agree there is a bug in that code, have fixed that now with the below code.

Please replace the below lines code with this.

string attachmentContent = Encoding.Default.GetString(ms.ToArray());

MemoryStream attachmentStream = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(attachmentContent));

Have tested the 3 scenarios.

- Text file

- XML file

- Excel File

Let us know if you face any issue again.

Thanks, SMSVikasK

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 7:22am

Hi Ranjana,

I suggest you to look the comments shared by "Maurice CGP Peters" to handle multiple attachments for file names.

for (int j = 0; j < inmsg.Context.CountProperties; j++)
{
string currentName;
string currentNamespace;
object obj = inmsg.Context.ReadAt(j, out currentName, out currentNamespace);
outMsg.Context.Write(currentName, currentNamespace, obj);
if (inmsg.Context.IsPromoted(currentName, currentNamespace))
{
   outMsg.Context.Promote(currentName, currentNamespace, obj);
}
}

Thanks, SMSVikasK

September 3rd, 2015 7:30am

Hi Ranjana,

In the link you provided Maurice CGP Peters provided the fix for having multiple attachments and also getting any attachment file and not just text files.

Basically the issue seems to be with the use of UTF8 Encoding Scheme in the implmentation. You can check the following code provided by Marcus that does not use any Encoding Scheme and will handle multiple attachments as well:

public void Disassemble(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
 
       {
 
           var partName = string.Empty;
 
           // we start from index 1 because index zero contains the body of the message
 
           // which we are not interested
 
           for (int i = 1; i < inmsg.PartCount; i++)
 
           {
 
               IBaseMessagePart currentPart = inmsg.GetPartByIndex(i, out partName);
 
               Stream currentPartStream = currentPart.GetOriginalDataStream();
 
               var ms = new MemoryStream();
 
               IBaseMessage outMsg;
 
               outMsg = pc.GetMessageFactory().CreateMessage();
 
               outMsg.AddPart("Body", pc.GetMessageFactory().CreateMessagePart(), true);
 
               for (int j = 0; j < inmsg.Context.CountProperties; j++)
 
               {
 
                   string currentName;
 
                   string currentNamespace;
 
                   object obj = inmsg.Context.ReadAt(j, out currentName, out currentNamespace);
 
                   outMsg.Context.Write(currentName, currentNamespace, obj);
 
                   if (inmsg.Context.IsPromoted(currentName, currentNamespace))
 
                   {
 
                       outMsg.Context.Promote(currentName, currentNamespace, obj);
 
                   }
 
               }
 
               currentPartStream.CopyTo(ms);
 
               outMsg.GetPart("Body").Data = ms;
 
               //Promote attachment file name
 
               outMsg.Context.Write("ReceivedFileName", "schemas.microsoft.com/.../file-properties" , currentPart.PartProperties.Read("FileName", "schemas.microsoft.com/.../mime-properties" ));
 
               _msgs.Enqueue(outMsg);
 
           }
 
       }

Please indicate "Mark as Answer" or "Mark as Helpful" if this post has answered the question

Rahul

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 7:40am

Hi Vikas,

With the code you provided my pipeline component worked for all the three scenarios.

Thanks!!

September 3rd, 2015 10:58am

Hi  Vikas,

My requirement is only for one attachment. Thanks for the link though. It may be useful to me in future.

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 11:00am

Hi Rahul,

My requirement is only for one attachment. Thanks for the link though. It may be useful to me in future.

September 3rd, 2015 11:00am

Hi Ranjana,

I Agree there is a bug in that code, have fixed that now with the below code.

Please replace the below lines code with this.

string attachmentContent = Encoding.Default.GetString(ms.ToArray());

MemoryStream attachmentStream = new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(attachmentContent));

Have tested the 3 scenarios.

- Text file

- XML file

- Excel File

Let us know if you face any issue again.

Thanks, SMSVikasK

Free Windows Admin Tool Kit Click here and download it now
September 3rd, 2015 11:15am

Hi Ranjana,

I suggest you to look the comments shared by "Maurice CGP Peters" to handle multiple attachments for file names.

for (int j = 0; j < inmsg.Context.CountProperties; j++)
{
string currentName;
string currentNamespace;
object obj = inmsg.Context.ReadAt(j, out currentName, out currentNamespace);
outMsg.Context.Write(currentName, currentNamespace, obj);
if (inmsg.Context.IsPromoted(currentName, currentNamespace))
{
   outMsg.Context.Promote(currentName, currentNamespace, obj);
}
}

Thanks, SMSVikasK

September 3rd, 2015 11:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics