This FAQ will show you how to send an email when a digital Input or Output is triggered. The email is sent over SMTP. Note this FAQ also applies to Brainboxes Remote IO (ED) device range, but the code will have to run on another piece of hardware (e.g. a PC or laptop). For further information on how to set up your development environment and write C# for the BB-400 please follow the FAQ Write C Sharp Application for BB-400.

Send Email on Changing of an IO Line on the BB-400

Now you can edit the code in Program.cs to monitor and control the IO on a BB-400 then send an email when an IO line changes. Change the Program.cs file to be the following, edit the IP address on line 11, and save the file:

using System;
using Brainboxes.IO;
using MailKit.Net.Smtp;
using MailKit;
using MimeKit;

namespace bb400project
{
    class Program
    {
        static void Main(string[] args)
        {
            // update this value to the IP address of your BB-400
            string ipAddress = "192.168.0.85";
            BB400 bb = new BB400(new TCPConnection(ipAddress));

            Console.WriteLine($"Connecting to BB-400 at address {ipAddress}");
            bb.Connect();
            Console.WriteLine("Connected!");

            Console.WriteLine("Monitoring IO Line 0 for when it goes from high to low, send email when it happens");
            bb.Inputs[0].IOLineFallingEdge += IOChangeSendEmail;

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

            Console.WriteLine("Key Pressed, disconnecting and exiting!");
            bb.Disconnect();
        }

        private static void IOChangeSendEmail(IOLine line, EDDevice device, IOChangeTypes changeType)
        {
            Console.WriteLine($"Line {line} changed from high to low sending email");
            var message = new MimeMessage ();
            message.From.Add (new MailboxAddress ("Joey Tribbiani", "[email protected]"));
            message.To.Add (new MailboxAddress ("Mrs. Chanandler Bong", "[email protected]"));
            message.Subject = "IO Line went from High to Low";

            message.Body = new TextPart ("plain") {
                Text = $"Digital Input Line {line} on BB-400 just went from high to low"
            };

            using (var client = new SmtpClient ()) {
                // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
                client.ServerCertificateValidationCallback = (s,c,h,e) => true;

                client.Connect ("smtp.myserver.com", 587, false);

                // Note: only needed if the SMTP server requires authentication
                client.Authenticate ("joey", "password");

                client.Send (message);
                client.Disconnect (true);
            }
            Console.WriteLine("Email Sent");
        }
    }
}

Use NuGet in VS Code to install MailKit. MailKit is the .NET core library used to connect to mailservers and send email.

The MailKit library works with lots of different email servers, for example to work with gmail, simply swap out the using(var client = new SmtpClient()) { block in the example above with the following:

    using (var client = new SmtpClient ()) {
       client.Connect ("smtp.gmail.com", 587);

       // use the OAuth2.0 access token obtained by following: https://developers.google.com/identity/protocols/OAuth2 
       var oauth2 = new SaslMechanismOAuth2 ("[email protected]", credential.Token.AccessToken);
       client.Authenticate (oauth2);

       client.Send (message);
       client.Disconnect (true);
    }

Note: Google specifies that you use OAuth as the connection method.

This FAQ Applies to the following Brainboxes Product Ranges: