Friday, 17 August 2018

Leverage Microsoft Email Adapter to send email with attachment in Oracle Integration Cloud (OIC) (Part-2)

In the previous blog, we have showcased how to send email notification leveraging MS Email adapter. In this article, we'll see how to send email notification along with attachments.

Use Case: Send an email along with attachment

To achieve this use case, a REST service will be exposed which will take a file as an input parameter along with below payload and send the file as an attachment in the email notification to the recipient sent in the payload

{
"To":"recipients@example.com",
"Body":"Hi, Sending an email with attachment",
"Subject":"Attachment Testing"
}

Let's get started and see how to achieve the use case

Below are the high-level activities which need to perform  to achieve the use case
  • Configure MS Email adapter in Oracle Integration Cloud 
  • Create REST Service

Configure MS Email adapter in Oracle Integration Cloud

Follow the blog to see how to configure MS email adapter

While configuring the email adapter, we had provided https://outlook.office.com/Mail.ReadWrite offline_access as a scope. Since we are sending an email instead of reading email then scope must be changed. So in this case scope will be https://outlook.office.com/Mail.Send offline_access

Create REST Service
  • Create an Orchestration Integration with the option of Application event or  business object
  • Drop REST adapter on the Integration Canvas as Trigger point
  • Enter the values and Click Next button
    • Name of the end point: GetAttachments
    • Relative URI(must start with /): /sendEmail
    • Select HTTP verb: POST
    • Select Configure a request payload for this endpoint checkbox- This option allows us to assign payload in the next step
  • In the Next screen choose below option and select Next
    • Select Accept attachments from request checkbox
    • Select JSON Sample Radio button: This option allows us to take input in json format. Select inline link to give sample payload and enter below the payload
{
"To":"recipients@example.com",
"Body":"Hi, Sending an email with attachment",
"Subject":"Attachment Testing"
}

  • Click Done button on Next screen to complete the Wizard
  • Drop the MS email adapter just below the REST service adapter
  • Enter endpoint name and click Next button
  • Select Send a new message operation and click Next button
  • Click Done button on Next screen to complete the Wizard
  • Open the Mapper, expand Message object and map values in below attributes
Source
Target
Body
Body -> Content
To
ToRecipients -> EmailAddress -> Address
Subject
Subject

As per the Microsoft Document, to send an attachment in an email an Attachments object is required in the https://outlook.office.com/api/v2.0/me/sendmail API. When we open the ICS mapper we don't see this Attachments object. So we have to manually add the Attachments object by exporting the integration. 
  • Save the integration and click on the Export button

  • Open the SendAttachments_REQUEST.wsdl file in 7-zip directly which will be in below path. Highlighted number might be different
SENDATTACHMENTNOTIFICATION_01.00.0000.iar\icspackage\project\SENDATTACHMENTNOTIFICATION_01.00.0000\resources\application_15\inbound_16\resourcegroup_17\SendAttachments_REQUEST.wsdl
  • Search complex element of type "messageResource.definitions.messageResource" and add below element
<xs:element name="Attachments" minOccurs="0" maxOccurs="unbounded">
            <xs:complexType>
                        <xs:complexContent>
                                    <xs:extension base="messageResource.definitions.attachments">
                                                <xs:attribute name="odata.type" />
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>
  • Add below complex element in the WSDL file
<xs:complexType name="messageResource.definitions.attachments">
            <xs:annotation>
                        <xs:appinfo>
                                    <ns1:custom>false</ns1:custom>
         </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
                        <xs:element name="Name" type="xs:string" />
        <xs:element name="ContentBytes" type="xs:string" />
    </xs:sequence>
</xs:complexType>
  • Save the file and import the integration back in OIC
  • Open the mapper again and see in the Target mapper, Attachments object would be shown
  • Hard code the value "#Microsoft.OutlookServices.FileAttachment" of odata.type attribute
  • Map the Request attachmentproperties - partName to Attachments - Name
  • Open the ContentTypes attribute and drop the function encodeReferenceToBase64 on the attribute
  • Drop the attachments->attachment->attachmentReference in the  function  encodeReferenceToBase64 
  • Below would be the final mapping
  • Save the integration and Activate
  • Let's test the integration now. Open POSTMAN tool and hit the REST service URL
  • See the inbox, an email notification should reach with an attachment

11 comments:

  1. HI Ankur,

    I am getting 415 Unsupported Media Type Error on hitting the REST URL.
    Can you suggest me any reason for this.

    ReplyDelete
    Replies
    1. Hi,
      It look like you have added wrong Content-Type header. Please correct the same and send the request again.

      Regards,
      Ankur

      Delete
    2. What Contetnt -type should i use?

      Delete
    3. Hi Ankur,

      Still stuck. Can you help me in this.
      When i use Multipart/Form-data I am getting the following error.
      "exception occurred while translating the request from native to xml"

      Delete
    4. Hi Hemant,
      You can find out the content type by entering the service endpoint into the browser till /metadata.

      Can you send the exact error which you are getting from the ICS logs along with the IAR and request which you are trying to make.
      Regards,
      Ankur

      Delete
  2. Hi Ankur,

    I have followed all the steps. The error i am getting is
    OSB-380001: 403 :Application Error

    OK

    Could you pls give a pointer on this.

    Regards,
    Vivek

    ReplyDelete
  3. OSB-380001: 403 :Application Error

    OK

    ReplyDelete
  4. ErrorMessageSubmissionBlocked","message":"Cannot send mail. Follow the instructions in your Inbox to verify your account., WASCL UserAction verdict is not None. Actual verdict is HipSend, ShowTierUpgrade."}}]]> :Application Error

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete