<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: processing Email using Inbound Email Adapter and Groovy Script in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812500#M4811316</link>
    <description>&lt;P&gt;Hello AR,&lt;/P&gt;&lt;P&gt;you can introduce some error handling and retries in your script, retry the below logic,&lt;/P&gt;&lt;P&gt;import com.sap.gateway.ip.core.customdev.util.Message

def MAX_RETRIES = 3

def Message processData(Message message) {
    def retryCount = 0

    while (retryCount &amp;lt; MAX_RETRIES) {
        try {
            processAttachments(message)
            return message
        } catch (Exception e) {
            retryCount++
            if (retryCount &amp;lt; MAX_RETRIES) {
                log.error("Error processing attachments. Retrying (Attempt $retryCount)")
            } else {
                log.error("Max retries reached. Unable to process attachments.")
                throw e
            }
        }
    }

    return message
}

def processAttachments(Message message) {
    def attachments = message.getAttachments()
    def bTypeSupported = false

    if (attachments != null &amp;amp;&amp;amp; !attachments.isEmpty()) {
        attachments.values().each { attachment -&amp;gt;
            if (bTypeSupported) {
                return
            }

            def sContentType = attachment.getContentType()
            message.setProperty("MailContentType", sContentType)
            sContentType = sContentType.substring(0, sContentType.lastIndexOf(";")).toLowerCase()

            if (sContentType.contains("csv")) {
                sContentType = "text/csv"
                bTypeSupported = true
            }

            if (bTypeSupported) {
                message.setProperty("MailContentTypeSupported", "True")
                message.setBody(attachment.getContent())
                message.setHeader("attachment_fileName", attachment.getName())
                message.setHeader("attachment_type", sContentType)
            }
        }
    }
}
&lt;/P&gt;&lt;P&gt;a MAX_RETRIES constant to specify the maximum number of retry attempts.&lt;/P&gt;&lt;P&gt;The processData function now includes a retry loop that attempts to process attachments. If an exception is caught, it will retry up to the maximum number of times defined by MAX_RETRIES.
The processAttachments function remains mostly the same for processing attachments.
This retry logic should help address the issue of attachments not being fully loaded or available on the first attempt. If there's an error during processing, the script will log the error and retry until the maximum number of retries is reached or the processing is successful.&lt;/P&gt;&lt;P&gt;Alternately you can check the below blog for configuring mail sender Adapter in Integration suite.&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.sap.com/2023/02/26/inbound-email-integration-from-o365-to-sap-s4-via-sap-cpi/" target="test_blank"&gt;https://blogs.sap.com/2023/02/26/inbound-email-integration-from-o365-to-sap-s4-via-sap-cpi/&lt;/A&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Lakshmi.&lt;/P&gt;</description>
    <pubDate>Fri, 22 Sep 2023 22:02:05 GMT</pubDate>
    <dc:creator>vishalakshmi</dc:creator>
    <dc:date>2023-09-22T22:02:05Z</dc:date>
    <item>
      <title>processing Email using Inbound Email Adapter and Groovy Script</title>
      <link>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaq-p/12812497</link>
      <description>&lt;P&gt;import com.sap.gateway.ip.core.customdev.util.Message&lt;/P&gt;
  &lt;P&gt;import java.util.Map&lt;/P&gt;
  &lt;P&gt;import java.util.Iterator&lt;/P&gt;
  &lt;P&gt;import javax.activation.DataHandler&lt;/P&gt;
  &lt;P&gt;def Message processData(Message message) {&lt;/P&gt;
  &lt;P&gt;def attachments = message.getAttachments();&lt;/P&gt;
  &lt;P&gt;def bTypeSupported = false;&lt;/P&gt;
  &lt;P&gt;if ((attachments!=null)&amp;amp;&amp;amp;(!attachments.isEmpty())) {&lt;/P&gt;
  &lt;P&gt;attachments.values().each{ attachment -&amp;gt;&lt;/P&gt;
  &lt;P&gt;if (bTypeSupported == true){&lt;/P&gt;
  &lt;P&gt;return;&lt;/P&gt;
  &lt;P&gt;}&lt;/P&gt;
  &lt;P&gt;def sContentType = attachment.getContentType()&lt;/P&gt;
  &lt;P&gt;message.setProperty("MailContentType", sContentType);&lt;/P&gt;
  &lt;P&gt;sContentType = sContentType.substring(0, sContentType.lastIndexOf(";")).toLowerCase();&lt;/P&gt;
  &lt;P&gt;if (sContentType.contains("csv")){&lt;/P&gt;
  &lt;P&gt;sContentType="text/csv";&lt;/P&gt;
  &lt;P&gt;bTypeSupported = true;&lt;/P&gt;
  &lt;P&gt;}&lt;/P&gt;
  &lt;P&gt;if (bTypeSupported == true){&lt;/P&gt;
  &lt;P&gt;message.setProperty("MailContentTypeSupported", "True");&lt;/P&gt;
  &lt;P&gt;message.setBody(attachment.getContent());&lt;/P&gt;
  &lt;P&gt;message.setHeader("attachment_fileName",attachment.getName());&lt;/P&gt;
  &lt;P&gt;message.setHeader("attachment_type",sContentType);&lt;/P&gt;
  &lt;P&gt;}&lt;/P&gt;
  &lt;P&gt;}&lt;/P&gt;
  &lt;P&gt;}&lt;/P&gt;
  &lt;P&gt;return message&lt;/P&gt;
  &lt;P&gt;}&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 12:06:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaq-p/12812497</guid>
      <dc:creator>amit023</dc:creator>
      <dc:date>2023-09-22T12:06:37Z</dc:date>
    </item>
    <item>
      <title>Re: processing Email using Inbound Email Adapter and Groovy Script</title>
      <link>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812498#M4811314</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;It would be easier to look into this, if you:&lt;/P&gt;&lt;P&gt;1) Used the CODE button to insert the script (without it, the formatting is all over the place)&lt;/P&gt;&lt;P&gt;2) Removed all commented-out code before posting&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Morten&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 12:30:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812498#M4811314</guid>
      <dc:creator>MortenWittrock</dc:creator>
      <dc:date>2023-09-22T12:30:42Z</dc:date>
    </item>
    <item>
      <title>Re: processing Email using Inbound Email Adapter and Groovy Script</title>
      <link>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812499#M4811315</link>
      <description>&lt;P&gt; Hi Morten thanks a lot for replying, &lt;/P&gt;&lt;P&gt;First attempt is always failing with this script and it is working fine only when we retry the script again to process the attachment in email.&lt;/P&gt;&lt;P&gt;Not sure if this is impacted due to groovy script or the Email adapter in SAP BTP.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 13:39:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812499#M4811315</guid>
      <dc:creator>amit023</dc:creator>
      <dc:date>2023-09-22T13:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: processing Email using Inbound Email Adapter and Groovy Script</title>
      <link>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812500#M4811316</link>
      <description>&lt;P&gt;Hello AR,&lt;/P&gt;&lt;P&gt;you can introduce some error handling and retries in your script, retry the below logic,&lt;/P&gt;&lt;P&gt;import com.sap.gateway.ip.core.customdev.util.Message

def MAX_RETRIES = 3

def Message processData(Message message) {
    def retryCount = 0

    while (retryCount &amp;lt; MAX_RETRIES) {
        try {
            processAttachments(message)
            return message
        } catch (Exception e) {
            retryCount++
            if (retryCount &amp;lt; MAX_RETRIES) {
                log.error("Error processing attachments. Retrying (Attempt $retryCount)")
            } else {
                log.error("Max retries reached. Unable to process attachments.")
                throw e
            }
        }
    }

    return message
}

def processAttachments(Message message) {
    def attachments = message.getAttachments()
    def bTypeSupported = false

    if (attachments != null &amp;amp;&amp;amp; !attachments.isEmpty()) {
        attachments.values().each { attachment -&amp;gt;
            if (bTypeSupported) {
                return
            }

            def sContentType = attachment.getContentType()
            message.setProperty("MailContentType", sContentType)
            sContentType = sContentType.substring(0, sContentType.lastIndexOf(";")).toLowerCase()

            if (sContentType.contains("csv")) {
                sContentType = "text/csv"
                bTypeSupported = true
            }

            if (bTypeSupported) {
                message.setProperty("MailContentTypeSupported", "True")
                message.setBody(attachment.getContent())
                message.setHeader("attachment_fileName", attachment.getName())
                message.setHeader("attachment_type", sContentType)
            }
        }
    }
}
&lt;/P&gt;&lt;P&gt;a MAX_RETRIES constant to specify the maximum number of retry attempts.&lt;/P&gt;&lt;P&gt;The processData function now includes a retry loop that attempts to process attachments. If an exception is caught, it will retry up to the maximum number of times defined by MAX_RETRIES.
The processAttachments function remains mostly the same for processing attachments.
This retry logic should help address the issue of attachments not being fully loaded or available on the first attempt. If there's an error during processing, the script will log the error and retry until the maximum number of retries is reached or the processing is successful.&lt;/P&gt;&lt;P&gt;Alternately you can check the below blog for configuring mail sender Adapter in Integration suite.&lt;/P&gt;&lt;P&gt;&lt;A href="https://blogs.sap.com/2023/02/26/inbound-email-integration-from-o365-to-sap-s4-via-sap-cpi/" target="test_blank"&gt;https://blogs.sap.com/2023/02/26/inbound-email-integration-from-o365-to-sap-s4-via-sap-cpi/&lt;/A&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Lakshmi.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Sep 2023 22:02:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812500#M4811316</guid>
      <dc:creator>vishalakshmi</dc:creator>
      <dc:date>2023-09-22T22:02:05Z</dc:date>
    </item>
    <item>
      <title>Re: processing Email using Inbound Email Adapter and Groovy Script</title>
      <link>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812501#M4811317</link>
      <description>&lt;P&gt;Did'nt helped.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2023 13:16:24 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812501#M4811317</guid>
      <dc:creator>amit023</dc:creator>
      <dc:date>2023-10-03T13:16:24Z</dc:date>
    </item>
    <item>
      <title>Re: processing Email using Inbound Email Adapter and Groovy Script</title>
      <link>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812502#M4811318</link>
      <description>&lt;P&gt;another problem is you cannot keep tracing enabled for longer duration and once again another pathetically designed adapter for handling things like attachment for which we have to take help from groovy script.&lt;/P&gt;</description>
      <pubDate>Tue, 03 Oct 2023 13:19:52 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/processing-email-using-inbound-email-adapter-and-groovy-script/qaa-p/12812502#M4811318</guid>
      <dc:creator>amit023</dc:creator>
      <dc:date>2023-10-03T13:19:52Z</dc:date>
    </item>
  </channel>
</rss>

