从VB 2012发送电子邮件

时间:2014-08-12 14:33:41

标签: vb.net

我想从我的表单发送消息,但我收到以下错误:

The SMTP server requires a secure connection or the client was not authenticated. 
The server response was: 5.7.0 Must issue a STARTTLS command first. mw4sm56776703wic.20 - gsmtp

这是我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim email As New MailMessage
    Dim smtp As New SmtpClient
    email.From = New MailAddress("my gmail account")
    email.Subject = "Hello!"
    email.To.Add("the one I want to send it to")
    smtp.Port = 25    'i use an aruba email, it work with other application'
    smtp.Host = "smtp.gmail.com"
    smtp.Credentials = New Net.NetworkCredential("the gmail account i used", "pwd i have hided it")
    smtp.Send(email)
End Sub

注意:我使用的是Visual Studio 2012。

2 个答案:

答案 0 :(得分:2)

您提到了一个Gmail帐户和gmail smtp服务器。 Gmail不允许未加密的smtp访问,这是25端口的用途。您需要使用端口465,并告诉.Net进行正确的加密:

smtp.Port = 465
smtp.EnableSSL = True

答案 1 :(得分:0)

看起来您可能需要启用安全连接:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim email As New MailMessage
    Dim smtp As New SmtpClient
    email.From = New MailAddress("my gmail account")
    email.Subject = "Hello!"
    email.To.Add("the one I want to send it to")
    smtp.Port = 465 ' From Joel's answer, encryption requires port 465 rather than 25
    smtp.Host = "smtp.gmail.com"
    smtp.Credentials = New Net.NetworkCredential("the gmail account i used", "pwd i have hided it")
    smtp.EnableSsl = True ' *** Add this line ***
    smtp.Send(email)
End Sub