Workflow scripted elements can include scripting of common email-related tasks. You can cut, paste, and adapt these examples into your scripted elements.

When you run a mail workflow, it uses the default mail server configuration that you set in the Configure mail workflow. You can override the default values by using input parameters, or by defining custom values in workflow scripted elements.

Obtain an Email Address

The following JavaScript example obtains the email address of the current owner of a running script.

var emailAddress = Server.getRunningUser().emailAddress ;

Send an Email

The following JavaScript example sends an email to the defined recipient, through an SMTP server, with the defined content.

var message = new EmailMessage() ; 
message.smtpHost = "smtpHost" ;
message.subject= "my subject" ; 
message.toAddress = "receiver@vmware.com" ; 
message.fromAddress = "sender@vmware.com" ;
message.addMimePart("This is a simple message","text/html") ;
message.sendMessage() ;

Retrieve Email Messages

The following JavaScript example retrieves the messages of an email account, without deleting them, by using the scripting API provided by the MailClient class.

var myMailClient = new MailClient();

myMailClient.setProtocol(mailProtocol);
if(useSSL){
 myMailClient.enableSSL();
}

myMailClient.connect( mailServer, mailPort, mailUsername, mailPassword);
System.log("Successfully login!");

try {
	myMailClient.openFolder("Inbox");
	
	var messages = myMailClient.getMessages();
	System.log("Reading messages...!");
	if ( messages != null && messages.length > 0 ) {
	  System.log( "You have " + messages.length + " email(s) in your inbox" );
	 for (i = 0; i < messages.length; i++) {
	  System.log("");
	  System.log("-----MSG-------");
	  System.log("Headers: ");
	  var headerProp = messages[i].getHeaders();
	  for each(key in headerProp.keys){
	   System.log(key+": "+headerProp.get(key));
	  }
	  System.log("");  
	
	  System.log( "Message["+ i +"] with from: " + messages[i].from + " to: " + messages[i].to);
	  System.log( "Message["+ i +"] with subject: " + messages[i].subject);
	  var content = messages[i].getContent();
	  System.log("Msg content as string: " + content);
	 }
	} else {
	  System.warn( "No messages found" );
	}
} finally {
	myMailClient.closeFolder();
	myMailClient.close();
}