【问题标题】:System.Net.Mail.SmtpException The SMTP server requires a secure connection or the client was not authenticatedSystem.Net.Mail.SmtpException SMTP 服务器需要安全连接或客户端未通过身份验证
【发布时间】:2012-03-06 00:15:43
【问题描述】:

我正在尝试从我的 vb.net 表单应用程序发送 SMTP 电子邮件。应用此代码时,我收到以下错误。我做错了什么?

代码:

Imports System.Net.Mail

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim SmtpServer As New SmtpClient()
            Dim mail As New MailMessage()
            SmtpServer.Credentials = New  _
            Net.NetworkCredential("myemail@gmail.com", "mypassword")
            SmtpServer.Port = 587
            SmtpServer.Host = "smtp.gmail.com"
            mail = New MailMessage()
            mail.From = New MailAddress("myemail@gmail.com")
            mail.To.Add("sendto@hotmail.co.uk")
            mail.Subject = "Test Mail"
            mail.Body = "This is for testing SMTP mail from GMAIL"
            SmtpServer.Send(mail)
            MsgBox("mail send")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Class

错误:

System.Net.Mail.SmtpException:SMTP 服务器需要安全 连接或客户端未通过身份验证。服务器响应 是:5.7.0 必须先发出 STARTTLS 命令

【问题讨论】:

    标签: vb.net email smtp gmail system.net.mail


    【解决方案1】:
    1. 检查登录名和密码是否正确;
    2. 使用 SmtpServer.EnableSsl = true;
    3. Gmail默认禁用使用凭据(用户和密码)访问,您需要转到此页面:https://www.google.com/settings/security/lesssecureapps和“启用不太安全的应用程序”,即:“启用应用程序使用用户和密码登录”。更多细节在这里:https://support.google.com/accounts/answer/6010255

    【讨论】:

      【解决方案2】:

      看看这是否有帮助;添加

      SmtpServer.EnableSSL= true
      

      【讨论】:

        【解决方案3】:

        我知道这是去年的事,但我想我应该公布答案,因为五分钟前我遇到了同样的问题。

        基本上您的登录凭据不正确,需要更改。

        也感谢之前的回答,这段代码允许我通过 SSL 加密发送电子邮件(我希望 LOL)。

        【讨论】:

          猜你喜欢
          • 2023-03-21
          • 2011-05-28
          • 2017-12-02
          • 2015-11-26
          • 2016-09-25
          • 2019-01-28
          相关资源
          最近更新 更多