无法在电子邮件上发送多个附件,只能发送一个文件附件

时间:2013-02-13 15:37:34

标签: vb.net winforms visual-studio-2010 .net-4.0 email-attachments

我有一个工作的vb.net,它发送一封带有附件的电子邮件,其中包含来自字段的数据。但我有两个问题:

  1. 即使我选择了多个文件,我也无法将多个文件附加到电子邮件中。
  2. 我希望能够看到我附加的内容,以便在提交表单上的详细信息之前,如有必要,我可以删除附件。
  3. 非常感谢您的帮助,谢谢。

    我有一个附件按钮,您可以浏览:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim openDLG As New OpenFileDialog
    
        openDLG.AddExtension = True
        openDLG.ReadOnlyChecked = True
        openDLG.Multiselect = True
        openDLG.Title = "Select the file(s) you want added to the message..."
        openDLG.Filter = "All Files (*.*)|*.*"
    
        If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
    
            For Each item As String In openDLG.FileNames
    
                'Create a new System.NET.Mail.Attachment class instance for each file.
                attachToMsg = New System.Net.Mail.Attachment(item)
    
            Next
    
            MsgBox("I have finished adding all of the selected files! You can do more if you want!")
    
        End If
    

    然后我有一个提交按钮,可以从表单中发送所有信息,包括附件:

     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Using message As New MailMessage()
    
            message.From = (New MailAddress(TextBox3.Text.ToString()))
            message.[To].Add(New MailAddress("benjamin.boothe@experian.com"))
            message.Subject = "New commission query"
    
    
    
    
            message.Attachments.Add(attachToMsg)
    
            Dim MsgBody As String
            MsgBody = TextBox1.Text.ToString() & vbCr & _
                      TextBox2.Text.ToString() & vbCr & _
                      TextBox3.Text.ToString() & vbCr & _
                      ComboBox1.Text.ToString() & vbCr & _
                      ComboBox2.Text.ToString() & vbCr & _
                      ComboBox3.Text.ToString() & vbCr & _
                      ComboBox4.Text.ToString() & vbCr
            message.Body = MsgBody
            Dim client As New SmtpClient()
            client.Host = "mailhost"
            client.Send(message)
    
        End Using
    
        MessageBox.Show("Your request has been submitted!", "Congratulations!")
        'close form
        Me.Close()
    End Sub
    

    结束班

    jamman

1 个答案:

答案 0 :(得分:1)

您正在定义附件对象一次,并将每个文件分配给同一附件对象。您应该将附件定义为List(Of Attachement)

所以而不是

Private attachToMsg As System.Net.Mail.Attachment

这样做:

Private attachmentList As List(Of System.Net.Mail.Attachment)

然后当用户用户选择文件时:

attachmentList = New List(Of System.Net.Mail.Attachment)
If openDLG.ShowDialog = Windows.Forms.DialogResult.OK Then
    For Each item As String In openDLG.FileNames
        'Create a new System.NET.Mail.Attachment class instance for each file.
        attachmentList.add(New System.Net.Mail.Attachment(item))
    Next
    MsgBox("I have finished adding all of the selected files! You can do more if you want!")
End If

现在在发送代码中添加每个附件:

If Not attachmentList Is Nothing Then
    For Each attachment As System.Net.Mail.Attachment In attachmentList 
        message.Attachments.Add(attachment)
    Next
End If