Send an email via GEL using java method
search cancel

Send an email via GEL using java method

book

Article ID: 232943

calendar_today

Updated On:

Products

Clarity PPM On Premise Clarity PPM SaaS

Issue/Introduction

Is there an example on how to send an email via GEL script using java methods?

Resolution

Below sample script using java mail methods

<gel:script xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:core="jelly:core" xmlns:jemail="jelly:email"
xmlns:file="jelly:com.niku.union.gel.FileTagLibrary"
xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
xmlns:soap="jelly:com.niku.union.gel.SOAPTagLibrary"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sql="jelly:sql" xmlns:util="jelly:util"
xmlns:xog="http://www.niku.com/xog" xmlns:bpm="jelly:com.niku.bpm.gel.BPMTagLibrary"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <!-- Initializing variables -->
 <core:set value="0" var="counter"/>
 <gel:parameter default="[email protected]" var="fromEmailAddress"/>
 <gel:parameter default="[email protected]" var="supportEmail"/>

 <core:invokeStatic className="java.lang.System" method="getenv" var="NIKU_HOME">
  <core:arg value="NIKU_HOME"/>
 </core:invokeStatic>
 <gel:parse file="${NIKU_HOME}/config/properties.xml" var="properties"/>
 <gel:set asString="true" select="$properties//mailServer/@host" var="SMTPServerHost"/>
 <gel:set asString="true" select="$properties/properties/webServer/@schedulerUrl" var="XOGURL"/>
 <gel:set asString="true" var="SMTPServer" select="$properties//mailServer/@host"/>

 <gel:log> SMTPServer : ${SMTPServer}</gel:log>

 <!-- Set MailAgent -->
 <core:invokeStatic className="java.lang.Class" method="forName" var="recipientType">
  <core:arg type="java.lang.String" value="javax.mail.Message$RecipientType" />
 </core:invokeStatic>
 <core:set value="${recipientType.getField('TO')}" var="recipientTypeTO" />
 <core:set value="${recipientTypeTO.TO}" var="recipientTypeTO" />
 <core:set value="${recipientType.getField('CC')}" var="recipientTypeCC" />
 <core:set value="${recipientTypeCC.CC}" var="recipientTypeCC" />
 <core:set value="${recipientType.getField('BCC')}" var="recipientTypeBCC" />
 <core:set value="${recipientTypeBCC.BCC}" var="recipientTypeBCC" />

 <!-- Initiate a new java mail session -->
 <core:new className="java.util.Properties" var="props" />
 <core:set value="${props.put('mail.transport.protocol', 'smtp')}" var="void" />
 <core:set value="${props.put('mail.smtp.host', SMTPServer)}" var="void" />
 <core:set value="${props.put('mail.smtp.auth', 'true')}" var="void" />

 <core:new className="com.niku.union.notification.MailAuthenticator" var="clarity_auth" />
 <core:invokeStatic className="javax.mail.Session" method="getInstance" var="mailSession">
  <core:arg type="java.util.Properties" value="${props}" />
  <core:arg type="javax.mail.Authenticator" value="${clarity_auth}" />
 </core:invokeStatic>


 <!-- Establish a database connection. -->
 <core:catch var="errorVar">
  <gel:setDataSource dbId="Niku" var="clarityDS"/>
 </core:catch>
 <core:choose>
  <core:when test="${errorVar != null}">
   <core:set value="Database Connection Failed. Error Messge: ${errorVar}" var="errorText"/>
   <gel:log level="WARNING">${errorText}</gel:log>
  </core:when>
  <core:otherwise>
   <gel:log level="INFO">Connected to Database successfully</gel:log>
  </core:otherwise>
 </core:choose>
 <gel:parse var="htmlMessage">
  <message>
  <![CDATA[
  <html>

  <table border="2">
  <th>Name</th>
  <th>Value</th>
  <tr>
  <td>Pi</td>
  <td>3.1416</td>
  </tr>
  <tr>
  <td>Light Speed</td>
  <td>3*10^8</td>
  </tr>
  </table>
  </font>
  </html>
  ]]>
  </message>
 </gel:parse>

 <gel:forEach select="$htmlMessage/message/node()" var="thisMessageNode">
  <gel:set asString="true" select="$thisMessageNode" var="thisMessageNodeText" />
  <core:set value="${thisMessageNodeText}" var="thisMessageText" />
 </gel:forEach>

 <gel:log level="INFO">Start Notification</gel:log>

 <!-- Add sender -->
 <core:new className="javax.mail.internet.InternetAddress" var="sender">
  <core:arg type="java.lang.String" value="${fromEmailAddress}" />
 </core:new>

 <!-- Add recipients -->
 <core:new className="javax.mail.internet.InternetAddress" var="internetAddress"/>
 <core:set value="[email protected]" var="recipientsTO" />
 <core:set value="[email protected]" var="recipientsCC" />
 <gel:log level="INFO">${recipientsCC}</gel:log>
 <core:set value="${supportEmail}" var="recipientsBCC" />

 <!-- Add subject -->
 <core:set value="Clarity Notification - TEST" var="subject" />

 <!-- Add email body text and format -->
 <core:new className="javax.mail.internet.MimeMultipart" var="multiPart" />
 <core:new className="javax.mail.internet.MimeBodyPart" var="bodyPart" />
 <core:set value="${bodyPart.setContent(thisMessageText, 'text/html')}" var="void" />
 <core:set value="${multiPart.addBodyPart(bodyPart)}" var="void" />

 <!-- Send email message -->
 <core:new className="javax.mail.internet.MimeMessage" var="message">
  <core:arg type="javax.mail.Session" value="${mailSession}" />
 </core:new>
 <core:set value="${mailSession.setDebug(debug)}" var="void" />
 <core:set value="${message.setFrom(sender)}" var="void" />

 <core:choose>
  <core:when test="${recipientsTO.indexOf(',') > 0}">
   <core:set value="${message.setRecipients(recipientTypeTO, internetAddress.parse(recipientsTO))}" var="void" />
  </core:when>
  <core:otherwise>
   <core:new className="javax.mail.internet.InternetAddress" var="recipientsTO">
    <core:arg type="java.lang.String" value="${recipientsTO}" />
   </core:new>
   <core:set value="${message.setRecipient(recipientTypeTO, recipientsTO)}" var="void" />
  </core:otherwise>
 </core:choose>

 <core:set value="[email protected]" var="recipientsReplyTo" />
 <core:set value="${message.setReplyTo(internetAddress.parse(recipientsReplyTo))}" var="void" />

 <core:set value="${message.setSubject(subject)}" var="void" />
 <core:set value="${message.setContent(multiPart)}" var="void" />
 <core:set value="${mailSession.getTransport()}" var="transport" />
 <core:set value="${transport.connect()}" var="void" />

 <core:switch on="${transport.isConnected()}">
  <core:case value="${true}">
   <core:set value="${transport.send(message)}" var="void" />
   <core:set value="${transport.close()}" var="void" />
  </core:case>
  <core:default>
   <core:new className="java.lang.Exception" var="exception">
    <gel:log level="ERROR">"Cannot connect to mail server: ${mailServer}"</gel:log>
   </core:new>
  </core:default>
 </core:switch>
 <gel:log level="INFO">End Notification</gel:log>

</gel:script>