'Variable which will send the mail
Dim obj As System.Net.Mail.SmtpClient
'Variable to store the attachments
Dim Attachment As System.Net.Mail.Attachment
'Variable to create the message to send
Dim Mailmsg As New Mail.MailMessage()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim ol As New Outlook.Application()
Dim ns As Outlook.NameSpace
Dim fdMail As Outlook.MAPIFolder
ns = ol.GetNamespace("MAPI")
ns.Logon(, , True, True)
'creating a new MailItem object
Dim newMail As Outlook.MailItem
'gets defaultfolder for my Outlook Outbox
fdMail = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)
'assign values to the newMail MailItem
newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem)
newMail.Subject = "tesst"
newMail.Body = "test"
newMail.To = TextBox1.Text
Dim sSource As String = Application.StartupPath + "\kk.sys"
' TODO: Replace with attachment name
Dim sDisplayName As String = "kaar.jpg"
Dim sBodyLen As String = newMail.Body.Length
newMail.SaveSentMessageFolder = fdMail
newMail.Send()
Catch ex As Exception
Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err1.txt")
writer.WriteLine(ex.ToString)
End Using
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Dim address As New MailAddress(TextBox1.Text, "Nigraan")
Dim oAttch As Mail.Attachment = New Mail.Attachment(Application.StartupPath + "\kk.sys")
SmtpServer.Credentials = New _
Net.NetworkCredential(TextBox2.Text, TextBox3.Text)
SmtpServer.Port = "587"
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress(TextBox2.Text)
mail.To.Add(New MailAddress(TextBox1.Text))
mail.Subject = TextBox3.Text
mail.Body = "test"
mail.Attachments.Add(oAttch)
SmtpServer.Send(mail)
Catch ex As Exception
Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err2.txt")
writer.WriteLine(ex.ToString)
End Using
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
System.Diagnostics.Process.Start("mailto:" & TextBox1.Text & "?subject=" & "re:Subject" & "&body=" & "EmailBody")
Catch ex As Exception
Using writer As StreamWriter = New StreamWriter(Application.StartupPath + "\err3.txt")
writer.WriteLine(ex.ToString)
End Using
End Try
End Sub`
错误是:
错误1:
System.Runtime.InteropServices.COMException (0x80004005):收件人、抄送或密件抄送框中必须至少有一个名称或分发列表。在 Microsoft.Office.Interop.Outlook._MailItem.Send() 在 WindowsApplication1.Form1.Button1_Click(对象发件人,EventArgs e)
错误2:
System.ArgumentException:参数“地址”不能为空字符串。参数名称:地址在 System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) at WindowsApplication1.Form1.Button2_Click(Object sender, EventArgs e)
当我使用带有 Visual Studio 的机器发送时,两封邮件都会被发送,而当这些错误不显示时。
我已经仔细检查了 .net 框架
谢谢你..