cancel
Showing results for 
Search instead for 
Did you mean: 

File to Mail Scenario

former_member192181
Participant
0 Kudos

Hi All,

My Scenario is , I need to pick the file from SFTP location and send the file as attachment to the receiver mail.

Please provide the steps to send the source file as attachment.

Thanks and Regards,

Lalitha

Accepted Solutions (0)

Answers (4)

Answers (4)

monikandan_p
Active Participant
0 Kudos

Hi Lailtha,

   Kindly follow the below link for dynamic name in mail.

Dynamic name in the mail attachment :

https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/3202

Also,

sending emails with attachment with help of Java mapping

https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/6321

Best Regards,

Monikandan

Muniyappan
Active Contributor
0 Kudos
former_member192181
Participant
0 Kudos

Hi Muniyappan,

Thanks for the link provided. But in that link, I observe that they are providing the filename as "file1".

In my scenario everytime file name will be different, then how to handle swap.keyvalue in module configuration??

Thanks,

Lalitha

Muniyappan
Active Contributor
0 Kudos

Hi Lalitha,

can you check this docuement?

this docuemnt results look good but i did not try this.

http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/603a7bd8-a6a0-2e10-388b-fc9cb9dae6c4&override...

Regards,

Muniyappan.

iaki_vila
Active Contributor
0 Kudos

Hi Lalitha,

Depending of your PI version the SFTP protocol is available wiith PI (7.3x and above) or you need to purchase it.

The step are the next:

- Configure the SFTP sender adapter like this document: http://scn.sap.com/docs/DOC-35572

- Configure the Mail receiver like this document: http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/a0a27c01-26b0-2c10-bd8a-94498efa7...

If you have special requeriments like to deal with atachments, please write them.

Regards.

former_member192181
Participant
0 Kudos

Hi Inaki,

My problem is not with SFTP , Iam using it from long time.

Whatever file I pick from directory had to go as a attachment to a mail. Something like a passthrough scenario.

Thanks,

Lalitha

Former Member
0 Kudos

Hi Lalitha

Use Mail package for receiver mail adapter. You can easily get the XSD for mail package in SCN.

Then create an message mapping or java mapping and map the target field 'Content' with the data that you want to send as attachment.

receiver mail adapter setting will be like below

You can also check this blog if you don't want to use mail package

http://scn.sap.com/community/pi-and-soa-middleware/blog/2005/11/23/xi-html-e-mails-from-the-receiver...

former_member192181
Participant
0 Kudos

Hi Indrajit,

I dont want to manipulate the Text File. It is something like a pass through scenario.

So, The File which I pick from directory had to go as attachment.

Do I have to use any modules??

Thanks,

Lalitha

Former Member
0 Kudos

Hi Lalitha

Yes you need to create an adapter module which will set the content of the text file as attachment.

Then you need to use mail adapter with option 'keep attachment'.

Check the module code below which is also setting the file content as attachment


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.Local;
import javax.ejb.LocalHome;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;

import com.sap.aii.af.lib.mp.module.Module;
import com.sap.aii.af.lib.mp.module.ModuleContext;
import com.sap.aii.af.lib.mp.module.ModuleData;
import com.sap.aii.af.lib.mp.module.ModuleException;
import com.sap.engine.interfaces.messaging.api.Message;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.MessagePropertyKey;
import com.sap.engine.interfaces.messaging.api.Payload;
import com.sap.engine.interfaces.messaging.api.PublicAPIAccess;
import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
import com.sap.engine.interfaces.messaging.api.exception.MessagingException;

/**
* @author Vinay Puttaswamy
*
*/

@Stateless(name="FileModule")
@LocalHome(value=com.sap.aii.af.lib.mp.module.ModuleLocalHome.class)
@Local(com.sap.aii.af.lib.mp.module.ModuleLocal.class)

public class FileModule implements SessionBean, Module {

private SessionContext myContext;

public void ejbRemove() {
}

public void ejbActivate() {
}

public void ejbPassivate() {
}

public void setSessionContext(SessionContext context) {
  myContext = context;
}

public void ejbCreate() throws CreateException {
}

public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData)
        throws ModuleException
{
  Message msg = null;
  String rootDir = null;
  String triggerFileName = null;
  String FileKey = null;
 
 
  try
  {
   msg = (Message)inputModuleData.getPrincipalData(); 
  
   MessageKey amk = new MessageKey(msg.getMessageId(), msg.getMessageDirection());
   PublicAPIAccess pa;
   try
   {
    pa = PublicAPIAccessFactory.getPublicAPIAccess();
   }
   catch (MessagingException me)
   {
     throw new ModuleException("Internal SAP PI 7.31 Error -> Accessing PublicAPIAccessFactory Failed!");    
   }
   AuditAccess audit = pa.getAuditAccess();              
  
   audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS,"ReadFiles: MODULE called");
  
   MessagePropertyKey msgFileName = new MessagePropertyKey("FileName", "http://sap.com/xi/XI/System/File");
  
   //Read Trigger File Name
   triggerFileName = msg.getMessageProperty(msgFileName);  
   audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS,"Trigger File Name -> " + triggerFileName);     
  
   //Get the first 4 characters to get the unique number.This will be used to retrieve the Main file from the NFS folder
   FileKey = triggerFileName.substring(0,4);
   audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS,"File Key -> " + FileKey);  
  
  
   //Read the root directory entered in Custom Parameters
   rootDir = moduleContext.getContextData("rootDir");
  
   audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Trigger file is being read");
   audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Connected to Root Directory -> " + rootDir);   
   
   
  
    File folder = new File(rootDir);
    File[] listOfFiles = folder.listFiles();
    String[] fileNames;

     //Get the list of files present in root directory
     for (int k = 0; k <listOfFiles.length; k++)
     {    
      String nfsFileName = listOfFiles[k].getName();
      if(nfsFileName.startsWith(rootDir))
       nfsFileName = listOfFiles[k].getName().substring(rootDir.length());
     
      if(nfsFileName.startsWith(FileKey)&& nfsFileName.endsWith("trigger.txt"))
      {
       String triggerfilepath = rootDir+nfsFileName;
       File triggerfile = new File(triggerfilepath);
       triggerfile.delete();
       audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Deleting the Trigger file from root dir-> " + nfsFileName);
      }
     
      //Check if filename begins with the Unique Key value
      if(nfsFileName.startsWith(FileKey)&& nfsFileName.endsWith("main.txt"))
      {  
       
       audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Main File -> " + nfsFileName);
             
       //Reading the contents of the Main file from the NFS folder     
       byte[] ftpFileDocContent = null;
       audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Main File Reading Started -> " + nfsFileName);
       String mainfilepath = rootDir+nfsFileName;
       File mainfile = new File(mainfilepath);
       BufferedReader br = new BufferedReader(new FileReader(mainfilepath));
       StringBuilder sb = null;
       String line = null;
        String everything = null;
       try
       {
       
        sb = new StringBuilder();
              line = br.readLine();

              while (line != null) {
                  sb.append(line);
                 
                  sb.append(System.getProperty("line.separator"));
                  line = br.readLine();
              }
              everything = sb.toString();

       }
      
       finally{
       
        br.close();

       
       }
       audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Main File Reading Completed -> " + nfsFileName);
      
       mainfile.delete();
       audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "Deleting the Main file from root dir-> " + nfsFileName);
      
      
     
  
       ftpFileDocContent = everything.getBytes();
      
        //Create Main File as an attachment
        Payload ftpFilePayload = msg.createPayload();
        ftpFilePayload.setName(nfsFileName);       
        ftpFilePayload.setContentType("text/plain;charset = \"UTF-8\"");
        ftpFilePayload.setContent(ftpFileDocContent); 
        ftpFilePayload.setDescription(nfsFileName); 

        msg.addAttachment(ftpFilePayload);
        audit.addAuditLogEntry(amk,AuditLogStatus.SUCCESS, "File -> " + nfsFileName + " attachment created successfully");        
      
       
       
      }//End IF for checking filename begins with Unique Key
     }// End For Loop      
    
     audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Number of attachments processed -> " + msg.countAttachments() + ", for Unique Key -> " + FileKey);
   
    
     // Get PI Main Payload
     Payload xiMsgPayload = msg.getDocument();
    
     //Update Main Payload Data
     String xiMsgPayloadStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><ns0:MT_FuelAct xmlns:ns0=\""+"Hi"+""+"Hi"+"http://mpc.com/373221\"><Records><field1>"+"Hi"+"</field1><field2>"+"Hi"+"</field2><...>";
     xiMsgPayload.setContent(xiMsgPayloadStr.getBytes());    
     audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS, "Payload updated");
    
     inputModuleData.setPrincipalData(msg);    
      
  }

  catch (Exception e)
  {     
   Message currentMsg = (Message) inputModuleData.getPrincipalData();
   MessageKey mk = new MessageKey(currentMsg.getMessageId(), currentMsg.getMessageDirection());
   ModuleException me = new ModuleException(new MessagingException(mk, e.getMessage()));           
   throw me;
  }
  return inputModuleData;
}


 
}