How to send an image file to Mail using SAP CPI
2023-10-3 05:8:34 Author: blogs.sap.com(查看原文) 阅读量:39 收藏

In this scenario we are sending the image as attachment to mail from SAP CPI.

There are Three ways:

  1. By hosting the image in the cloud services (ex: google drive, Dropbox etc.)
  2. Directly creating the image as Attachment using Groovy.
  3. Directly By setting the MIME Type(image/(jpg/jpeg).

Method 1: By hosting the image in the cloud services (ex: google drive, Dropbox etc.).

We can use any cloud service to store the image or host the image in any private cloud services.

In this method, first we need to host the image.

For hosting purpose, we can use online free hosting websites. In this blog I am using the ImgBB website (https://imgbb.com/upload) here we can upload the image for free and after uploading the image we get the html URL link.

Step 1: First, go to this link https://imgbb.com/upload and, click on the upload button.

Step 2: Click on the browse from computer and select the image you want to upload from the local computer.

Step 3: Click on the upload button after that. we get the html code tag  with the image URL encoded in the tags.

Step 4: Note down the Embed Code for future use.

Step 5: Login to SAP CPI and the create the iflow with the desired name with sender adapter as HTTP and give the preferred HTTP address.

Step 6: Now Choose the send Element from palette (Call > External call > Send>) and join the send element with the receiver with Mail Adapter.

Step7: Now configure the Mail Adapter.

For the mail adapter Configuration follow the below links.

https://help.sap.com/docs/cloud-integration/sap-cloud-integration/configure-mail-receiver-adapter

https://blogs.sap.com/2022/01/06/sap-cpi-step-by-step-scenario-for-beginners-http-to-email/

Step 8: After the Mail configuration deploy the iflow and copy the endpoint URL form the monitoring section.

Step 9: Now we need to send the image for that we use Postman tool and , create the request with endpoint taken from the CPI Monitoring.

In Authorization section use basic authorization type with username and password as ClientId and ClientSecret.

And in the Body paste the image encoded html URL.

Now click on the Send button.

And verify your mail is image is sent or not.

Method 2: In this method we encode the image and directly creating the attachment.

Step 1: Create the iflow with sender adapter as HTTP.

Step 2: Add the Base64 Encoder from palette and write the groovy script as below given.

import com.sap.gateway.ip.core.customdev.util.Message
import org.apache.camel.impl.DefaultAttachment
import javax.mail.util.ByteArrayDataSource

def Message processData(Message message) {
    
    // 1: initailizing the variable to get the body from sent message
    def body = message.getBody(String);

    // 2: Get the binary attachment content in the form of a byte array
    def imageBytes = body.decodeBase64()

    // 3: Construct a ByteArrayDataSource object with the byte array and the content's MIME type
    def dataSource = new ByteArrayDataSource(imageBytes, 'image/png')

    // 4: Construct a DefaultAttachment object
    def attachment = new DefaultAttachment(dataSource)

    // 5: Add the attachment to the message
    message.addAttachmentObject('image.png', attachment)
   
    return message

}

Step 3: After the groovy script, add the send element and connect to receiver via mail adapter ,follow the above mail configuration (step7 in method 1).

The only change in this method is we are directly giving the MIME type in the groovy while creating the attachment. So, no need to give name and the MIME type in the attachment section in the mail configuration, just we need to check the add message attachment in the mail configuration.

Method 3: In this method follow the same steps from steps 7 – 8 in method 1. Here the only change is MIME type “image/png”(here we can use the different formats of image like jpeg, jpg etc.)

After the above changes now test the iflow in postman as same as above postman test.

Now in the body change the body type to binary and upload the image directly and send the request.

And verify the mail.


文章来源: https://blogs.sap.com/2023/10/02/how-to-send-an-image-file-to-mail-using-sap-cpi/
如有侵权请联系:admin#unsafe.sh