Technical Articles
SAP Cloud Platform Integration- Sending Multiple Attachments in Email
Introduction:
You might have come across a scenario, where it is required to send multiple attachments in Email using CPI Mail receiver adapter, As we have only option to send an attachment either by content of body or Header. Also we can not use header for sending large files as adapter has a size limitation of header content, If size exceeds the header allowable limit, Interface will fail. So only option left is to send body content as an attachment.
To overcome this limitation, we can use Groovy script to send multiple files as an attachment. There is a detailed blog on this topic by Morten Wittrock ,which you can refer.
How to use script for sending email attachments- Step by Step:
- Add content modifier step and add the custom text which needs to be sent in mail body.
- Save the content of the attachment in the property which has to be sent as an attachment.
- Add a Groovy script step to send the attachment:- Must use correct MIME type and file format in the script
import org.apache.camel.impl.DefaultAttachment
import javax.mail.util.ByteArrayDataSource
def Message processData(Message message) {
//Body
def body = message.getBody();
def id = map.get("requestData");
def bytes = map.get("requestData");
def dataSource = new ByteArrayDataSource(bytes, 'Text/CSV') //Set MIME type
def attachment = new DefaultAttachment(dataSource)
message.addAttachmentObject("Request.txt", attachment) //Add request attachment
id = map.get("responseData");
bytes = map.get("responseData");
dataSource = new ByteArrayDataSource(bytes, 'Text/CSV')
attachment = new DefaultAttachment(dataSource)
message.addAttachmentObject("Response.txt", attachment)
return message
- Don’t use any standard message attachment options from Mail Adapter. Just enable Add attachment option.
Conclusion:
This will be useful in not only implementing a effective error handling, but it can be also used in encoding or decoding the email attachment content before sending it to the required recipients. I hope this blog will be of a help where you are struggling in dealing with Email attachments.
Great blog. Thanks for sharing.