Sending Duplicate EMails

Hello,

In my code below when it send files as emails, it sometime sends duplicate emails. For example it might send:
Email 1, Email 2, Email 3, Email 3, Email 4, Email 5, Email 5

It is quite random and has no pattern. Anyone have any ideas?
Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX2.Click
        Dim heartBeat As New Thread(AddressOf Me.RunProcess)
        heartBeat.IsBackground = True
        heartBeat.Start()
End Sub

Private Sub RunProcess()
        Dim files As String()
        Dim File As String

        files = IO.Directory.GetFiles(TextBox1.Text)
        FileLength = System.IO.Directory.GetFiles(TextBox1.Text).Length()
        ProgressBar1.Maximum = FileLength
        ProgressBar1.Value = 0
        LabelX3.Text = "0/" & FileLength

        If MsgBox("Found " & FileLength & " file(s) in the directory. This program will send ALL files in this directory. " & vbCrLf & "Do you want to send the emails?", MsgBoxStyle.YesNo + MsgBoxStyle.Question) = MsgBoxResult.No Then
            Exit Sub
        End If


        For Each File In files
            Dim mail As MailMessage = New MailMessage
            Dim attach As New Attachment(File)
            Dim addrCC As New System.Net.Mail.MailAddress("email1@mydomain.com")
            Dim addrCC2 As New System.Net.Mail.MailAddress("email2@mydomain.com")
            Dim MsgFail As Integer = 0

            Dim MessageBody As String = "<span style=""font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px"">Hello,<br><br>" & _
                "Please find attached " & GetFileName(File) & ">"

            mail.From = New MailAddress("support@previewservices.com", "Preview Support")

         
            mail.To.Add(New MailAddress("sendto@mydomain.com"))
 

            mail.Subject = Now.Date & " - " & GetFileName(File) & " (" & ComboBoxEx1.Text & ")"
            mail.Body = MessageBody
            mail.CC.Add(addrCC.Address)
            mail.CC.Add(addrCC2.Address)
            mail.Attachments.Add(attach)
            mail.Priority = Net.Mail.MailPriority.Normal
            mail.IsBodyHtml = True

            Dim client As SmtpClient = New SmtpClient("smtp.mydomain.com", 2525)
            client.EnableSsl = False
            client.Credentials = New System.Net.NetworkCredential("USERNAME", "PASSWORD")

            Try
                client.Send(mail)
            Catch ex As Exception
                MsgFail = 1
                MessageBox.Show("Sending email failed. Please Try again (" & GetFileName(File) & ")" & vbCrLf & ex.Message)
            End Try

            If MsgFail = 0 Then
                ListBox1.Items.Add("Sent " & GetFileName(File) & " (" & ComboBoxEx1.Text & ")")
            Else
                ListBox1.Items.Add("Failed " & GetFileName(File) & "!")
            End If


            ProgressBar1.Value += 1
            LabelX3.Text = ProgressBar1.Value & "/" & FileLength
        Next
        MsgBox("Done!")
    End Sub

Open in new window

previewservicesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
How many entries are being output in your listbox? In other words, is your code sending the duplicate, or is it your smtp server?
0
previewservicesAuthor Commented:
133 Files
133 Items in the ListBox
But i got around 164 emails.
0
Carl TawnSystems and Integration DeveloperCommented:
Then it sounds more like your SMTP server is sending out duplicates, rather than them being sent by your code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
Are you sure the button is pressed only once? Does Files.Count return same number as FileLength?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.