尝试通过.NET函数发送电子邮件

时间:2015-01-02 07:01:05

标签: vb.net

所以,我正在尝试使用下面的代码发送电子邮件,我收到此错误:

System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. 

我已经允许不安全的应用程序向我发送电子邮件,并在gmail上关闭了我的两步验证,当然我用真实的电子邮件和密码替换了Net.NetworkCredential("myvalidemail@gmail.com", "mypassword"),但我仍然得到那个错误。
我通过VB调试运行程序,但这不重要。

Private Sub SendBtn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles SendBtn.Click
    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()
        SmtpServer.UseDefaultCredentials = False
        SmtpServer.Credentials = New  _
    Net.NetworkCredential("myvalidemail@gmail.com", "mypassword")
        SmtpServer.Port = 587
        SmtpServer.Host = "smtp.gmail.com"
        mail = New MailMessage()
        mail.From = New MailAddress("myvalidemail@gmail.com")
        mail.To.Add("myvalidemail@gmail.com")
        mail.Subject = "Test Mail"
        mail.Body = "This is for testing SMTP mail from GMAIL"
        mail.Priority = MailPriority.High
        SmtpServer.Send(mail)
        MsgBox("mail send")
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub    

2 个答案:

答案 0 :(得分:0)

例外情况说SMTP服务器需要安全连接。尝试启用SSL

SmtpServer.EnableSsl = true;

答案 1 :(得分:0)

“客户端未经过身份验证” 只是一个想法:你使用谷歌双因素授权吗?