Info |
---|
Download the solution AlarmEmailAndSMS.dbsln. Built with v10. This solution demonstrates how to configure the system to send email and SMS messages. |
Summary
This sample demonstrates how to configure the system to send
anemail and
aSMS
messagemessages with alarm details
whenever an alarm event occurs, using the "NotificationMethod
Technical Information
The NotificationMethod used in this solution example allows configuring your solution to send an email, an SMS, a message box, or speech the alarm message every time a conditional event alarm occurs.
"
feature. When a conditional event alarm is triggered, a message is automatically sent containing key alarm information.
Technical Information
The method AlarmEvents(AlarmEventInfo[] events)
is automatically called whenever a conditional alarm event occurs. It receives an array of events and processes the first one to send a notification.
OAuth2 Authentication Update (Gmail/Outlook)
Important:
This version uses OAuth2 instead of Basic Authentication for email sending. Username/password authentication is no longer supported for providers like Gmail and Outlook.
Required Dependencies
The necessary libraries are included by default in the software package.
When the solution runs, the NotificationMethod is called every time a conditional event alarm occurs. Then, this method receives an Array of AlarmEventInfo as a parameter. See an example code below:
Code Block | ||
---|---|---|
| ||
public void AlarmEvents(AlarmEventInfo[] events) { //Protection in case events its null if (events == null) return; //Get the first event AlarmEventInfo event @Info.Trace("Alarms, count = "+events.Length.ToString()); AlarmEventInfo test = events[0]; @Info.Trace("Alarms, State = " + test.State.ToString()); if (eventtest.State != 1) return; //Get information about the alarm event to create the body of the email string body = "Time: " + eventtest.ActiveLocalTime.ToString() + "\n" + "Message = " + event test.Message + "\n" + "Area = " + eventtest.Area + "\n" + "Group = " + eventtest.Group + "\n" + "Tag = " + event test.TagName + "\n"; //Code to send email try { //Configuring the SMTP Client System.Net.Mail.SmtpClient mySmtpClient = new System.Net.Mail.SmtpClient(@Tag.smtpServer, 587); mySmtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; mySmtpClient.EnableSsl = true; mySmtpClient.UseDefaultCredentials = false; mySmtpClient.Credentials = new System.Net.NetworkCredential(@Tag.fromEmail, @Tag.passFromEmail); //Sending the email mySmtpClient.Send ; OAuth2EmailSender mySender = new OAuth2EmailSender(); mySender.SendEmailAsync(@Tag.fromEmail, @Tag.toEmail, "DmailAlarm notificationbeeped!", body); } catch (Exception ex) { @Info.Trace("Error sending message: " + ex.Message); } }} |
Credentials Required
Depending on your provider, you’ll need to configure OAuth2 credentials instead of using a regular email and password. Here's some examples on how you can do that:
For Gmail
To authenticate with Gmail via OAuth2, you’ll need to generate a Client ID and a Client Secret through the Google Cloud Console:
Go to Google Cloud Console
Create or select a project
In the APIs & Services > Library, search for and enable the Gmail API
Then go to OAuth consent screen and choose the External option. Set your app name, support email, and developer contact info (all can be your own Gmail).
After configuring the consent screen, go to Credentials, click Create Credentials > OAuth Client ID, and choose Desktop App
Name it something like
"DesktopMailer"
and click Create — you'll receive your Client ID and Client SecretAs a final step, go back to the OAuth consent screen, scroll to Test users, and add your own Gmail to be able to test sending emails
You'll use the credentials like this:
Code Block |
---|
GmailClientId = "your-client-id.apps.googleusercontent.com";
GmailClientSecret = "your-secret"; |
For Outlook
To authenticate with Outlook using OAuth2, you’ll need a Client ID and Tenant ID, obtained through the Azure portal.
You must register an application, define redirect URIs, and configure Microsoft Graph API permissions (e.g., Mail.Send
)
.
Reference Information
→ See Alarms for more information.
In this section:
Page Tree | ||||
---|---|---|---|---|
|