Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

There are some requirements where you need to send file as an attachment to an Email with proper Email Body. Email attachment also should be named as required.

You will be wondering how to achieve this functionality with existing Email adapter options.Here I am providing the Generic Module code which renames the Email attachment with the file that you have attached in the File Adapter.

Below are the configurations that need to be done for achieving the functionality discussed.

File adapter Configurations:

Below are the configurations in the file adapter to attach the file which has to be attached in the mail Adapter.

Make sure you check the ASMA parameters for File Name

I am using Mail package for customising the Email Body.

In this case, I am not parsing my input message. So some dummy input Message type is used and fields are mapped with constants.You can populate the fields of subject, From, To Fields with the required information. If you want these fields to be populated at Run time and required to change time to time, use parameterised Message Mapping.

For the Field Content_Type, populate text/plain;charset="iso-8859-1"

Coming to main part of interface is Mail Adapter configuration,

Mail Adapter Configurations:

As I am using Mail package, I have checked Use Mail Package option. File was attached in the file adapter which needs to be attached in the Email, so check Keep Attachments option in the adapter configurations.

Make sure that you have checked the ASMA Parameters in the Email Adapter.

Now try to process the your file as an attachment, You will see Email as below with untitled.xml as an attachment instead of file name that was attached.

Now the question is how to change the attachment name as your input file name….Yes, it can be achieved. Please find the below module code for identifying your File attachment by the Mail adapter and rename the attachment with the file name.

Module Code:

/**

*

*/

package com.sap.emailattachment;

import java.rmi.RemoteException;

import java.util.StringTokenizer;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import javax.ejb.EJBException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;

import javax.ejb.TimedObject;

import javax.ejb.Timer;

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.aii.af.service.auditlog.Audit;

import com.sap.engine.interfaces.messaging.api.Message;

import com.sap.engine.interfaces.messaging.api.MessageKey;

import com.sap.engine.interfaces.messaging.api.Payload;

import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;

import com.sap.engine.interfaces.messaging.api.XMLPayload;

import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;

import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;

import com.sap.tc.logging.Location;

/**

* @author Madhav.Poosarla

*

*/

public class EmailattachBean implements SessionBean, Module  {

public static final String VERSION_ID ="$Id://tc/aii/30_REL/src/_adapters/_sample/java/user/module/NGMINB_validateBean.java#1 $";

           

            static final long serialVersionUID = 7435850550539048633L;

            String fileName = null;

            @SuppressWarnings("deprecation")

           

            public ModuleData process(ModuleContext moduleContext, ModuleData inputModuleData) throws ModuleException

            {

                        String SIGNATURE = "process(ModuleContext moduleContext, ModuleData inputModuleData)";

                        Location location = null;

                        AuditAccess audit = null;

                        Payload attName =null;

                        int attnum=0;

                        try {

                                    location = Location.getLocation(this.getClass().getName());

                                    }

                        catch (Exception t)

                        {

                                    t.printStackTrace();

                                    ModuleException me = new ModuleException("Unable to create trace location", t);

                                    throw me;

                        }

                                    Object obj = null;

                                    Message msg = null;

                       

                        MessageKey key = null;

                        try {

                                   

                                    obj = inputModuleData.getPrincipalData();

                                    msg = (Message) obj;

                                    key = new MessageKey(msg.getMessageId(), msg.getMessageDirection());

                                    audit = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();// creating object for audit log

                                    audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS, "EmailAttach: Module called");

                       

                                    fileName = msg.getMessageProperty("http://sap.com/xi/XI/System/File", "FileName");

                                   

                                    audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"RequestFileName:" + fileName);

                                    attnum=msg.countAttachments();

                                    audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"number of Attachment: " + attnum);

                                    attName = msg.getAttachment("%F");

                                   

                                    msg.removeAttachment("%F");

                                    attnum=msg.countAttachments();

                                    if(attName != null)

                                    {

                                    audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"Name of Attachment: " + attName.getName());

                                    }

                                                      else

                                    {

                                    audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"Attachment not found" + attName);

                                    }                   

                                    try

                                    {

                                                attName.setName(fileName);

                                                audit.addAuditLogEntry(key, AuditLogStatus.SUCCESS,"Attachment name is successfully changed to : " + attName.getName());

                                    }

                                    catch(Exception e)

                                    {

                                                audit.addAuditLogEntry(key, AuditLogStatus.ERROR,attName +": attachmentName is not matching");

                                                ModuleException me = new ModuleException(e);

                                                throw me;

                                    }        

                                  msg.addAttachment(attName);

                                               }

                                    catch (Exception e) {

                                    ModuleException me = new ModuleException(e);

                                    throw me; 

                                    }                      

                                    return inputModuleData;//sending original payload to call adapter.

        }

            /* (non-Javadoc)

             * @see javax.ejb.SessionBean#ejbActivate()

             */

            public void ejbActivate() throws EJBException, RemoteException {

                        // TODO Auto-generated method stub

            }

            /* (non-Javadoc)

             * @see javax.ejb.SessionBean#ejbPassivate()

             */

            public void ejbPassivate() throws EJBException, RemoteException {

                        // TODO Auto-generated method stub

            }

            /* (non-Javadoc)

             * @see javax.ejb.SessionBean#ejbRemove()

             */

            public void ejbRemove() throws EJBException, RemoteException {

                        // TODO Auto-generated method stub

            }

            /* (non-Javadoc)

             * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)

             */

            public void setSessionContext(SessionContext arg0) throws EJBException,

                                    RemoteException {

                        // TODO Auto-generated method stub

            }

            /* (non-Javadoc)

             * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)

             */

            public void ejbTimeout(Timer arg0) {

                        // TODO Auto-generated method stub

            }

            public void ejbCreate() throws javax.ejb.CreateException {         

            }}

Below is the module configuration and make sure that you are Message Transformation bean with content disposition parameter.

I don’t want to send my mail body with single line as below.Once you deploy the Module, process the file as attachment. Here I am processing a PDF with the file name 2bg52000000.pdf

Now Mail contains with the proper attachment of the file name that was sent.

In this scenario, I wish to enhance one more functionality which is Email Body with which is customised. I don’t want to send my mail body with single line as below.

For customising your Email Body, it needs a small UDF while populating the data to the Content tag in the Mail package.

In the Constant I am passing a String as below

**Automated Email-Dont Reply**,EmailBody,,ThankYou,Madhav

UDF nextLine which is used will identify the comma in the String and put it in the next line.

nextLine (UDF):

Now Email looks like as below!!!

12 Comments
Labels in this area