2009-12-15 115 views

回答

2

Send email in vb.net

或者

Imports System.Net.Mail 
Public Class Form1 
    Private Sub Button1_Click(ByVal sender As System.Object, _ 
     ByVal e As System.EventArgs) Handles Button1.Click 
     Try 
      Dim SmtpServer As New SmtpClient() 
      Dim mail As New MailMessage() 
      SmtpServer.Credentials = New _ 
       Net.NetworkCredential("[email protected]", "password") 
      SmtpServer.Port = 587 
      SmtpServer.Host = "smtp.gmail.com" 
      mail.From = New MailAddress("[email protected]") 
      mail.To.Add("TOADDRESS") 
      mail.Subject = "Test Mail" 
      mail.Body = "This is for testing SMTP mail from GMAIL" 
      SmtpServer.Send(mail) 
      MsgBox("mail send") 
     Catch ex As Exception 
      MsgBox(ex.ToString) 
     End Try 
    End Sub 
End Class 
+1

看起來正確的,但我會建議使用'郵件作爲MAILMESSAGE新()'MAILMESSAGE以來實現IDisposable – MarkJ 2009-12-15 12:18:48

+0

此外,代碼實例化一個新的兩次MAILMESSAGE。 – 2009-12-15 14:30:13

+1

這是一種典型的編程式編程方式 - 通過異常進行切換直至「有效」。通常你會看到每個方法都以try開始,並以捕獲基本異常結束。我們有一個開發人員會這樣做。在每種方法中,用戶都喜歡將這些作爲額外的獎勵,如果在異常消息中有單引號,會有一個js錯誤,頁面不會正確執行 – Shawn 2009-12-15 14:37:50