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過,但仍然它不工作
看看內部異常 - 告訴我們它是什麼。 – 2011-01-06 12:55:31
你有什麼異常?你的MessageBox裏有什麼? – 2011-01-06 12:55:55