0

フィールドからのデータも添付して受信者に電子メールを送信するvb.netが機能しています。しかし、私には2つの問題があります:

  1. 複数のファイルを選択しても、複数のファイルをメールに添付できません。
  2. フォームに詳細を送信する前に、必要に応じて添付ファイルを削除できるように、添付ファイルを確認できるようにしたいと思います。

どうぞよろしくお願いいたします。

参照できる添付ファイル ボタンがあります。

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

クラス終了

ジャムマン

4

1 に答える 1

1

添付オブジェクトを 1 回定義し、各ファイルを同じ添付オブジェクトに割り当てます。添付ファイルを として定義する必要がありますList(Of Attachement)

だから代わりに

Private attachToMsg As System.Net.Mail.Attachment

これを行う:

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

次に、ユーザー user がファイルを選択すると:

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
于 2013-02-13T16:37:09.100 に答える