Author Topic: Emailer threading  (Read 4261 times)

Phrog30

  • Guest
Emailer threading
« on: April 22, 2017, 02:04:33 PM »
I was playing with the emailer and it clogs the UI and was trying to thread this.  The best I could come up with was this:

Code: [Select]
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Email test

        Dim t As Thread
        t = New Thread(AddressOf Me.Send_Mail)
        t.Start()

    End Sub

    Private Sub Send_Mail()

        Try
            Dim Smtp_Server As New SmtpClient
            Dim e_mail As New MailMessage()
            Smtp_Server.UseDefaultCredentials = False
            Smtp_Server.Credentials = New Net.NetworkCredential("xxxxxxx", "xxxxxxxxxxxx")
            Smtp_Server.Port = 587
            Smtp_Server.EnableSsl = True
            Smtp_Server.Host = "smtp.gmail.com"

            e_mail = New MailMessage()
            e_mail.From = New MailAddress("xxxxxxxxxx")
            e_mail.To.Add("xxxxxxxxxxxx")
            e_mail.Subject = "Email Sending"
            e_mail.IsBodyHtml = False
            e_mail.Body = "Email body"

            If Me.InvokeRequired Then
                Me.Invoke(Sub()
                              Smtp_Server.Send(e_mail)
                          End Sub)
            Else
                Smtp_Server.Send(e_mail)
            End If

            MsgBox("Mail Sent")

        Catch error_t As Exception
            MsgBox(error_t.ToString)
        End Try

    End Sub

This works, but it still clutters the UI briefly.  If I don't check invoke, I will get an exception.  So, is there a way to email/text without using the UI at all?

James

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Emailer threading
« Reply #1 on: April 22, 2017, 02:15:17 PM »
Did you try leaving out this code:
Code: [Select]
If Me.InvokeRequired Then
                Me.Invoke(Sub()
                              Smtp_Server.Send(e_mail)
                          End Sub)
            Else
The Invoke places it back onto the UI thread and holds it up until complete.

Phrog30

  • Guest
Re: Emailer threading
« Reply #2 on: April 22, 2017, 05:09:43 PM »
Yes, without it, it would throw an exception.  I think I found a way, I'm testing right now.  When I say found, I mean Google. :)

Phrog30

  • Guest
Re: Emailer threading
« Reply #3 on: April 22, 2017, 06:05:33 PM »
This works pretty well:
Code: [Select]
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim trd = New Thread(AddressOf Email)
        trd.IsBackground = True
        trd.Start()

    End Sub

    Private Sub Email()

        Using mail As New MailMessage
            mail.From = New MailAddress(xxxxx@gmail.com")
            mail.To.Add("5551234567@vtext.com")
            mail.Body = "AdvancedHMI just sent you a text message, pretty cool huh??"
            'If Not OpenFileDialog1.FileName = vbNullString Then
            '    Dim attach As New Attachment(OpenFileDialog1.FileName)
            '    mail.Attachments.Add(attach)
            'End If
            mail.Subject = ""     'leave blank for text
            mail.IsBodyHtml = False
            mail.Priority = MailPriority.Normal
            Using SMTP As New SmtpClient
                SMTP.EnableSsl = True
                SMTP.Port = "587"
                SMTP.Host = "smtp.gmail.com"
                SMTP.UseDefaultCredentials = False
                SMTP.DeliveryMethod = SmtpDeliveryMethod.Network
                SMTP.Credentials = New Net.NetworkCredential("xxxxxx@gmail.com", "Password123")
                Try
                    SMTP.Send(mail)
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Return
                End Try

                Email_Success()

            End Using
        End Using

    End Sub

Now, nothing is stopping me from blasting emails or texts.  What is the best way to limit threads?

Phrog30

  • Guest
Re: Emailer threading
« Reply #4 on: April 22, 2017, 06:15:54 PM »
One more question, we have two terminals on order for us to demo, arriving next week.  Each has two NIC's.  If I want to communicate to the PLCs on one, but email on the other, is there anything to do to route in AHMI?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Emailer threading
« Reply #5 on: April 29, 2017, 11:01:55 AM »
Windows will automatically determine where to send the email packet based on the IP resolution and the settings of the ports.

Phrog30

  • Guest
Re: Emailer threading
« Reply #6 on: May 10, 2017, 09:58:29 AM »
Windows will automatically determine where to send the email packet based on the IP resolution and the settings of the ports.

Not that I doubted you Archie, but I did confirm this in a test this morning.  I connected one NIC to the PLC with a static IP and the other NIC to our office network as DHCP.  Both PLC comms and email worked.

James

sebas

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Emailer threading
« Reply #7 on: June 13, 2017, 10:09:27 PM »
Hello I have a small problem with sending email when I do code myself and I press button1 its works very well but when I use the function emailler1 its not working. I use visual studio 2015.

Imports System.Web
Imports System.IO
Imports System.Net.Mail



Public Class MainForm
    '*******************************************************************************
    '* Stop polling when the form is not visible in order to reduce communications
    '* Copy this section of code to every new form created
    '*******************************************************************************
    Private NotFirstShow As Boolean

    Private Sub Form_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.VisibleChanged
        '* Do not start comms on first show in case it was set to disable in design mode
        If NotFirstShow Then
            AdvancedHMIDrivers.Utilities.StopComsOnHidden(components, Me)
        Else
            NotFirstShow = True
        End If
    End Sub

    '***************************************************************
    '* .NET does not close hidden forms, so do it here
    '* to make sure forms are disposed and drivers close
    '***************************************************************
    Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Dim index As Integer
        While index < My.Application.OpenForms.Count
            If My.Application.OpenForms(index) IsNot Me Then
                My.Application.OpenForms(index).Close()
            End If
            index += 1
        End While
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Buttontest.Click
        Try
            Dim Smtp_Server As New SmtpClient
            Dim e_mail As New MailMessage()
            Smtp_Server.UseDefaultCredentials = False
            Smtp_Server.Credentials = New Net.NetworkCredential("sebastienduhaime@gmail.com", "xxxxxx")
            Smtp_Server.Port = 587
            Smtp_Server.EnableSsl = True
            Smtp_Server.Host = "smtp.gmail.com"

            e_mail = New MailMessage()
            e_mail.From = New MailAddress("sebastienduhaime@gmail.com")
            e_mail.To.Add("sebastienduhaime@live.ca")
            e_mail.Subject = "Email Sending"
            e_mail.IsBodyHtml = False
            e_mail.Body = TxtMessage.Text
            Smtp_Server.Send(e_mail)
            MsgBox("Mail Sent")

        Catch error_t As Exception
            MsgBox(error_t.ToString)
        End Try

    End Sub

    Private Sub DataSubscriber1_DataChanged(sender As Object, e As Drivers.Common.PlcComEventArgs) Handles DataSubscriber1.DataChanged

    End Sub
End Class




Here are the mistakes I have when I use emailler:


Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Exception levée : 'System.Net.Mail.SmtpException' dans System.dll
Exception levée : 'System.Net.Mail.SmtpException' dans System.dll
Exception levée : 'System.Net.Mail.SmtpException' dans System.dll
Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Exception levée : 'System.NullReferenceException' dans AdvancedHMIControls.dll
Le thread 0x28d8 s'est arrêté avec le code 0 (0x0).
Le thread 0x3edc s'est arrêté avec le code 0 (0x0).
Le thread 0x359c s'est arrêté avec le code 0 (0x0).
Le thread 0x3784 s'est arrêté avec le code 0 (0x0).
Le programme '[15132] AdvancedHMI.vshost.exe' s'est arrêté avec le code 0 (0x0).


thanks and best regard!!!

Sebastien Duhaime




Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Emailer threading
« Reply #8 on: June 13, 2017, 10:20:36 PM »
Can you post the values you are using in the Emailer properties?

sebas

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Emailer threading
« Reply #9 on: June 14, 2017, 09:49:19 AM »
Attach a screenshot

thanks and best regard!!

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5262
    • View Profile
    • AdvancedHMI
Re: Emailer threading
« Reply #10 on: June 14, 2017, 10:02:04 AM »
Add a button, then in the click event add this code:

        Emailer1.SendMessage()

This will force a send to see if the settings work.

sebas

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Emailer threading
« Reply #11 on: June 14, 2017, 07:30:40 PM »
hello Archie,

This is an email send by a code its works well


view screnshot.


thanks!!

sebas

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Emailer threading
« Reply #12 on: June 14, 2017, 07:34:06 PM »
This is an email send by emailer1 with button

Sorry for french !


An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional Information: The SMTP server requires a secure connection or the client is not authenticated. The server response was: Authentication required


« Last Edit: June 14, 2017, 08:00:44 PM by sebas »

Phrog30

  • Guest
Re: Emailer threading
« Reply #13 on: June 14, 2017, 07:49:14 PM »
This is an email send by emailer1 with button

Sorry for french !

Don't you think it should be up to you to translate from french to English?

sebas

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: Emailer threading
« Reply #14 on: June 14, 2017, 08:02:09 PM »
Sorry i translated it

thanks!!