Skip to Content
0

SAP HCI Access mail attachment from sender

Feb 15, 2017 at 06:14 PM

647

avatar image

Hello,

I have a scenario where I collect an email by IMAP. This mail will have one XML-file attachment. I need to map this XML file. But I don’t know how to access this attachment and route it forward instead of the message body.

I tried to access those with ${in.attachments} and ${in.attachment} but this doesen’t work. If I just forward the whole message to an email receiver, I get the attachment again. Therefore, the attachment is there but I just do not know how to access it and forward it to a mapping.

Can anybody may help me?

Thanks Florian

10 |10000 characters needed characters left characters exceeded

If you are interested in a bit more detail, I've expanded on my answer in a blog post.

0
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Morten Wittrock Feb 15, 2017 at 11:16 PM
6

Hi Florian

You can access the attachment in a script step, and replace the message body with its content. Give the following code a try:

import com.sap.gateway.ip.core.customdev.util.Message
import java.util.Map
import java.util.Iterator
import javax.activation.DataHandler

def Message processData(Message message) {
   Map<String, DataHandler> attachments = message.getAttachments() // [1]
   Iterator<DataHandler> it = attachments.values().iterator()      // [2]
   Object attachment = it.next().getContent()                      // [3]
   message.setBody(attachment)                                     // [4]
   return message
}

Most of the code deals with the fact, that the attachments are returned as a Map object, and we don't know the key of the only value.

In [1] I get the Map of all the message's attachments (we're assuming there's only one). In [2] I create an Iterator, that will iterate over the values, which are DataHandler objects. In [3] I get the content of the first value, and finally in [4] I set the message's body to that content.

I've only tested this with SOAP attachments, but the principles are the same.

Let me know how it works out :)

Regards,

Morten

Show 7 Share
10 |10000 characters needed characters left characters exceeded

Thank you very much Morten!

This is working. I just need to add the ";".

If I forward this message by mail, I get the XML in the mail body. Exactly how I expected it.

To test the XML structure I just add a message mapping where the input is the same as the output structure. Then I added the mapping for every field.

But then I get the following error

com.sap.xi.mapping.camel.XiMappingException:
com.sap.aii.mappingtool.tf7.IllegalInstanceException: Cannot produce target
element /ns1:JournalEntryBulkCreateRequest/MessageHeader. Queue has not enougth
values in context. Target xsd requires a value for this element, but target
field mapping does not produce one. Probably the xml-instance is not valid to
the source xsd, or the target field mapping does not fulfill the requirement of
the target xsd., cause: com.sap.aii.mappingtool.tf7.IllegalInstanceException:
Cannot produce target element /ns1:JournalEntryBulkCreateRequest/MessageHeader.
Queue has not enougth values in context. Target xsd requires a value for this
element, but target field mapping does not produce one. Probably the
xml-instance is not valid to the source xsd, or the target field mapping does
not fulfill the requirement of the target xsd.

But this element is there in the source message. So do I need to make some kind of MIME encoding that the payload is treated like XML?

Could you may also help me with that? :-)

Thank you and regards

Florian

0

Hi Florian. I take it you have an XML Schema for the XML format. Is the message valid according to that schema?

1

Thanks Morten - that was the problem. There was on surrounding element.

Problem is solved.

0

Glad to hear it :)

0

Hello Florian, hello Morten,

would you please be so kind and explain to a rookie, how it is possible to receive emails via imap to the SCI (formerly HCI) e.g. from gmail? I am only able to send, using the mail adapter, but I have no clue how to receive emails.

Best regards,
Patric

0

Hi Morten,

Do you have any idea on how to create an attachment in HCI integration flow.

Actually I have a use case of message body content to be created as an attachment. So that I can use that attachment in mail adapter.

Regards,

Deva

0

Hi Deva

I've replied in your other thread.

Regards,

Morten

0