Skip to Content

SAP HCI Access mail attachment from sender

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    Feb 15, 2017 at 11:16 PM

    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

    Add comment
    10|10000 characters needed characters exceeded