AnsweredAssumed Answered

How to code an HTML formatted email using GEL script

Question asked by William_W on Sep 27, 2013
Latest reply on Aug 25, 2015 by DK_Winmill
I'd like to share my findings about how to write a HTML formatted email using GEL script. If any of you have a better way to do this, or if you can optimise the code, please share with us. Thanks
<gel:script xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:core="jelly:core"

xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"

xmlns:sql="jelly:sql" xmlns:util="jelly:util"

xmlns:xog="http://www.niku.com/xog"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">



<!-- Variables and Parameter declarations... -->

<core:set value="${true}" var="debug" />

<gel:parameter default="clarityAdmin@mycompany.com" var="FromEmailAddress"/>



<!--Read Property File... -->

<core:invokeStatic className="com.niku.union.config.ConfigurationManager"


method="getInstance" var="config" />

<core:set value="${config.getProperties()}" var="config" />

<core:set value="${config.getMailServer().getHost()}" var="mailServer" />



<!-- 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', mailServer)}" 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>



<!-- Add sample HTML email message -->

<gel:parse var="htmlMessage">


<message><![CDATA[


<html>


<h1>Hello World</h1>




</html>


]]></message>

</gel:parse>

<gel:forEach select="$htmlMessage/message/node()" var="thisMessageNode">


<gel:set asString="true" select="$thisMessageNode" var="thisMessageNodeText" />


<core:set value="${thisMessageText}${thisMessageNodeText}" var="thisMessageText" />

</gel:forEach>



<!-- 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="user1@mycompany.com,user2@mycompany.com" var="recipientsTO" />

<core:set value="me@mycompany.com" var="recipientsCC" />
                             

<!-- Add subject -->

<core:set value="Hello World!" 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:choose>


<core:when test="${recipientsCC.indexOf(',') > 0}">



<core:set value="${message.setRecipients(recipientTypeCC, internetAddress.parse(recipientsCC))}" var="void" />
                        




</core:when>


<core:otherwise>



<core:new className="javax.mail.internet.InternetAddress" var="recipientsCC">




<core:arg type="java.lang.String" value="${recipientsCC}" />



</core:new>




<core:set value="${message.setRecipient(recipientTypeCC, recipientsCC)}" var="void" />


</core:otherwise>

</core:choose>




<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:script>

Outcomes