AdvancedHMI Software

General Category => Support Questions => Topic started by: Phrog30 on April 22, 2017, 02:04:33 PM

Title: Emailer threading
Post by: Phrog30 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
Title: Re: Emailer threading
Post by: Archie 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.
Title: Re: Emailer threading
Post by: Phrog30 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. :)
Title: Re: Emailer threading
Post by: Phrog30 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?
Title: Re: Emailer threading
Post by: Phrog30 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?
Title: Re: Emailer threading
Post by: Archie 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.
Title: Re: Emailer threading
Post by: Phrog30 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
Title: Re: Emailer threading
Post by: sebas 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



Title: Re: Emailer threading
Post by: Archie on June 13, 2017, 10:20:36 PM
Can you post the values you are using in the Emailer properties?
Title: Re: Emailer threading
Post by: sebas on June 14, 2017, 09:49:19 AM
Attach a screenshot

thanks and best regard!!
Title: Re: Emailer threading
Post by: Archie 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.
Title: Re: Emailer threading
Post by: sebas on June 14, 2017, 07:30:40 PM
hello Archie,

This is an email send by a code its works well


view screnshot.


thanks!!
Title: Re: Emailer threading
Post by: sebas 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


Title: Re: Emailer threading
Post by: Phrog30 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?
Title: Re: Emailer threading
Post by: sebas on June 14, 2017, 08:02:09 PM
Sorry i translated it

thanks!!
Title: Re: Emailer threading
Post by: Archie on June 14, 2017, 08:06:32 PM
Download and extract the attached solution. Open the Form1 and set the properties, then see what result you get from this.
Title: Re: Emailer threading
Post by: sebas on June 16, 2017, 02:55:35 PM
Same error message.

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


thanks!!
Title: Re: Emailer threading
Post by: Archie on June 16, 2017, 04:00:30 PM
When it stops at the exception, hover over "Client" and see if UseDefauleCredentials is False and Credentials are set to something.
Title: Re: Emailer threading
Post by: sebas on June 16, 2017, 04:13:16 PM
I start with visual studio so I do not understand everything but if you want one could use team viewer you could see for yourself.
Title: Re: Emailer threading
Post by: Archie on June 16, 2017, 04:24:40 PM
I'm not able to do a TeamViewer session today, but if you hover the mouse cursor over "client" on the line of code in which it stopped at, a window will popup with all of the properties and their values. Scroll through that list and look for UseDefauleCredentials and Credentials to see what value they have.
Title: Re: Emailer threading
Post by: sebas on June 16, 2017, 04:48:21 PM

 Credentials = {System.Net.NetworkCredential}         

UseDefaultCredentials = False
Title: Re: Emailer threading
Post by: Phrog30 on June 16, 2017, 05:01:10 PM
What happens if you use the code in reply #3? Assuming you are using Gmail, just input your specific info.
Title: Re: Emailer threading
Post by: sebas on June 16, 2017, 05:12:54 PM
I do not understand what you mean?
Title: Re: Emailer threading
Post by: Phrog30 on June 16, 2017, 05:21:13 PM
Go to reply #3 in this thread and copy the code, paste it in your application and try it, obviously using your email settings.
Title: Re: Emailer threading
Post by: sebas on June 16, 2017, 05:29:08 PM
I did it and it works very well its the emailer function which wants not to work with the same email setting.
Title: Re: Emailer threading
Post by: Phrog30 on June 16, 2017, 05:49:31 PM
Ok, now I don't understand. That code is an emailer.
Title: Re: Emailer threading
Post by: Archie on June 17, 2017, 06:41:53 PM
I have tried this using the emailer component and the using the code you posted, in both cases I get the same message from Gmail:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

I even went into my Gmail settings and turned on the "Allow less secure apps" setting, but it still does not work.
Title: Re: Emailer threading
Post by: sebas on June 18, 2017, 09:06:22 PM
I managed to run the emailertest that you made me try at Reply # 15 by modifying the code as follows:

  Client.Credentials = NewSystem.Net.NetworkCredential( m_SmtpLoginUserName , m_SmtpLoginPassword)
 

I modify the line so high by:

  Client.Credentials = NewSystem.Net.NetworkCredential ("sebastienduhaime@gmail.com", "xxxxxxxx")

But when I made the same change in the anvanced HMI emailer its not working.


Thank you very much for your help it is very appreciated!!!
Title: Re: Emailer threading
Post by: sebas on June 19, 2017, 03:32:41 PM
Did you test with anything other than gmail and did it work?
Title: Re: Emailer threading
Post by: Archie on June 20, 2017, 07:50:30 PM
I finally got to the bottom of this problem. It will be fixed in the next version.
Title: Re: Emailer threading
Post by: M4 on July 04, 2017, 02:05:36 AM
Hi Archie,
I am still having the same issue
Any update on this will be much appreciated.
Thanks
Manoj
Title: Re: Emailer threading
Post by: Phrog30 on July 04, 2017, 09:05:00 AM
Wrap the send command in a try,catch block, this will prevent the app from "crashing".  The error you are getting is probably in your email settings.  I suggest searching the web for your exact problem, there are tons of info out.  You mention you are still having problems, but this is your first post.  I have successfully emailed using Gmail with no problems, here is the code and settings that I used:

Code: [Select]
Private Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

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

    End Sub

    Private Sub Email()

        Using mail As New MailMessage
            Try
                mail.From = New MailAddress("xxxxxxxxxxx@gmail.com") 'Your email
                mail.To.Add(txt_Email_To.Text) ' Address to send to...
                mail.Body = txt_Email_Body.Text
            Catch
            End Try

            mail.Subject = txt_Email_Subject.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("xxxxxxxx@gmail.com", "password") 'Enter your email and password

                Try
                    SMTP.Send(mail)
                Catch ex As Exception
                    MsgBox(ex.Message)
                    Return
                End Try

                Email_Success()

            End Using
        End Using

    End Sub

    Private Sub Email_Success()

        MsgBox("Email has been sent successfully!")

    End Sub

You obviously will need to replace "xxxxxxxx@gmail.com" with your email and "password" with your password.