Example Code

CVI Sending mails thanks to Powershell

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Operating System

  • Windows

    Programming Language

  • C

Code and Documents

Attachment

Description

Purpose

Due to the hardening of the security policy on the net, some of former facilities of the CVI Internet library don't work as expected any more. Actually most of the SMTP servers require that the sender owns an account and moreover, require an SSL protocol. In turn, the sender must provide the correct TCP IP PORT and, unfortunately, this last feature is not available in the standard InetSendMail which only allows to get the server, using the standard PORT 25. To overcome this difficulty, this article suggests to lies upon the Powershell abilities.

Principle

Build a function named 'PS_SendMail' which:

  • gets the main pieces of information useful to form a message: from, to, subject, message body;
  • builds a Powershell script file and store it in a public location on the storage medium;
  • evokes Powershell to execute the script.

The critical and indispensable parameters, such as the smtp server address, the smtp port, the smtp user and his password can remain hidden in the code or put in a configuration file.

PS-SendMail

The function PS_SendMail provided in the C source in attachment of this article is really short and simple, doesn't need any extra library and launches the PowerShell thanks to a call to the well-known function system(). Of course, the syntax of Powershell has to be known.

 

How to Use

  • Make a project with the C source ps_sendmail.c
  • Compile it as a static library
  • Use it in any program requiring to send mail, providing the following declaration:
static int PS_SendMail(char *from, char *to, char *subject, char *message) ;

 

 

Additional Information

 

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

Contributors