我有一个工作的vb.net,它发送一封带有附件的电子邮件,其中包含来自字段的数据。但我有两个问题:
非常感谢您的帮助,谢谢。
我有一个附件按钮,您可以浏览:
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
答案 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