2011-01-06 100 views
1

我想通過下面的代碼來發送電子郵件發送電子郵件通過Gmail使用SMTP

CODE


Dim MyMailMessage As New MailMessage() 
    'From requires an instance of the MailAddress type 
    MyMailMessage.From = New MailAddress("[email protected]") 
    'To is a collection of MailAddress types 
    MyMailMessage.To.Add("[email protected]") 
    MyMailMessage.Subject = "GMail Test" 
    MyMailMessage.Body = "This is the test text for Gmail email" 
    'Create the SMTPClient object and specify the SMTP GMail server 
    Dim SMTPServer As New SmtpClient("smtp.gmail.com") 
    SMTPServer.Port = 465 
    SMTPServer.Credentials = New System.Net.NetworkCredential("[email protected]", "*****") 
    SMTPServer.EnableSsl = True 
    Try 
     SMTPServer.Send(MyMailMessage) 
     MessageBox.Show("Email Sent") 
    Catch ex As SmtpException 
     MessageBox.Show(ex.Message) 
    End Try 

但是這個代碼不工作 我得到以下情況例外:

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll 
A first chance exception of type 'System.Net.WebException' occurred in System.dll 
A first chance exception of type 'System.Net.WebException' occurred in System.dll 
A first chance exception of type 'System.Net.WebException' occurred in System.dll 
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll 
A first chance exception of type 'System.Net.Mail.SmtpException' occurred in MailSender.exe 

最後郵件傳遞失敗。

任何想法爲什麼它不工作?

注:我已經試過SMTPServer.Port = 587過,但仍然它不工作

+0

看看內部異常 - 告訴我們它是什麼。 – 2011-01-06 12:55:31

+0

你有什麼異常?你的MessageBox裏有什麼? – 2011-01-06 12:55:55

回答

1

爲了隔離端口問題做如下測試: 開始>運行>鍵入有「CMD」>輸入。黑屏中的 類型:telnet smtp.gmail.com 465
,如果你得到這個消息:

Connecting To smtp.gmail.com...Could not open connection to the host, on port 465: Connect failed 

這意味着你的網絡管理員關閉此端口外部連接。 如果您獲得黑色空白屏幕,則表示連接正常。 如果您的命令telnet不可用,您必須添加此服務(搜索Google如何添加telnet客戶端)