cancel
Showing results for 
Search instead for 
Did you mean: 

Mail to File Scenario

Former Member
0 Kudos

Hi All,

I am working on a Mail to File scenario. Where The mail is sent with a single attachment. We take this attachment and store it in PI application server for further processing. The scenario is working fine.

Now we need to build some validation in it:-

(1) If the attachment is of content type other that "text" like image or excel, we need to reply back to sender mentioning invalid attachment. How do we validate the content type ?

(2) If we receive multiple attachment, the processing should not happen and need to reply back to sender to send only one attachment at a time.

I have gone through some blogs / forums but didn't much info.

Please advice.

Thanks,

Navneeth K.

Accepted Solutions (0)

Answers (1)

Answers (1)

RaghuVamseedhar
Active Contributor
0 Kudos

Hi Naveeth,

You need to use PI7.1 Java API. InputAttachments [Link1|http://help.sap.com/javadocs/pi/SP3/xpi/com/sap/aii/mapping/api/InputAttachments.html] and Attachment [Link2|http://help.sap.com/javadocs/pi/SP3/xpi/com/sap/aii/mapping/api/Attachment.html].

Solution: Mail to File scenario with Java Mapping. Java Mapping can read all attachments. Input and output structures for mapping will be dummy structure.

Logic in Java Mapping: 1. If attachment name ends with other than .txt, trigger mail, use getContentType() method. 2. If there are more than one attachment, trigger mail, use areAttachmentsAvailable() method to count attachments. 3. Else, copy input to output (success, it is a single text file).

Regards,

Raghu_Vamsee

Former Member
0 Kudos

Thanks Raghu for the reply.

I just went through this blog:-

/people/yugapreetha.t/blog/2009/08/24/read-the-attachments-of-the-input-xi-message-in-the-message-mapping

Is this what you want me to implement?

I can also use a graphical mapping with dummy structures right ?? and use the UDF mentioned in the blog.

Please check and advice.

RaghuVamseedhar
Active Contributor
0 Kudos

Hi Navneeth,

You cannot use graphical mapping, as your output (in success case) is a text file, not a XML. So, use Java Mapping, use dummy source and target message type.

The UDF in the blog, you have pointed out, should be used in Java Mapping. Use JavaMail API [Link|http://en.wikipedia.org/wiki/Javamail] to trigger mail.

Please note, I have not tried out this below JavaMapping code or JavaMail API, please do your research before implementing.

package com.test;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.InputAttachments;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;

public class TriggerMail_ProtoType extends AbstractTransformation {

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
            throws StreamTransformationException {
        try {
            InputStream inputstream = transformationInput.getInputPayload().getInputStream();
            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();

            byte[] b = new byte[inputstream.available()];
            inputstream.read(b);
            String strContent = new String(b);


            InputAttachments inputAttachments = transformationInput.getInputAttachments();
            Collection <String> strContentIds = inputAttachments.getAllContentIds(true);
            int iCount = 0;
            String strMailText = "";
            for (String sContentID : strContentIds) {
                //First attachment
                if(iCount==0)
                {
                                 if(!inputAttachments.getAttachment(sContentID).getContentType().endsWith(".txt"))
                                 {
                                     //Trigger mail... attachment is not text.
                                     // You need to read "strContent" to get sender mail ID.
                                     strMailText = "Mail was triggered as,attachment is not text. ";
                                 }else{
                                     strMailText = inputAttachments.getAttachment(sContentID).getContent().toString();
                                 }
                }else{
                    //Trigger mail... There are more than one attachment.
                    // You need to read "strContent" to get sender mail ID.
                    strMailText = "Mail was triggered as,There are more than one attachment ";
                }
                iCount++;
            }

            //Write strMailText, to output.
            outputstream.write(strMailText.getBytes());

        } catch (Exception exception) {
            getTrace().addDebugMessage(exception.toString());
        }
    }
}

Regards,

Raghu_Vamsee

Former Member
0 Kudos

Hi,

You should be able to implement this with a GUI mapping using dummy structures.

When you are defining your outbound and inbound service interfaces, for Interface pattern, choose option "Stateless XI30(Compatible)". This will ensure that even if the payload is text, Integration Engine will not try to validate the payload and throw a Unparsable exception.

Regards