简体   繁体   English

使用SMTP通过gmail发送电子邮件

[英]Sending email through gmail using SMTP

I am trying to send an email through following code 我正在尝试通过以下代码发送电子邮件

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

However this code is not working I get following exceptions: 但是此代码不起作用,我得到以下异常:

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

And finally delivery of message fails. 最终消息传递失败。

Any idea why it is not working? 知道为什么它不起作用吗?

NOTE : I have tried SMTPServer.Port = 587 too but still its not working 注意:我也尝试了SMTPServer.Port = 587,但仍然无法正常工作

In order to isolate port problem do the following test: Start>run> type there "cmd"> enter. 为了隔离端口问题,请执行以下测试:开始>运行>键入“ cmd”,然后输入。 in the black screen type: telnet smtp.gmail.com 465 . 在黑屏中,键入: telnet smtp.gmail.com 465
if you get this message: 如果收到此消息:

Connecting To smtp.gmail.com...Could not open connection to the host, on port 465: Connect failed

it means you network administrator closed this port to outside connection. 这意味着您的网络管理员已将该端口关闭到外部连接。 If you get black empty screen it means the connection is OK. 如果黑屏,则表示连接正常。 if the command telnet is not available for you, you have to add this service (search Google how to add telnet client) 如果您无法使用telnet命令,则必须添加此服务(搜索Google如何添加telnet客户端)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM