【问题标题】:emailing listboxitems by orders按订单通过电子邮件发送列表框项
【发布时间】:2015-08-21 00:02:08
【问题描述】:

我有一个从文件系统观察器获取其项目的列表框。每次将项目添加到列表框时,我希望能够自动通过电子邮件将项目一一发送。这是我的代码问题是只有第一个项目是通过电子邮件发送的

Private Sub FileSystemWatcher1_Created(sender As Object, e As FileSystemEventArgs) Handles FileSystemWatcher1.Created
    If DeimosRadioButton1.Enabled = True Then
        ListBox1.Items.Add(e.FullPath.ToString)
        Label2.Hide()
        If ListBox1.Items.Count > 0 Then
            Dim Counter As Integer = 0

            Dim Mail As New MailMessage
            Mail.Subject = "HACK REPORT!"
            Mail.To.Add("@gmail.com")
            Mail.From = New MailAddress("@gmail.com")
            Mail.Body = "Proof is attached in this email"

            Dim Attachment As System.Net.Mail.Attachment

                Attachment = New Attachment(ListBox1.Items(Counter).ToString)
                Mail.Attachments.Add(Attachment)



            Dim SMTP As New SmtpClient("smtp.gmail.com")
            SMTP.EnableSsl = True
            SMTP.Credentials = New System.Net.NetworkCredential(Label4.Text, Label5.Text)
            SMTP.Port = "587"
            SMTP.Send(Mail)
        End If
    End If

End Sub

【问题讨论】:

  • 如果在将文件名添加到列表框之前通过电子邮件发送文件名(e.FullPath - 不需要 ToString),则无需跟踪任何内容。 FSW 在其自己的线程上运行,因此您可以将项目#10 发送两次,而从不发送#9。照原样,Counter 始终为零并发送第一项

标签: .net vb.net visual-studio-2010


【解决方案1】:

正如 plutonix 所说,您的 COUNTER 永远不会增加,因为它不是任何形式的 FOR/NEXT 或 DO/LOOP

Private Sub FileSystemWatcher1_Created(sender As Object, e As FileSystemEventArgs) Handles FileSystemWatcher1.Created
'
If DeimosRadioButton1.Enabled = True Then
  ListBox1.Items.Add(e.FullPath.ToString)
  Label2.Hide()
  If ListBox1.Items.Count > 0 Then
    Dim Counter As Integer = 0
    For Counter = 0 to Listbox1.Items.Count - 1
      Dim Mail As New MailMessage
      Mail.Subject = "HACK REPORT!"
      Mail.To.Add("@gmail.com")
      Mail.From = New MailAddress("@gmail.com")
      Mail.Body = "Proof is attached in this email"
      '
      Dim Attachment As System.Net.Mail.Attachment
      '
      Attachment = New Attachment(ListBox1.Items(Counter).ToString)
      Mail.Attachments.Add(Attachment)
      '
      Dim SMTP As New SmtpClient("smtp.gmail.com")
      SMTP.EnableSsl = True
      SMTP.Credentials = New System.Net.NetworkCredential(Label4.Text, Label5.Text)
      SMTP.Port = "587"
      SMTP.Send(Mail)
      'DoEvents/Yield system time to the sending or other events
    Next
  End If
End If
'
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多