我正在尝试通过以下代码发送电子邮件
CODE
Dim MyMailMessage As New MailMessage()
'From requires an instance of the MailAddress type
MyMailMessage.From = New MailAddress("a@gmail.com")
'To is a collection of MailAddress types
MyMailMessage.To.Add("a@something.com")
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("a@gmail.com", "*****")
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 :(得分:1)
为了隔离端口问题,请执行以下测试:
开始>&运行GT;键入“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客户端)