public static Map<String,Object> sendMailFromScreen(DispatchContext dctx,Map<String,? extends Object> rServiceContext){
  Map<String,Object> serviceContext=UtilMisc.makeMapWritable(rServiceContext);
  LocalDispatcher dispatcher=dctx.getDispatcher();
  String webSiteId=(String)serviceContext.remove("webSiteId");
  String bodyText=(String)serviceContext.remove("bodyText");
  String bodyScreenUri=(String)serviceContext.remove("bodyScreenUri");
  String xslfoAttachScreenLocationParam=(String)serviceContext.remove("xslfoAttachScreenLocation");
  String attachmentNameParam=(String)serviceContext.remove("attachmentName");
  List<String> xslfoAttachScreenLocationListParam=UtilGenerics.checkList(serviceContext.remove("xslfoAttachScreenLocationList"));
  List<String> attachmentNameListParam=UtilGenerics.checkList(serviceContext.remove("attachmentNameList"));
  List<String> xslfoAttachScreenLocationList=new LinkedList<String>();
  List<String> attachmentNameList=new LinkedList<String>();
  if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationParam))   xslfoAttachScreenLocationList.add(xslfoAttachScreenLocationParam);
  if (UtilValidate.isNotEmpty(attachmentNameParam))   attachmentNameList.add(attachmentNameParam);
  if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationListParam))   xslfoAttachScreenLocationList.addAll(xslfoAttachScreenLocationListParam);
  if (UtilValidate.isNotEmpty(attachmentNameListParam))   attachmentNameList.addAll(attachmentNameListParam);
  Locale locale=(Locale)serviceContext.get("locale");
  Map<String,Object> bodyParameters=UtilGenerics.checkMap(serviceContext.remove("bodyParameters"));
  if (bodyParameters == null) {
    bodyParameters=MapStack.create();
  }
  if (!bodyParameters.containsKey("locale")) {
    bodyParameters.put("locale",locale);
  }
 else {
    locale=(Locale)bodyParameters.get("locale");
  }
  String partyId=(String)serviceContext.get("partyId");
  if (partyId == null) {
    partyId=(String)bodyParameters.get("partyId");
  }
  String orderId=(String)bodyParameters.get("orderId");
  String custRequestId=(String)bodyParameters.get("custRequestId");
  bodyParameters.put("communicationEventId",serviceContext.get("communicationEventId"));
  NotificationServices.setBaseUrl(dctx.getDelegator(),webSiteId,bodyParameters);
  String contentType=(String)serviceContext.remove("contentType");
  StringWriter bodyWriter=new StringWriter();
  MapStack<String> screenContext=MapStack.create();
  screenContext.put("locale",locale);
  ScreenRenderer screens=new ScreenRenderer(bodyWriter,screenContext,htmlScreenRenderer);
  screens.populateContextForService(dctx,bodyParameters);
  screenContext.putAll(bodyParameters);
  if (bodyScreenUri != null) {
    try {
      screens.render(bodyScreenUri);
    }
 catch (    GeneralException e) {
      Debug.logError(e,"Error rendering screen for email: " + e.toString(),module);
      return ServiceUtil.returnError(UtilProperties.getMessage(resource,"CommonEmailSendRenderingScreenEmailError",UtilMisc.toMap("errorString",e.toString()),locale));
    }
catch (    IOException e) {
      Debug.logError(e,"Error rendering screen for email: " + e.toString(),module);
      return ServiceUtil.returnError(UtilProperties.getMessage(resource,"CommonEmailSendRenderingScreenEmailError",UtilMisc.toMap("errorString",e.toString()),locale));
    }
catch (    SAXException e) {
      Debug.logError(e,"Error rendering screen for email: " + e.toString(),module);
      return ServiceUtil.returnError(UtilProperties.getMessage(resource,"CommonEmailSendRenderingScreenEmailError",UtilMisc.toMap("errorString",e.toString()),locale));
    }
catch (    ParserConfigurationException e) {
      Debug.logError(e,"Error rendering screen for email: " + e.toString(),module);
      return ServiceUtil.returnError(UtilProperties.getMessage(resource,"CommonEmailSendRenderingScreenEmailError",UtilMisc.toMap("errorString",e.toString()),locale));
    }
  }
  boolean isMultiPart=false;
  if (UtilValidate.isNotEmpty(xslfoAttachScreenLocationList)) {
    List<Map<String,? extends Object>> bodyParts=new LinkedList<Map<String,? extends Object>>();
    if (bodyText != null) {
      bodyText=FlexibleStringExpander.expandString(bodyText,screenContext,locale);
      bodyParts.add(UtilMisc.<String,Object>toMap("content",bodyText,"type","text/html"));
    }
 else {
      bodyParts.add(UtilMisc.<String,Object>toMap("content",bodyWriter.toString(),"type","text/html"));
    }
    for (int i=0; i < xslfoAttachScreenLocationList.size(); i++) {
      String xslfoAttachScreenLocation=xslfoAttachScreenLocationList.get(i);
      String attachmentName="Details.pdf";
      if (UtilValidate.isNotEmpty(attachmentNameList) && attachmentNameList.size() >= i) {
        attachmentName=attachmentNameList.get(i);
      }
      isMultiPart=true;
      try {
        Writer writer=new StringWriter();
        MapStack<String> screenContextAtt=MapStack.create();
        ScreenRenderer screensAtt=new ScreenRenderer(writer,screenContext,foScreenRenderer);
        screensAtt.populateContextForService(dctx,bodyParameters);
        screenContextAtt.putAll(bodyParameters);
        screensAtt.render(xslfoAttachScreenLocation);
        StreamSource src=new StreamSource(new StringReader(writer.toString()));
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        Fop fop=ApacheFopWorker.createFopInstance(baos,MimeConstants.MIME_PDF);
        ApacheFopWorker.transform(src,null,fop);
        baos.flush();
        baos.close();
        bodyParts.add(UtilMisc.<String,Object>toMap("content",baos.toByteArray(),"type","application/pdf","filename",attachmentName));
      }
 catch (      Exception e) {
        Debug.logError(e,"Error rendering PDF attachment for email: " + e.toString(),module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resource,"CommonEmailSendRenderingScreenPdfError",UtilMisc.toMap("errorString",e.toString()),locale));
      }
      serviceContext.put("bodyParts",bodyParts);
    }
  }
 else {
    isMultiPart=false;
    if (bodyText != null) {
      bodyText=FlexibleStringExpander.expandString(bodyText,screenContext,locale);
      serviceContext.put("body",bodyText);
    }
 else {
      serviceContext.put("body",bodyWriter.toString());
    }
    if (contentType != null && contentType.equalsIgnoreCase("text/plain")) {
      serviceContext.put("contentType","text/plain");
    }
 else {
      serviceContext.put("contentType","text/html");
    }
  }
  String subject=(String)serviceContext.remove("subject");
  subject=FlexibleStringExpander.expandString(subject,screenContext,locale);
  Debug.logInfo("Expanded email subject to: " + subject,module);
  serviceContext.put("subject",subject);
  serviceContext.put("partyId",partyId);
  if (UtilValidate.isNotEmpty(orderId)) {
    serviceContext.put("orderId",orderId);
  }
  if (UtilValidate.isNotEmpty(custRequestId)) {
    serviceContext.put("custRequestId",custRequestId);
  }
  if (Debug.verboseOn())   Debug.logVerbose("sendMailFromScreen sendMail context: " + serviceContext,module);
  Map<String,Object> result=ServiceUtil.returnSuccess();
  Map<String,Object> sendMailResult;
  Boolean hideInLog=(Boolean)serviceContext.get("hideInLog");
  hideInLog=hideInLog == null ? false : hideInLog;
  try {
    if (!hideInLog) {
      if (isMultiPart) {
        sendMailResult=dispatcher.runSync("sendMailMultiPart",serviceContext);
      }
 else {
        sendMailResult=dispatcher.runSync("sendMail",serviceContext);
      }
    }
 else {
      if (isMultiPart) {
        sendMailResult=dispatcher.runSync("sendMailMultiPartHiddenInLog",serviceContext);
      }
 else {
        sendMailResult=dispatcher.runSync("sendMailHiddenInLog",serviceContext);
      }
    }
  }
 catch (  Exception e) {
    Debug.logError(e,"Error send email:" + e.toString(),module);
    return ServiceUtil.returnError(UtilProperties.getMessage(resource,"CommonEmailSendError",UtilMisc.toMap("errorString",e.toString()),locale));
  }
  if (ServiceUtil.isError(sendMailResult)) {
    return ServiceUtil.returnError(ServiceUtil.getErrorMessage(sendMailResult));
  }
  result.put("messageWrapper",sendMailResult.get("messageWrapper"));
  result.put("body",bodyWriter.toString());
  result.put("subject",subject);
  result.put("communicationEventId",sendMailResult.get("communicationEventId"));
  if (UtilValidate.isNotEmpty(orderId)) {
    result.put("orderId",orderId);
  }
  if (UtilValidate.isNotEmpty(custRequestId)) {
    result.put("custRequestId",custRequestId);
  }
  return result;
}

public static Map < String , Object > sendMailFromScreen ( DispatchContext dctx , Map < String , ? extends Object > rServiceContext ) { Map < String , Object > serviceContext = UtilMisc . makeMapWritable ( rServiceContext ) ; LocalDispatcher dispatcher = dctx . getDispatcher ( ) ; String webSiteId = ( String ) serviceContext . remove ( "webSiteId" ) ; String bodyText = ( String ) serviceContext . remove ( "bodyText" ) ; String bodyScreenUri = ( String ) serviceContext . remove ( "bodyScreenUri" ) ; String xslfoAttachScreenLocationParam = ( String ) serviceContext . remove ( "xslfoAttachScreenLocation" ) ; String attachmentNameParam = ( String ) serviceContext . remove ( "attachmentName" ) ; List < String > xslfoAttachScreenLocationListParam = UtilGenerics . checkList ( serviceContext . remove ( "xslfoAttachScreenLocationList" ) ) ; List < String > attachmentNameListParam = UtilGenerics . checkList ( serviceContext . remove ( "attachmentNameList" ) ) ; List < String > xslfoAttachScreenLocationList = new LinkedList < String > ( ) ; List < String > attachmentNameList = new LinkedList < String > ( ) ; if ( UtilValidate . isNotEmpty ( xslfoAttachScreenLocationParam ) ) xslfoAttachScreenLocationList . add ( xslfoAttachScreenLocationParam ) ; if ( UtilValidate . isNotEmpty ( attachmentNameParam ) ) attachmentNameList . add ( attachmentNameParam ) ; if ( UtilValidate . isNotEmpty ( xslfoAttachScreenLocationListParam ) ) xslfoAttachScreenLocationList . addAll ( xslfoAttachScreenLocationListParam ) ; if ( UtilValidate . isNotEmpty ( attachmentNameListParam ) ) attachmentNameList . addAll ( attachmentNameListParam ) ; Locale locale = ( Locale ) serviceContext . get ( "locale" ) ; Map < String , Object > bodyParameters = UtilGenerics . checkMap ( serviceContext . remove ( "bodyParameters" ) ) ; if ( bodyParameters == null ) { bodyParameters = MapStack . create ( ) ; } if ( ! bodyParameters . containsKey ( "locale" ) ) { bodyParameters . put ( "locale" , locale ) ; } else { locale = ( Locale ) bodyParameters . get ( "locale" ) ; } String partyId = ( String ) serviceContext . get ( "partyId" ) ; if ( partyId == null ) { partyId = ( String ) bodyParameters . get ( "partyId" ) ; } String orderId = ( String ) bodyParameters . get ( "orderId" ) ; String custRequestId = ( String ) bodyParameters . get ( "custRequestId" ) ; bodyParameters . put ( "communicationEventId" , serviceContext . get ( "communicationEventId" ) ) ; NotificationServices . setBaseUrl ( dctx . getDelegator ( ) , webSiteId , bodyParameters ) ; String contentType = ( String ) serviceContext . remove ( "contentType" ) ; StringWriter bodyWriter = new StringWriter ( ) ; MapStack < String > screenContext = MapStack . create ( ) ; screenContext . put ( "locale" , locale ) ; ScreenRenderer screens = new ScreenRenderer ( bodyWriter , screenContext , htmlScreenRenderer ) ; screens . populateContextForService ( dctx , bodyParameters ) ; screenContext . putAll ( bodyParameters ) ; if ( bodyScreenUri != null ) { try { screens . render ( bodyScreenUri ) ; } catch ( GeneralException e ) { Debug . logError ( e , "Error rendering screen for email: " + e . toString ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource , "CommonEmailSendRenderingScreenEmailError" , UtilMisc . toMap ( "errorString" , e . toString ( ) ) , locale ) ) ; } catch ( IOException e ) { Debug . logError ( e , "Error rendering screen for email: " + e . toString ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource , "CommonEmailSendRenderingScreenEmailError" , UtilMisc . toMap ( "errorString" , e . toString ( ) ) , locale ) ) ; } catch ( SAXException e ) { Debug . logError ( e , "Error rendering screen for email: " + e . toString ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource , "CommonEmailSendRenderingScreenEmailError" , UtilMisc . toMap ( "errorString" , e . toString ( ) ) , locale ) ) ; } catch ( ParserConfigurationException e ) { Debug . logError ( e , "Error rendering screen for email: " + e . toString ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource , "CommonEmailSendRenderingScreenEmailError" , UtilMisc . toMap ( "errorString" , e . toString ( ) ) , locale ) ) ; } } boolean isMultiPart = false ; if ( UtilValidate . isNotEmpty ( xslfoAttachScreenLocationList ) ) { List < Map < String , ? extends Object > > bodyParts = new LinkedList < Map < String , ? extends Object > > ( ) ; if ( bodyText != null ) { bodyText = FlexibleStringExpander . expandString ( bodyText , screenContext , locale ) ; bodyParts . add ( UtilMisc . < String , Object > toMap ( "content" , bodyText , "type" , "text/html" ) ) ; } else { bodyParts . add ( UtilMisc . < String , Object > toMap ( "content" , bodyWriter . toString ( ) , "type" , "text/html" ) ) ; } for ( int i = 0 ; i < xslfoAttachScreenLocationList . size ( ) ; i ++ ) { String xslfoAttachScreenLocation = xslfoAttachScreenLocationList . get ( i ) ; String attachmentName = "Details.pdf" ; if ( UtilValidate . isNotEmpty ( attachmentNameList ) && attachmentNameList . size ( ) >= i ) { attachmentName = attachmentNameList . get ( i ) ; } isMultiPart = true ; try { Writer writer = new StringWriter ( ) ; MapStack < String > screenContextAtt = MapStack . create ( ) ; ScreenRenderer screensAtt = new ScreenRenderer ( writer , screenContext , foScreenRenderer ) ; screensAtt . populateContextForService ( dctx , bodyParameters ) ; screenContextAtt . putAll ( bodyParameters ) ; screensAtt . render ( xslfoAttachScreenLocation ) ; StreamSource src = new StreamSource ( new StringReader ( writer . toString ( ) ) ) ; ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; Fop fop = ApacheFopWorker . createFopInstance ( baos , MimeConstants . MIME_PDF ) ; ApacheFopWorker . transform ( src , null , fop ) ; baos . flush ( ) ; baos . close ( ) ; bodyParts . add ( UtilMisc . < String , Object > toMap ( "content" , baos . toByteArray ( ) , "type" , "application/pdf" , "filename" , attachmentName ) ) ; } catch ( Exception e ) { Debug . logError ( e , "Error rendering PDF attachment for email: " + e . toString ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource , "CommonEmailSendRenderingScreenPdfError" , UtilMisc . toMap ( "errorString" , e . toString ( ) ) , locale ) ) ; } serviceContext . put ( "bodyParts" , bodyParts ) ; } } else { isMultiPart = false ; if ( bodyText != null ) { bodyText = FlexibleStringExpander . expandString ( bodyText , screenContext , locale ) ; serviceContext . put ( "body" , bodyText ) ; } else { serviceContext . put ( "body" , bodyWriter . toString ( ) ) ; } if ( contentType != null && contentType . equalsIgnoreCase ( "text/plain" ) ) { serviceContext . put ( "contentType" , "text/plain" ) ; } else { serviceContext . put ( "contentType" , "text/html" ) ; } } String subject = ( String ) serviceContext . remove ( "subject" ) ; subject = FlexibleStringExpander . expandString ( subject , screenContext , locale ) ; Debug . logInfo ( "Expanded email subject to: " + subject , module ) ; serviceContext . put ( "subject" , subject ) ; serviceContext . put ( "partyId" , partyId ) ; if ( UtilValidate . isNotEmpty ( orderId ) ) { serviceContext . put ( "orderId" , orderId ) ; } if ( UtilValidate . isNotEmpty ( custRequestId ) ) { serviceContext . put ( "custRequestId" , custRequestId ) ; } if ( Debug . verboseOn ( ) ) Debug . logVerbose ( "sendMailFromScreen sendMail context: " + serviceContext , module ) ; Map < String , Object > result = ServiceUtil . returnSuccess ( ) ; Map < String , Object > sendMailResult ; Boolean hideInLog = ( Boolean ) serviceContext . get ( "hideInLog" ) ; hideInLog = hideInLog == null ? false : hideInLog ; try { if ( ! hideInLog ) { if ( isMultiPart ) { sendMailResult = dispatcher . runSync ( "sendMailMultiPart" , serviceContext ) ; } else { sendMailResult = dispatcher . runSync ( "sendMail" , serviceContext ) ; } } else { if ( isMultiPart ) { sendMailResult = dispatcher . runSync ( "sendMailMultiPartHiddenInLog" , serviceContext ) ; } else { sendMailResult = dispatcher . runSync ( "sendMailHiddenInLog" , serviceContext ) ; } } } catch ( Exception e ) { Debug . logError ( e , "Error send email:" + e . toString ( ) , module ) ; return ServiceUtil . returnError ( UtilProperties . getMessage ( resource , "CommonEmailSendError" , UtilMisc . toMap ( "errorString" , e . toString ( ) ) , locale ) ) ; } if ( ServiceUtil . isError ( sendMailResult ) ) { return ServiceUtil . returnError ( ServiceUtil . getErrorMessage ( sendMailResult ) ) ; } result . put ( "messageWrapper" , sendMailResult . get ( "messageWrapper" ) ) ; result . put ( "body" , bodyWriter . toString ( ) ) ; result . put ( "subject" , subject ) ; result . put ( "communicationEventId" , sendMailResult . get ( "communicationEventId" ) ) ; if ( UtilValidate . isNotEmpty ( orderId ) ) { result . put ( "orderId" , orderId ) ; } if ( UtilValidate . isNotEmpty ( custRequestId ) ) { result . put ( "custRequestId" , custRequestId ) ; } return result ; }
javamail service that gets body content from a screen widget defined in the product store record and if available as attachment also .
