cancel
Showing results for 
Search instead for 
Did you mean: 

Processing multiple mail attachments in CPI

dan_dworak
Explorer

Has anyone been able to process multiple mail attachments in CPI? I'm able to get one attachment, and I've tried these approaches:

https://blogs.sap.com/2017/02/19/replacing-the-message-body-with-an-attachment/

https://answers.sap.com/questions/13316754/cpi-how-to-process-multiple-pdf-attachments.html

However it results in only capturing the last attachment. What I'm looking to do is process an email and send each attachment to a receiver such as SFTP.

Sending them in a zip archive and using a zip splitter works, but looking for a better solution.

Any help would be greatly appreciated!

Thanks

MortenWittrock
Active Contributor
0 Kudos

Hi Dan

Do you still have this problem? I believe I have a solution, but it's going to take a while to type it up.

Regards,

Morten

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

Hi Dan

Okay, here goes. The solution looks something like this:

The first script step (called "Prepare" in the above) copies the attachments into a property and then clears the attachments from the message. The Looping Process Call then calls the Local Integration Process called "Process one attachment" as long as there are attachments to process. Within the Local Integration Process, the script step picks the next attachment and replaces the message body with its contents. A Send step then sends that message to your SFTP server.

This is the code for the "Prepare" script step:

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

def Message processData(Message message) {
// Create a new Map of attachments and store it in a property.
def attachments = new HashMap<String, DataHandler>(message.getAttachments())
message.setProperty('Attachments', attachments)
// Store the number of attachments in a property.
message.setProperty('AttachmentCount', attachments.size())
// Clear the attachments and attachment wrappers.
message.getAttachments().clear()
message.getAttachmentWrapperObjects().clear()
// All done.
return message
}

The Looping Process Call step must be configured as follows:

Finally, here is the code for the "Get next attachment" script step:

import com.sap.gateway.ip.core.customdev.util.Message

def Message processData(Message message) {
def attachments = message.getProperty('Attachments')
if (attachments.isEmpty()) {
// This should never happen!
throw new AssertionError('Empty attachments Map in property Attachments')
}
// Get the key of the next attachment to process.
def nextKey = attachments.keySet().iterator().next()
// Remove the attachment from the Map
def attachment = attachments.remove(nextKey)
// Replace the message body with the attachment's contents.
message.setBody(attachment.getContent())
// Update the AttachmentCount property.
message.setProperty('AttachmentCount', attachments.size())
// All done.
return message
}

Let me know how it works out!

Regards,

Morten

dan_dworak
Explorer
0 Kudos

That worked perfectly. I just added a couple of lines to filter out the unwanted email signature/jpeg attachment.

Thanks so much for your help and taking the time to write this up!

dan_dworak
Explorer
0 Kudos

That worked perfectly. I just added a couple of lines to the script to filter out the unwanted email signature/jpeg attachment.

Thanks so much for your help and for taking the time to write up the details!

MortenWittrock
Active Contributor
0 Kudos

Hi dan.dworak

Great, glad to hear it worked out.

Regards,

Morten

yogesh_kumar_15
Explorer
0 Kudos

Hi 7a519509aed84a2c9e6f627841825b5a, I have a query that vice versa of this scenario, ie. Sending multiple SFTP files in a single mail receiver with multiple attachments. I have also raised a question about this. Send multiple attachment files in a single mail with multiple files from SFTP

Any idea on that?

Thanks.

Answers (2)

Answers (2)

iglmarkus
Explorer
0 Kudos

HI Morton,

I have the same requirement as Dan, but I have to process only XML and all other Attachment shall be ignored.

this is my groovy script - could you please tell me what is wrong. Is I am getting java.lang.NoSuchMethodException: No signature of method: java.util.HashMap.getContentType() is applicable for argument types: () values: [] during run-time.

import com.sap.gateway.ip.core.customdev.util.Message

import javax.activation.DataHandler

def Message processData(Message message) {

// Create a new Map of attachments and store it in a property.

def attachments = new HashMap<String, DataHandler>(message.getAttachments())

if(attachments.getContentType().contains("xml")){

message.setProperty('Attachments', attachments)

// Store the number of attachments in a property.

message.setProperty('AttachmentCount', attachments.size())

}

// Clear the attachments and attachment wrappers.

message.getAttachments().clear()

message.getAttachmentWrapperObjects().clear()

// All done.

return message

}

thanks for you help.

Markus

dan_dworak
Explorer
0 Kudos

Hi Morten,

I appreciate the response. No, I haven't been able to get it to work yet.