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.
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.
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
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>