2011-01-06 79 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客户端)