Author Topic: Sending an Email Triggered by a Bit  (Read 5441 times)

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Sending an Email Triggered by a Bit
« on: June 08, 2015, 09:36:37 PM »
You can fairly easily send an email with AdvancedHMI based on a bit going True.

- Add a driver to your form and set its properties
- Add a DataSubscriber to the form and set the PLCAddressValue property to the bit you want to trigger the email
- Double click the DataSubscriber to get back to the code
- Enter and modify this code accordingly:

    Private Sub DataSubscriber1_DataChanged_1(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged
        '* Make sure Data came back
        If e.Values IsNot Nothing AndAlso e.Values.Count > 0 Then
            '* Did it come back as True or 1?
            If String.Compare(e.Values(0), "TRUE", True) = 0 Or e.Values(0) = "1" Then
                Using email As New System.Net.Mail.MailMessage
                    '* Set who it is from
                    email.From = New System.Net.Mail.MailAddress("advancedhmi@mydomain.com")

                    '* Add the recipients
                    email.To.Add(New System.Net.Mail.MailAddress("receiver@theirdomain.com"))

                    email.Subject = "AdvancedHMI Event Was Triggered"
                    email.Body = "This event occurred"

                    '* Create the client that will send the message
                    Dim MailSendingClient As New System.Net.Mail.SmtpClient
                    '* set this to the smtp server you will use
                    MailSendingClient.Host = "MySmtpServer"
                    MailSendingClient.Port = 25

                    '* If your SMTP Server does not require a password, then remove this section
                    Dim credentials As New System.Net.NetworkCredential("MyUsername", "MyPassword")
                    MailSendingClient.Credentials = credentials

                    '* Send the email
                    MailSendingClient.Send(email)
                End Using
            End If
        End If


If your SMTP server has different requirements, there are many examples on the Internet. Google "send email .NET"