Sender and Receiver Mail Adapter in SAP Cloud Platform Integration
Cloud Platform Integration provides Mail adapter that enables you to poll mails from specified mail inbox or send mail to desired mail address. Below table shows the protocols that can be used for mail adapter.
Adapter | Protocol |
Sender Mail Adapter | IMAP or POP3 |
Receiver Mail Adapter | SMTP |
This blog provides you a detailed setup required to poll a mail or trigger a mail from CPI.
First and foremost prerequisite is to have mail domain certificates and mail credentials to be deployed in the tenant. Sometimes it might be possible that the certificates may have been deployed in your tenant, please check whether a valid certificates have been deployed as described below.
Step 1: Go to your tenant WebUI operations view->Connectivity Tests or if you are using eclipse, go to Node explorer and right click on the Tenant->Test Outbound Connection.
Step 2: For Receiver Mail Adapter, click on SMTP and fill in the details as shown below. There is an option for validating the certificates, by enabling the checkbox Validate Server Certificate. On the right hand side, you can see the response providing connection status. If server certificates are invalid, you can download the certificates right here, by clicking on the Download.
Step 3: The sender mail adapter can download e-mails using IMAP or POP3 protocol and access the e-mail body content as well as attachments. Below are few differences between POP3 and IMAP protocol.
POP3 – Post Office Protocol |
IMAP – Internet Messaging Access Protocol |
You can use only one computer to check your email (no other devices) | You can use multiple computers and devices to check your email |
Your mails are stored on the computer that you use | Your mails are stored on the server |
Sent mail is stored locally on your PC, not on a mail server | Sent mail stays on the server so you can see it from any device. |
Connectivity tests with IMAP protocol
Connectivity tests with POP3 protocol
Let’s build an IFlow with a scenario to poll mails from an Inbox and extract the attachment of the mail and use XSLT mapping to format and send to another mail box.
IFlow
Components Used
SL. No. | COMPONENT | DESCRIPTION |
1 | Sender Channel | Mail Sender channel to poll mails from Inbox |
2 | Script | To fetch the attachment |
3 | XSLT Mapping | To format the data. |
4 | Receiver Channel | Mail Receiver channel to send mail |
-
Mail Sender Adapter
-
Script
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()
if (attachments.isEmpty()) {
throw new Exception ("No content in Attachment")
} else {
Iterator<DataHandler> it = attachments.values().iterator()
DataHandler attachment = it.next()
message.setBody(attachment.getContent())
}
return message
}
-
XSLT Mapping (optional)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Weather Data of
<xsl:value-of select= "concat(current/city/@name, ', ', current/city/country)"/>
</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Condition</th>
<th>Value</th>
</tr>
<xsl:for-each select="/current/*">
<tr>
<td> <xsl:value-of select="translate(substring(name(),1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:value-of select="substring(name(), 2)"/></td>
<td><xsl:value-of select="concat(@name, @value, ' ', @unit)"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
-
Mail Receiver Adapter.
Mail attributes:
- From: Its mandatory field but a dummy mail id can be provided over here. As there is no validation done on the mail credentials deployed as an artifact with the address provided in the From parameter.
- To: It’s mandatory, a valid mail address need to be provided. You can pass this parameter through headers or property (ex: ${header.<Name>} or ${property.<Name>})
- Mail Body: Specifies the text of an e-mail message..
- Body MIME-Type: Specifies the type of the message body. This type determines how the message is displayed by different user agents.
- Body Encoding: Specifies the character encoding (character set) of the message body. The content of the input message will be converted to this encoding, and any character that is not available will be replaced with a question mark (‘?’). To ensure that data is passed unmodified, select a Unicode encoding, for example, UTF-8.
- Name(under Attachments): Specifies the file name of the attachment.
- MIME Type(under Attachments): The Multipurpose Internet Mail Extensions (MIME) type specifies the data format of the e-mail. You can select from the following MIME types: Text/Plain, Text/CSV, Text/HTML, Application/XML, Application/JSON, and Application/Octet-Stream.
- Source: Specifies the source of the data. This can be either Body, meaning the body of the input message, or Header, meaning a header of the input message.
- Header Name: If the source is Header, this parameter specifies the name of the header that is attached.
- Add Message Attachments: Select this option to add all attachments contained in the message exchange to the e-mail.
Note: The parameters From, To, Cc, Bcc, Subject, and Mail Body as well as the attachment name, can be dynamically set at run time from message headers or content.
Once the IFlow is saved and deployed, the sender mail adapter tries to poll new mails. The mail received at receipt address looks as below.
Drawbacks
Well, with the access to the deployed mail credential artifact and to the tenant, it’s possible to send mails to anyone. Therefore, data contained in an e-mail is not reliable without further verification. If the data is too sensitive, its the role of the tenant administrator to provide authorizations to restricted personnel of the team. E-mails can contain malware, such as viruses or Trojan horses, they can cause damage to a receiver system, Since CPI is just acts as a middle ware to connect the systems, its the responsibility of the receiver system to have sufficient protection strategies.
Attachment used in the Mail.
<?xml version="1.0" encoding="UTF-8"?>
<current>
<city id="1277333" name="Bangalore">
<coord lon="77.6" lat="12.98"/>
<country>IN</country>
<sun rise="2017-06-06T00:22:28" set="2017-06-06T13:14:20"/>
</city>
<temperature value="303.61" min="303.15" max="304.15" unit="kelvin"/>
<humidity value="48" unit="%"/>
<pressure value="1011" unit="hPa"/>
<wind>
<speed value="4.1" name="Gentle Breeze"/>
<gusts/>
<direction value="290" code="WNW" name="West-northwest"/>
</wind>
<clouds value="40" name="scattered clouds"/>
<visibility value="10000"/>
<precipitation mode="no"/>
<weather number="802" value="scattered clouds" icon="03d"/>
<lastupdate value="2017-06-06T10:00:00"/>
</current>
Nice and informative article..!!
Hi Ajay ,
Will this work for multiple attachments in a single mail .??
Hi Hariharan,
Yes, Logically it should work.
Regards,
Ajay
Hi Ajay, Thanks for the blog. How I can attach an image in my mail body ? Can you please share the details ? Thanks, Pushkar
Hi Ajay,
I Created One iflow with Mail Receiver Adapter. I am getting Error like Could not Connect to Host . Here I am attaching the screenshots . Please check the same and give me the solution. I am waiting for your replay. I am using SAP CPI Cloud foundry Trial version.
Test
Host Error
Can you Please give me the solution.
Regards
Rajesh Kumar.P
Hi Rajesh,
Don't use your personal account to make calls to an endpoint. create a service key, and use its clientId and clientsecret values as the user name and password.
How to create a service key is described in this blog post by Appala Naidu Uppada: Self-Service Enablement of Cloud Integration Service on Cloud Foundry Environment
Amiya
Hi Ajay, is there a way to change the email body mime-type using a property. I have a requirement where some time email body is passed as a plain text and in some case an html document. If I select from the available option (text/html) then I get unpredictable results and text shows as one long time. If I select text/plain and pass html body, it shows html annotation in the body. I am passing the mime type in the message, but I cannot use it. I have an option to externalize it but then end with the same issue. I need to use the type in message passed to the iFlow to determine which mime-type to use.
Any help will be greatly appreciated.
Thanks
Athar
Thanks for the blog. Can you give an example how to retrieve the subject of the mail?
I can see the subject captured on the header when the mail adapter is called. However, I couldn't saved it as a variable. Do you save it into a variable in the content modifier step or groovy?
I tried both and no luck.
Hi,
probably the answer is too late for you, but just in case if somebody else has the same issue. You have to add the header "Subject" to the "Allowed Headers" field in the "Runtime Configuration". This basically acts like a whitelist of the header fields from the sender adapter that will be passed to the rest of the iflow.
BR
Patrick
Hi all!
Who knows how to get Message-Id after sending via SMTP?
In the send step, I see the Message-Id in header, but it is not available in the next step.
And I eat to work with Amazon SMS. Has anyone had experience with this?
May be too late but it might help others...
Hi,
You can use the standard header SAP_MessageProcessingLogID via the expression ${header.SAP_MessageProcessingLogID}...This will be available at all the times if you have given/allowed the response header.