Stephen W. Thomas BizTalk Blog

100% Pure BizTalk
posts - 133, comments - 195, trackbacks - 427

My Links

News

Subscribe to my blog via:


Add to Google

Visit my website at:
www.BizTalkGurus.com

Archives

BizTalk 2004 Samples

BizTalk 2006 Samples

BizTalk Videos

BizTalk White Papers

BizTalkBlogs.com

Great BizTalk Blogs

Other Links

Splitting POP3 Email Attachments in BizTalk 2006

This was a question (paraphrased) on the BizTalkGurus.com Forum: “How can I take email attachments and put them as is into a folder?”

 

When using the POP3 Receive Adapter, you have the ability to receive multiple items as attachments and the adapter will handle them using MIME processing.  You do not need a custom pipeline. 

 

When receiving a multi-attachment message, you have the ability to define one of the parts as the main message Body.  If you leave the default (0), the email message body will be the main message Body and all attachments will be additional parts to the message.

 

Lets say you have 3 attachments and you want to receive in an email and write them out to disk in a folder location (you can always publish them to the message box and do anything you want with the messages).  You could probably use a custom pipeline to break out the multi-part message into separate messages... 

 

I found a very simple and straight forward approach to accomplish this task.  The Orchestration looks like this:

 

 

The key is to create an Orchestration variable of type Microsoft.XLANGs.BaseTypes.XLANGMessage.  In order to do this, you need to add a reference to Microsoft.XLang.BaseTypes.dll.  Once you have this variable, you just need to set it equal to your input message like: oXMessage = In. 

 

You now have access to everything the XLangMessage has available; most importantly Count and access to all the message parts.  Now a simple loop can be used to loop over the parts and create new output messages for each message part. 

 

The output messages are created like:  Out = oXMessage[n] were n is the message part index (starting at 1 since 0 is the Body).  It’s that simple.  It’s a total of 4 lines of code inside the Expression Shape and Message Construct. 

 

I have a sample solution available for download below.

 

Download: Splitting POP3 Email Attachments in BizTalk Server 2006

 

See the readme.txt file for set up information.

Print | posted on Tuesday, August 08, 2006 9:39 PM |

Feedback

Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Very nice example!
8/9/2006 9:19 AM | Jeff Lynch
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Is there a way to keep the attachment name?
9/13/2006 3:48 AM | Eric
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

I'm getting the following error when trying to deploy. I don't see any references to LAPTOP001 in the solution. Any help is appreciated.

Deployment cannot initialize the connection to the database "BizTalkMgmtDb" on server "LAPTOP001".
9/25/2006 5:08 AM | Ted E.
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

I'm getting the following error when trying to deploy. I don't see any references to LAPTOP001 in the solution. Any help is appreciated.

Deployment cannot initialize the connection to the database "BizTalkMgmtDb" on server "LAPTOP001".
9/25/2006 5:35 AM | Ted E.
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

On the project properties inside Visual Studios, you need to change that to the server you want to deploy to.

Stephen W. Thomas
9/25/2006 9:09 AM | Stephen W. Thomas
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Hello Stephen

I tried something similar to your solution all works fine. But when I sent an email with a XML attachment (1.xml), if fails in the first step of the Orchestration, which receives the message as a XML message. I downloaded your solution and tested, it has the same problem.

For some reason BizTalk doesn't like its body part being a XML document. Here is the event log info. Any idea or have you already come across this situation?

Event Type: Error
Event Source: XLANG/s
Event Category: None
Event ID: 10036
Date: 10/20/2006
Time: 5:05:33 PM
User: N/A
Computer: XXXXXXXX
Description:
Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'SampleEmailAttachment.SplitEmail(68855db4-196f-fd4a-61ef-367323d275ae)'.
The service instance will remain suspended until administratively resumed or terminated.
If resumed the instance will continue from its last persisted state and may re-throw the same unexpected exception.
InstanceId: 57b41a05-9b78-44a0-8703-cf5448f3d07b
Shape name: Receive_In
ShapeId: d1e5fb25-f931-478a-a24c-e601c3c4dc2f
Exception thrown from: segment 1, progress 3
Inner exception: Multi-part message 'In' has body part '1.xml', expected body part '{1A3D2E4A-EE92-4866-A179-D1A3F876B2A1}'.

Exception type: WrongBodyPartException
Source: Microsoft.XLANGs.BizTalk.Engine
Target Site: Void ReadMessageState(Microsoft.XLANGs.Core.Envelope, Microsoft.XLANGs.BaseTypes.XLANGMessage)
The following is a stack trace that identifies the location where the exception occured

at Microsoft.BizTalk.XLANGs.BTXEngine.BTXXlangStore.ReadMessageState(Envelope env, XLANGMessage msg)
at Microsoft.BizTalk.XLANGs.BTXEngine.BTXPortBase.ReceiveMessage(Int32 iOperation, Envelope env, XLANGMessage msg, Correlation[] initCorrelations, Context cxt, Segment s)
at SampleEmailAttachment.SplitEmail.segment1(StopConditions stopOn)
at Microsoft.XLANGs.Core.SegmentScheduler.RunASegment(Segment s, StopConditions stopCond, Exception& exp)
10/20/2006 6:31 PM | Umar Shareef
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Hi guys,

I've seen some posts asking how to get original filenames of the attachments. I've modified the Message Assignment part and sucessfully saved the attachments with the original file names:

-- start --
Out = oXMessage[n];
MsgPart = oXMessage[n];

// Set message properties
Out(FILE.ReceivedFileName) = MsgPart.Name;

-- end --

where MsgPart is a variable of type Microsoft.XLANGs.BaseTypes.XLANGPart

Then I use the %SourceFileName% macro in a FILE Send Port and able to save the attachments with the original filenames. Hope this helps.

11/15/2006 6:45 AM | Stephen Wong
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

I have the same WrongBodyPartException problem on getting XML attachments.. any help would be appreciated..
11/17/2006 3:45 AM | Stephen Wong
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

You might want to check out the forum.

This post might help: http://www.biztalkgurus.com/forums/1/4449/ShowThread.aspx I know we’ve talked about this problem but I’m not sure what the solution was.
11/20/2006 7:31 PM | Stephen W. Thomas
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

To get out the attachment file name you can find it in the MIME properties.
Eks.

// Get the n'th part and set to a new message
Out = oXMessage[n];

// Set message properties
Out(FILE.ReceivedFileName) = Out(MIME.FileName);

This works for me on Biztalk 2006. In Biztalk 2004 you have to use the SMTPUtils class.
Hope this hepls you out

Regards
Benny Helander
11/21/2006 4:58 AM | Benny Helander
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Thanks for the niec example, i am getting a weird error, when sending email attachments which are less than 70k in sixe i dun have any issues , but once the file size is around a 100k the Biztalk POP3 adapter errors and spits out the error message:
A message received by adapter "POP3" on receive location ".." with URI ".." is suspended.
Error details: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Any body came acorss this error before ???
5/3/2007 11:18 PM | ganda
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Thanks for the niec example, i am getting a weird error, when sending email attachments which are less than 70k in sixe i dun have any issues , but once the file size is around a 100k the Biztalk POP3 adapter errors and spits out the error message:
A message received by adapter "POP3" on receive location ".." with URI ".." is suspended.
Error details: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Any body came acorss this error before ???
5/3/2007 11:18 PM | ganda
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

For multipart message

String str_Type

str_Type = System.Convert.ToString(Out(Microsoft.XLANGs.BaseTypes.ContentType));

if (str_Type.Contains("xml")) {
Out(FILE.ReceivedFileName) = Out(MIME.FileName)
}
else {
Out(FILE.ReceivedFileName) = MsgPart.Name
};
8/20/2007 5:51 AM | Stanislav Kuzmin
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Did anyone solve the issue mentioned here by ganda. I too get the E_ACCESSDENIED problems. There is nothing about this anywhere that I can find. the problems occur with some Excel spreadsheet attachments. They are not protected, larger or formatted than my test spreadsheets that work fine!!

But they may be an earlier version Excel 95. Not sure why this should make a difference though???
9/24/2007 7:53 AM | Paul K
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Hey Stephen

I download the solution and it works pretty good to me except one issue to me. Some of my emails are generated in .Net code using SmtpClient class. For those emails, I can not use MIME.FileName to extract file names. It will throw exception like below

Uncaught exception (see the 'inner exception' below) has suspended an instance of service 'Sanjel.BizTalk.EngEmail.Mornitor.RetrieveEmailAttachments(186a881a-d009-d251-f4e4-d5c2bc210bf5)'.
The service instance will remain suspended until administratively resumed or terminated.
If resumed the instance will continue from its last persisted state and may re-throw the same unexpected exception.
InstanceId: 4ad55064-dca9-466c-b7f4-3db5a857fbf9
Shape name: ConstructSendMessage
ShapeId: 774a646e-8b07-4702-89dc-1d2a66742b71
Exception thrown from: segment 1, progress 15
Inner exception: There is no value associated with the property 'MIME.FileName' in the message.

Exception type: MissingPropertyException
Source: Microsoft.XLANGs.Engine
Target Site: System.Object GetPropertyValueThrows(System.Type)
The following is a stack trace that identifies the location where the exception occured

at Microsoft.XLANGs.Core.Part.GetPropertyValueThrows(Type propType)
at Sanjel.BizTalk.EngEmail.Mornitor.RetrieveEmailAttachments.segment1(StopConditions stopOn)
at Microsoft.XLANGs.Core.SegmentScheduler.RunASegment(Segment s, StopConditions stopCond, Exception& exp)



For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.


Any ideas will be extremly appreciated.

Please please help.
10/15/2007 3:48 PM | Michael
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Anybody can give me ideas?

Waiting online?
10/16/2007 9:43 AM | Michael
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Anybody can help me out? It is really urgent.

10/16/2007 3:43 PM | Michael
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

1. Microsoft Biztalk uses optional MIME Header “Content-Disposition” for retrieving the Filename of attachments.

This header was standarised 1997 for exactly that purpose. See RFC2183 for details (ftp://ftp.rfc-editor.org/in-notes/rfc2183.txt -> Point 2.3 The Filename parameter))

It is an optional Header because not all MIME message parts are to be an attachment and therefore need a filename suggestion.

It seems that still today work quite often older smtp servers are not using MIME header "Content-Disposition".

By the way : Outlook is automatically adding the MIME Header "Content-Disposition" when forwarding such an email.

However, we are receiving problematic emails through a forwarding rule from an Exchange 2003 Server(!), because Exchange 2003 does not add the MIME header.

These old smtp servers send Emails that are not conform to RFC2183 (=content disposition) AND ALSO is not conforming to RFC 2045 which is clearly stating that additional MIME header must begin with “Content-“ , this is not the case.

ftp://ftp.rfc-editor.org/in-notes/rfc2045.txt (Page 26, Number 9)

10/24/2007 7:30 AM | Robin Prosch
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

... so bad luck, no chance to get the filenames in Biztalk 2006 without some extra effort...

So my recommendation is

• Put a Outlook forwarding the emails between the outer world and the POP3 Biztalk Adapter.
Ugly, yes, but a valid workaround :-)

• Change your scenario so that you do not need the attachment name anymore.

• Bypass Microsoft Biztalk MIME decoder and build our own MIME decoder handling this issue more “backward-compatible”.

... or convince Microsoft that this is a mayor business impact, so they adapt their own "standard conform" but NOT backward compatible MIME decoder by themselves.


Yours,
Robin


P.S. All above statements are based upon my own research, maybe there are more expert persons out there that find something uncorrect in the above, then please feel free to give us all a hint.

10/24/2007 7:31 AM | Robin Prosch
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Thank you Robin

We gave up BizTalk and chose another sulotion for this scenario.
10/31/2007 3:12 PM | Michael
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Hi all,
I found a way of getting the attachment-name out of the message using Stephens starting point. The little trick I used was to implement a XlangPart variable.

(Cut from Stephens original code)
// Note the n was initilized to 1 inside Variables

// Get the n'th part and set to a new message
Out = oXMessage[n];
oXPart = oXMessage[n];

// Set message properties
Out(FILE.ReceivedFileName) = oXPart.Name;

// Add to count
n = n + 1;

I tested it and it seems to work. Thanks for the article Stephen, it has been of great help :).

Jmor
9/1/2008 8:36 AM | Jmor
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Doh!! Saw Stephen Wong solved this ages ago... sorry for the spamming...
Jmor
9/1/2008 8:39 AM | Jmor
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Hi,
I am very new to Biz talk. I downloaded application and deploy it successfully.

But when i create new receive location as per my mailid and server it gives error.

"Message-pop" could not be bound to "SampleEmailAttachment.splitEmail".The Artifact "Message-pop" does not belong to same application as "SampleEmailAttachment.splitEmail" or its reference

Message-pop is my new receive location which i want to create.

Please guide me how to configure receive and send location.
11/17/2008 12:48 AM | Umesh
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006


Hello, I´m having a similar problem. I used the discussed approach and found two problems:

1º in a specific message type (when sending a mail using outlook - rich text format) with one attachment (only one) the IN_MSG will only have ONE part. If more attachments are sent this problem will not ocurr. This situation only happens when the mail has no text in the body. Still, an annoying problem.

2º I need to extract the name of each attachment. I use the property Name from the XLangPart to get the attachment name. But... in some cases i cannot extract this information (a GUID appears instead of the name...)

Any ideas?

Thank you.

12/11/2008 10:54 AM | jcr
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Did anybody ever figure out this issue?

º I need to extract the name of each attachment. I use the property Name from the XLangPart to get the attachment name. But... in some cases i cannot extract this information (a GUID appears instead of the name...)

I'm getting the same thing, sometimes a nice filename, sometimes a weird guid
3/18/2009 9:49 AM | Paul
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

For those of you having troubles with XML attachments (WrongBodyPartException).
I've set the "Body Part Content Type" to "text/plain" (POP3 Transport Properties dialog in Receive Location Properties) and now it can handle XML files as well as other types of attachments.
Other settings are:
Body Part Index = 0
Apply Mime Decoding = True

PassThruReceive pipeline used.
4/10/2009 6:12 AM | evgendkv
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Hi All,

I have implemented the logci for splitting email attachments, but the only scenario its is failing is there are no attachments, still the "Count" is 2. So its trying to get the name from MIME.FileName which is not present.

How to handle this situation? can anyone please help me on this scenario?
8/11/2009 10:30 AM | Govardhan Reddy
Gravatar

# re: Splitting POP3 Email Attachments in BizTalk 2006

Hi, thanks for this sample! I was looking for exactly something like this to demo to someone else. I can report it works as is in BizTalk 2009 (including reading the attachment filenames using the following code:

MsgPart = oXMessage[n];
Out(FILE.ReceivedFileName) = MsgPart.Name;
9/30/2009 4:25 AM | Rudolf
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: