2011-03-30 92 views
1

我寫了下面的代碼從VB.net窗體發送電子郵件。 這裏是我的代碼從VB.net發送電子郵件Windows窗體

Try 
     Dim message As System.Net.Mail.MailMessage 
     Dim smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com") 
     Dim fromMailAddress As System.Net.Mail.MailAddress 
     Dim toMailAddress As System.Net.Mail.MailAddress 

     fromMailAddress = New System.Net.Mail.MailAddress("[email protected]") 
     toMailAddress = New System.Net.Mail.MailAddress("[email protected]") 

     message = New System.Net.Mail.MailMessage() 
     message.From = toMailAddress 
     message.To.Add(fromMailAddress) 
     message.Subject = "TestFromVB" 
     message.Body = "Test Message" 

     smtp.EnableSsl = True 
     smtp.UseDefaultCredentials = False 
     smtp.Credentials = New System.Net.NetworkCredential("[email protected]", "password") 
     smtp.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network 

     smtp.Send(message) 

     MessageBox.Show("sent...") 

    Catch ex As Exception 
     MessageBox.Show("error" + ex.Message + "\n" + ex.InnerException.ToString()) 
    End Try 

每當我點擊一個按鈕發送,應該發送電子郵件到指定的地址。卜這段代碼是給一些錯誤說無法連接到遠程計算機....

這裏是例外 enter image description here

截圖有人可以幫我解決這個問題。或者請建議您是否有任何工作樣本。

回答

4

我認爲你需要使用不同的端口號或者587465

this GMail文件。

  • 接收郵件(POP3)服務器 - 需要SSL:pop.gmail.com

    • 使用SSL:Yes
    • 端口:995
  • 發送郵件(SMTP )Ser版本 - 需要TLS或SSL: smtp.gmail.com

    • 使用驗證:Yes
    • 端口爲TLS/STARTTLS:587
    • SSL的端口:465
    • 帳戶名稱:your full email address (including @gmail.com or @your_domain.com)
    • 郵箱地址:your email address ([email protected] or [email protected]_domain.com)
    • 密碼:your Gmail password
0

對於其他人誰絆倒在這個問題上,除了需要的端口是正確的,因爲其他的答案中指出,很多時候谷歌不會從Vb.net程序不進行身份驗證更改您的Google帳戶中的設置以允許不太安全的連接。 link to google settings