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

Allan19111911

  • Newbie
  • *
  • Posts: 17
    • View Profile
Sending an Email Triggered by a Bit
« on: November 29, 2016, 10:10:09 PM »
Hello all, i have got the "sending email by bit working now"    is there a way to include variables in the message ?     ie,    The temp of the water is (n7:0)         I'm using the EthernetIPforSLCMicroCom1 driver

Thanks'
Allan



Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: Sending an Email Triggered by a Bit
« Reply #1 on: November 29, 2016, 11:05:20 PM »
The Emailer component is very basic and does not give a way to do anything like that. You can use a DataSubscriber and send an email via code as shown in this topic:

http://advancedhmi.com/forum/index.php?topic=796.msg3858#msg3858

You can then modify the line of code:

email.Body = "This event occurred"

to something like this:

email.Body = "The Temp of the water is " & EthernetIPforSLCMicroCom1.Read("N7:0")

Allan19111911

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Sending an Email Triggered by a Bit
« Reply #2 on: November 29, 2016, 11:26:42 PM »
worked first time, thank you so much

here is the code i used to get gmail to work,  I pieced it together from a few different sources.  It requires gmail to have an app password through 2 step verification
i am not very good with vb so dont laugh if there is mistakes,,, i am good at cut and paste

Private Sub DataSubscriber1_DataChanged(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




                Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()

            SmtpServer.EnableSsl = True
            SmtpServer.Credentials = New _
        Net.NetworkCredential("youremailaddress@gmail.com", "apppassword")
            SmtpServer.Port = 587

            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("someone@gmail.com")
            mail.To.Add("someone@yahoo.com")
            mail.Subject = "Test Mail"
                    mail.Body = "The Temp of the water is " & EthernetIPforSLCMicroCom1.Read("N7:0")
                    SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)

                End Try

            End If
        End If
    End Sub

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: Sending an Email Triggered by a Bit
« Reply #3 on: November 29, 2016, 11:31:16 PM »
Thanks for posting your solution. A while back I tried with Gmail, it worked once, then would not work again so I gave up on it.

Allan19111911

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Sending an Email Triggered by a Bit
« Reply #4 on: November 30, 2016, 08:22:02 PM »
hi archie,  One more question,  is there any way to get multiple lines and variables in the body

ie
 mail.Body = "The Temp of the water is " & EthernetIPforSLCMicroCom1.Read("N7:0")
                      "The depth of the water is "& EthernetIPforSLCMicroCom1.Read("N7:1")
                       " water flow is "& EthernetIPforSLCMicroCom1.Read("N7:1") 
 I tried this but get errors,

if i put mail.body in front of all of them i only get the last one.

thanks
Allan

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5261
    • View Profile
    • AdvancedHMI
Re: Sending an Email Triggered by a Bit
« Reply #5 on: November 30, 2016, 08:28:51 PM »
Try something like this:

mail.Body = "The Temp of the water is " & EthernetIPforSLCMicroCom1.Read("N7:0") & Environment.NewLine  & _
                      "The depth of the water is "& EthernetIPforSLCMicroCom1.Read("N7:1") & Environment.NewLine & _
                       " water flow is "& EthernetIPforSLCMicroCom1.Read("N7:1") 

dmroeder

  • Global Moderator
  • Full Member
  • *****
  • Posts: 206
    • View Profile
Re: Sending an Email Triggered by a Bit
« Reply #6 on: November 30, 2016, 08:31:14 PM »
You can't just put a new line in your code to get it to treat it as a new line in a string.  One way to add a new line is to use Environment.NewLine

Code: [Select]
mail.Body = "The Temp of the water is " & EthernetIPforSLCMicroCom1.Read("N7:0") & Environment.NewLine &_
                     "The depth of the water is "& EthernetIPforSLCMicroCom1.Read("N7:1") & Environment.Newline & _
                     " water flow is "& EthernetIPforSLCMicroCom1.Read("N7:1")

Well shoot, too slow...



Allan19111911

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Sending an Email Triggered by a Bit
« Reply #7 on: November 30, 2016, 08:32:34 PM »
thanks everyone

bkulrich

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Sending an Email Triggered by a Bit
« Reply #8 on: February 13, 2018, 09:21:09 AM »
I tried the above approach, but it seems to only read the values when the application is started, not when the emailer is activated.  (So whenever the emailer sends an email, the values sent are the values from when the application was started that day.)