使用vb.net发送邮件时出错

时间:2017-06-13 10:51:01

标签: vb.net

错误是SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.0必须首先发出STARTTLS命令。 t77sm26908892pfg.102 - gsmtp

          Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            SmtpServer.Credentials = New  _
        Net.NetworkCredential("user@gmail.com", "user1")
            mail = New MailMessage()
            mail.From = New MailAddress("user@gmail.com")

            mail.To.Add("user2@gmail.com")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")

1 个答案:

答案 0 :(得分:1)

正如Codexer在评论中所说,您未通过设置SmtpServer.EnableSsl = True

来启用SSL
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Port = 587
SmtpServer.EnableSsl = True ' <---- this
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.Credentials = New  _
Net.NetworkCredential("user@gmail.com", "user1")
mail = New MailMessage()
mail.From = New MailAddress("user@gmail.com")
mail.To.Add("user2@gmail.com")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
SmtpServer.Send(mail)
MsgBox("mail send")

最好不要通过互联网以纯文本形式发送您的凭据......