2013-04-06 37 views
1

我已經徹底搜索了網絡的解決方案,並且還沒有找到。 我試圖建立一個簡單的郵件客戶端發送文本文件作爲原始文本通過SMTP。爲此,我在VB.Net版本3.5下使用System.Net.Mail庫。System.Net.Mail VB.NET 3.5 System.Net.Mail.SmtpException

每當我嘗試發送消息時,都會收到一個System.Net.Mail.SmtpException。 我曾嘗試使用不同的郵件提供商,在不同的設置下,還沒有解決這個問題。這個問題已經在3臺獨立的計算機上測試過,所有計算機都有不同的規格 的代碼如下:

Private Sub SendEmailMessage() 
Try 
If My.Computer.Network.IsAvailable Then 
     If DataValid() Then 
       'Data is valid, send the eMail. 
       Dim MailReceiver As String = txtReceiver.Text 
       'Create the eMail Message. 
       'Dim message As New MailMessage(from:=txtMailAddress.Text, to:=MailReceiver, subject:="(MTG) <CARD> " & ID, body:="<p>" & ConstructedFileString() & "<p>") 
       Dim message As New MailMessage 
       Dim MessageFrom As New MailAddress(txtMailAddress.Text) 
       Dim MessageTo As New MailAddress(MailReceiver) 
       With message 
        .From = MessageFrom 
        .To.Add(MailReceiver) 
        .Subject = "(MTG) CARD " & ID 
        .IsBodyHtml = True 
        .Body = "<p>" & ConstructedFileString() & "<p>" 
       End With 
       'Establish eMail Client 
       Dim emailClient As New SmtpClient() 
       Dim emailCredentials As New Net.NetworkCredential 

       With emailCredentials 
        .UserName = txtMailAddress.Text 
        .Password = txtPassword.Text 
        .Domain = "gmail.com" 
       End With 

       With emailClient 
        .Host = txtHostServer.Text 
        .Port = txtPort.Text 
        .Credentials = emailCredentials 
        .EnableSsl = chkSSL.Checked 
        .Timeout = 5000 
       End With 

       'Dim MailDomain As String = "" 
       'Dim PositionAt As Byte = 0 
       'PositionAt = txtMailAddress.Text.IndexOf("@") 
       'For i = PositionAt + 1 To Len(txtMailAddress.Text) 
       ' MailDomain = MailDomain & txtMailAddress.Text.Chars(i) 
       'Next 
       'Debug.Print(MailDomain) 

       If My.Computer.Network.Ping(hostNameOrAddress:=emailClient.Host) Then 
        'Send the message. 
        emailClient.Send(message) 
       Else 
        'Could not ping, do not send. 
        ErrorOut("Could not reach the eMail Server.") 
       End If 


      Else 
       'Data is not valid, do not send the eMail. 
      End If 
    Else 
     ErrorOut("No network could be found. Check your network configurations.") 
    End If 
Catch ex As Security.SecurityException 
    'Security exception 
    ErrorOut("A Security Exception was raised.") 
Catch ex As Net.NetworkInformation.NetworkInformationException 
    'Network info exception 
    ErrorOut("Exception raised when retrieving network information.") 
Catch ex As Net.NetworkInformation.PingException 
    'Ping exception 
    ErrorOut("Exception raised when pinging the network.") 
Catch ex As Net.Mail.SmtpFailedRecipientException 
    'Recipient exception 
    ErrorOut("Mail Recipient Exception raised.") 
Catch ex As Net.Mail.SmtpException 
    'Mail Server Exception 
    ErrorOut("Mail Server Exception raised") 
Catch ex As Exception 
    'Generic Exception raised. 
    ErrorOut("General Exception raised", True) 
End Try 
End Sub 

在這個問題上的任何幫助是極大的讚賞,謝謝。

堆棧跟蹤:

System.Net.Mail.SmtpException: The operation has timed out. 
at System.Net.Mail.SmtpClient.Send(MailMessage message) 
at NetProj.EmailDialog.SendEmailMessage() in ... 
+2

什麼是異常的細節,包括內部異常?這是需要的信息。 – Oded 2013-04-06 18:58:37

+0

專業提示 - 將異常變量'ex'傳遞給'ErrorOut'並使用'ex.ToString()'獲取堆棧跟蹤,內部異常等。 – Oded 2013-04-06 19:01:47

+0

您有沒有在這些機器上安裝mcafee防病毒軟件?我曾遇到同樣的問題,但這是由於防病毒。 – Raman 2013-04-06 19:04:17

回答

1

我相信這就是著名的SSL問題與System.Net.Mail

http://blogs.msdn.com/b/webdav_101/archive/2008/06/02/system-net-mail-with-ssl-to-authenticate-against-port-465.aspx

這是那裏的框架3.5不知道4.0,如果他們修復它或不。

+0

猜猜我必須嘗試4.0然後;感謝你的回答。我不知道搜索時怎麼沒有出現,再次感謝^^ – 2013-04-06 19:30:33

+0

很高興我能幫上忙。我記得這是我的一位朋友面臨同樣的問題,最後他不得不使用一些第三方庫。如果有幫助,請標記爲答案和贊成。感謝:) – Raman 2013-04-06 19:37:48

+0

我發現使用VS2008使我處於一個相當不利的地位,我有.NET 4.0 Framework ...但我無法引用它。 我不認爲他們發佈2010年作爲免費軟件尚未... 與微軟的樂趣時間:D 而我需要15代表upvote :(否則我已經。 – 2013-04-06 19:40:27