vCO – Send email attachment

There are maybe times when you want to send an email attachment. A csv file for example. It is actually quit easy to achieve.

Just create a duplicate of the existing workflow “Send notification” and add a few lines.

Add following lines:

//Create Attachment
var fileAttachment = new MimeAttachment();
fileAttachment.name = attachmentName;
fileAttachment.content = attachment;
message.addMimePart(fileAttachment,"text/html; charset=UTF-8");

attachmentName is an input parameter of the type string with the file name of the attachment.
attachment is an input parameter of the type string with the data you want to attach. (“\n” will be a carriage return).

This will be the full code including the existing code from VMware.

var message = new EmailMessage();

// Override default settings if and only if input parameter is set
if ( smtpHost != null && smtpHost.length > 0 ){
	message.smtpHost = smtpHost;
}
if ( smtpPort != null && smtpPort > 0 ){
	message.smtpPort = smtpPort;
}
if ( username !=null && username.length > 0){
	message.username = username;
}
if ( password != null && password.length > 0){
	message.password = password;
}
if ( fromName != null && fromName.length > 0){
	message.fromName = fromName;
}
if ( fromAddress != null && fromAddress.length > 0){
	message.fromAddress = fromAddress;
}
message.toAddress = toAddress;
message.subject = subject;
message.addMimePart(content,"text/html; charset=UTF-8");

//Create Attachment
var fileAttachment = new MimeAttachment();
fileAttachment.name = attachmentName;
fileAttachment.content = attachment;
message.addMimePart(fileAttachment,"text/html; charset=UTF-8");

System.log( "sending mail to host: " + message.smtpHost + ":" + message.smtpPort + " with user:" + message.username + ", from:" + message.fromAddress + ", to:" + message.toAddress );

message.sendMessage();

Published by txusa

VMware Certified Design Expert - VCDX 92. VMware Architect, automation enthusiast.

2 thoughts on “vCO – Send email attachment

  1. What if i would like send mail with attachment which is on some Windows Server 2012 ? My idea is backup some keypass file from Windows Server to Sharepoint (which has assigned email-adress do folder).
    If this is possisble ? to send file from separate server ?

    1. That should be possible. You need the workflow “Copy file from guest to vCO” to copy the file (I assume vCO needs writing permissions to store file on vCO, c:\orchestrator is allowed by default). Then you need to read the file from vCO. I have not fully tested this but I think below might work to read the file. That file then must be passed on as attachment as described above.

      var myFile = new FileReader(filePath); //filePath is the path to the file on vCO
      try
      {
      myFile.open();
      }
      catch (ex)
      {
      System.error(“ERROR!”);
      }
      var fileContent = myFile.readAll();
      myFile.close();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: