Sending Duplicate EMails

Posted on 2010-01-11
Medium Priority
Last Modified: 2012-05-08

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("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.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")

            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

Question by:previewservices
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 52

Expert Comment

by:Carl Tawn
ID: 26281889
How many entries are being output in your listbox? In other words, is your code sending the duplicate, or is it your smtp server?

Author Comment

ID: 26281898
133 Files
133 Items in the ListBox
But i got around 164 emails.
LVL 52

Accepted Solution

Carl Tawn earned 1000 total points
ID: 26281951
Then it sounds more like your SMTP server is sending out duplicates, rather than them being sent by your code.
LVL 83

Expert Comment

ID: 26303821
Are you sure the button is pressed only once? Does Files.Count return same number as FileLength?

Featured Post

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question