kumsa29

IDM Suite : JS plugin code to verify email address

Blog Post created by kumsa29 Employee on May 15, 2018

 JS plugin code( Tested on vApp 14.0.1) to send Validation code email which can be used as a utility on the need basis.

Use case: Verify user-provided email address during the registration time.

 

//***********************************************************************

//  RhinoJS Plugin: Code

//************************************************************************

 

function SendValidationCodeEmail(toemailaddr) {


// Allows the usage of 'importPackage' with JRE8
try{
load("nashorn:mozilla_compat.js");
}
catch(e){}

// Import mail Packages
importPackage(java.util.Properties);
importPackage(java.util.Random);
importPackage(javax.mail.Message);
importPackage(javax.mail.MessagingException);
importPackage(javax.mail.Session);
importPackage(javax.mail.Transport);
importPackage(javax.mail.internet.InternetAddress);
importPackage(javax.mail.internet.MimeMessage);

importPackage(com.idmlogic.sigma.plugin.BasePlugin);
importPackage(com.idmlogic.sigma.plugin.PluginException);
importPackage(com.idmlogic.sigma.plugin.annotations.ExportedServerFunction);

// create mail session
var smtp_host = "mail.xyz.com";
var smtp_port = 25;

var from = "xyz@xyz.com";
var subject = "MSG01 :: Don't Reply - Validation Code";
var bodystart = "MSG02 :: Validation Code = [ ";
var bodyend = " ]" ;
var validationCode = 123456;
var to = toemailaddr;

var rnd = new java.util.Random();
validationCode = 100000 + rnd.nextInt(900000);
java.lang.System.out.println("MSG03 :: Randomly Generated Code = [" + validationCode + "]");

//Get the session object
var properties = java.lang.System.getProperties();
properties.setProperty("mail.smtp.host", smtp_host);
var session = javax.mail.Session.getDefaultInstance(properties);

//compose the message
try{
var message = new javax.mail.internet.MimeMessage(session);
message.setFrom(new javax.mail.internet.InternetAddress(from));
message.addRecipient(javax.mail.Message.RecipientType.TO,new javax.mail.internet.InternetAddress(to));
message.setSubject(subject);
message.setText(bodystart + validationCode + bodyend);

// Send message
javax.mail.Transport.send(message);
java.lang.System.out.println("MSG04 :: message sent successfully....");

return validationCode;

} catch (e) {
return 'Error: ' + e.toString();
}

}

 

 

//***********************************************************************

//  Validation Handler Code

//************************************************************************

 

Validation handler(api,prop){

 

return api.server(['SendValidationCodeEmail', "abc@ca.com"]).then(

                function(success) {

                                // First of all, get your return object

                                console.log(success);

                                var result = success.returnValue;

                                console.log(result);

                                api.prompt("MSGXX:: " + result );

                                return false;

                },

                function(error) {

                                // Do some eventual error handling like a message display

                                api.prompt("MSGXX- Error" );

                                return false;

                }

);

Outcomes