2012-10-01 22 views
0
'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` 

錯誤是:vb.net發送郵件的SMTP和前景錯誤(單獨)

ERR1:

System.Runtime.InteropServices.COMException(0x80004005的):必須有至少一個名稱或收件人,抄送或密件抄送框中的通訊組列表。 在Microsoft.Office.Interop.Outlook._MailItem.Send() 在WindowsApplication1.Form1.Button1_Click(對象發件人,EventArgs的)

ERR2:

System.ArgumentException:參數 '地址' 不能一個空的字符串。 參數名稱:地址 在System.Net.Mail.MailAddress..ctor(字符串地址,字符串顯示名,編碼displayNameEncoding) 在WindowsApplication1.Form1.Button2_Click(對象發件人,EventArgs的)

當我發送使用機器與視覺工作室兩個郵件得到發送,當沒有這些錯誤顯示。

我有雙重檢查的.NET Framework

謝謝你..

+0

相同的結果帶或不帶這個代碼'昏暗的地址作爲新的MailAddress(TextBox1.Text,「Nigraan」)' – user1429371

回答

0

我得到的一切工作,

了SMTP通過提供SSL加密真正

有前景的工作創造一個接觸,並給予該聯繫人的電子郵件ID在工作「到」領域

只是不保存聯繫人如果u不希望在Outlook中添加聯繫人信息:d

yeyyy!

0

嘗試創建一個變量字符串,並設置發送郵件之前

Dim ToEmail as string 
ToEmail = Textbox1.text 

然後設置你先解決。

'assign values to the newMail MailItem 
    newMail.To = ToEmail 
    newMail = fdMail.Items.Add(Outlook.OlItemType.olMailItem) 
    newMail.Subject = "tesst" 
    newMail.Body = "test" 
+0

smtp正在通過把ssl轉換爲true ..將它放入一個變量後,仍然不工作還有,同樣的問題... – user1429371