在按下我的提交按钮之前,我在将所选文件附加到我的 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)
'Then add the attachment to your message. You have to do this everytime you run the code
'above.
EmailMessage.Attachments.Add(attachToMsg)
Next
MsgBox("I have finished adding all of the selected files! You can do more if you want!")
End If
End Sub
按钮3代码:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using message As New MailMessage()
'set to the from, to and subject fields
message.From = (New MailAddress(TextBox3.Text.ToString()))
message.[To].Add(New MailAddress("NO.ONE@elsewhere.com"))
message.Subject = "New commission query"
'code the message body
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
'display submitted box
MessageBox.Show("Your request has been submitted!", "Congratulations!")
'close form
Me.Close()
End Sub