Sending Duplicate EMails


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
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("")
            Dim addrCC2 As New System.Net.Mail.MailAddress("")
            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("", "Preview Support")

            mail.To.Add(New MailAddress(""))

            mail.Subject = Now.Date & " - " & GetFileName(File) & " (" & ComboBoxEx1.Text & ")"
            mail.Body = MessageBody
            mail.Priority = Net.Mail.MailPriority.Normal
            mail.IsBodyHtml = True

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

            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 & ")")
                ListBox1.Items.Add("Failed " & GetFileName(File) & "!")
            End If

            ProgressBar1.Value += 1
            LabelX3.Text = ProgressBar1.Value & "/" & FileLength
    End Sub

Open in new window

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?
previewservicesAuthor Commented:
133 Files
133 Items in the ListBox
But i got around 164 emails.
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.

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
Are you sure the button is pressed only once? Does Files.Count return same number as FileLength?
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.