Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

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

0
previewservices
Asked:
previewservices
  • 2
1 Solution
 
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
 
CodeCruiserCommented:
Are you sure the button is pressed only once? Does Files.Count return same number as FileLength?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now