AdvancedHMI Software
General Category => Support Questions => Topic started 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:
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
-
Did you try leaving out this code:
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.
-
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. :)
-
This works pretty well:
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?
-
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?
-
Windows will automatically determine where to send the email packet based on the IP resolution and the settings of the ports.
-
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
-
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
-
Can you post the values you are using in the Emailer properties?
-
Attach a screenshot
thanks and best regard!!
-
Add a button, then in the click event add this code:
Emailer1.SendMessage()
This will force a send to see if the settings work.
-
hello Archie,
This is an email send by a code its works well
view screnshot.
thanks!!
-
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
-
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?
-
Sorry i translated it
thanks!!
-
Download and extract the attached solution. Open the Form1 and set the properties, then see what result you get from this.
-
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!!
-
When it stops at the exception, hover over "Client" and see if UseDefauleCredentials is False and Credentials are set to something.
-
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.
-
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.
-
Credentials = {System.Net.NetworkCredential}
UseDefaultCredentials = False
-
What happens if you use the code in reply #3? Assuming you are using Gmail, just input your specific info.
-
I do not understand what you mean?
-
Go to reply #3 in this thread and copy the code, paste it in your application and try it, obviously using your email settings.
-
I did it and it works very well its the emailer function which wants not to work with the same email setting.
-
Ok, now I don't understand. That code is an emailer.
-
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.
-
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!!!
-
Did you test with anything other than gmail and did it work?
-
I finally got to the bottom of this problem. It will be fixed in the next version.
-
Hi Archie,
I am still having the same issue
Any update on this will be much appreciated.
Thanks
Manoj
-
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:
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.