Example Code

Send an Email From DIAdem

Code and Documents

Attachment

Overview

DIAdem is a great tool for reporting locally. This class can help extend this by allow you to add email capabilities to your scripts.

Description

This is achieved by loading a CDO.Message ActiveX class which is provided by windows. In general this class can be used for a number of different message types but we set it up for SMTP to an external server.

Implementation Notes

  • Normally ActiveX provides type libraries (.tlb) that define constants and enums that you may need to run the library. However these cannot be loaded into VBS to use so I create the needed items as constants at the start. You can find the values on the internet or by loading the .tlb file into an environment that can understand them such as Visual Studio.
  • I have wrapped this in a class. In reality the class doesn't do much but pass on the same requests, but it gives us an abstraction layer incase we have to change how the CDO.Message class works in the future.

 

'-------------------------------------------------------------------------------

'-- VBS Script for Sending Email from DIAdem

'-- Created on 09/07/2013 09:38:26

'-- Author: James McNally, NI UK

'-- Comment: Mostly just a wrapper for CDO Objects. Fully wrapped for future flexibility.

'-------------------------------------------------------------------------------

Option Explicit  'Forces the explicit declaration of all the variables in a script.

 

 

'Type library elements that we cannot load into VBS.

'Keeps the code below more readable.

private const cdoSendUsingMethod = "http://schemas.microsoft.com/cdo/configuration/sendusing"

private const cdoSMTPServer = "http://schemas.microsoft.com/cdo/configuration/smtpserver"

private const cdoSMTPServerPort = "http://schemas.microsoft.com/cdo/configuration/smtpserverport"

private const cdoSendUsingPort = 2

 

 

Class Email

 

 

private m_cdoMsg

 

 

'Public properties

public From, SendTo, Subject, TextBody, HTMLBody, SendCC, SendBCC, SMTPServer, SMTPPort

 

 

private sub Class_Initialize

 

  'CDO.Message documentation at http://msdn.microsoft.com/en-us/library/ms526453(v=exchg.10).aspx

  set m_cdoMsg = createobject("CDO.Message")

 

End Sub

 

 

Public Sub AddAttachment(filepath)

 

 

  m_cdoMsg.AddAttachment(filepath)

 

 

End Sub

 

Public Sub Send

 

 

  'Check configuration for missing required elements

  if (isEmpty(SMTPServer) or isEmpty(SendTo) or isEmpty(From)) then

    Call Err.Raise(5000,"Email.Send","You must specify an SMTP server, To and From.")

  end if

 

  'Handle a default port if not set.

  if isEmpty(SMTPPort) then

    SMTPPort = 25

  end if

 

 

  With m_cdoMsg

      .From = From

      .To = SendTo

      .Subject = Subject

      .Textbody = TextBody

      .HTMLBody = HTMLBody

      .CC = SendCC

      .BCC = SendBCC

  End With

 

 

  With m_cdoMsg.Configuration.Fields

              .Item(cdoSendUsingMethod) = cdoSendUsingPort

              .Item(cdoSMTPServer) = SMTPServer

              .Item(cdoSMTPServerPort) = SMTPPort

              .Update

  End With

 

  m_cdoMsg.Send

 

 

End Sub

 

 

End Class

 

Steps to Implement or Execute Code

  1. This only provides a class. You need to add this as a user command or include it in another script where you call it.
  2. When you do this create a new Email object, assign a To, From and SMTPServer at a minumum and call the send method.

 

Requirements to Run

Software

  • Tested on DIAdem 2012. Should work in previous versions with VBS scripting.

The 64-bit version of DIAdem 2017 has an object-oriented script interface for sending emails. Details can be found in this topic and also in the related links: http://zone.ni.com/reference/en-XX/help/370858N-01/emailservice/objects/iemailservice_objects_overvi...

James Mc
========
CLA and cRIO Fanatic
My writings on LabVIEW Development are at devs.wiresmithtech.com

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.