Getting an error when sending email through Gmail SMTP

梦想与她 提交于 2019-12-11 09:16:36

问题


Basically I have attempted to send an email when a button is pressed.

With the following code, I get an error that says something about 'The SMTP server requires a secure connection or the client was not authenticated'.

What is causing this error?

Imports System.Net.Mail    

   Private Sub Button1_Click_2(sender As Object, e As 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.EnableSsl = True
        SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
        SmtpServer.UseDefaultCredentials = False
        SmtpServer.Port = 587
        SmtpServer.Host = "smtp.gmail.com"
        mail = New MailMessage()
        mail.From = New MailAddress("MYEMAIL")
        mail.To.Add("SENDINGADRESS")
        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

回答1:


You're very close, you need to set the following properties as well.

SmtpServer.EnableSsl = True
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.UseDefaultCredentials = False


来源:https://stackoverflow.com/questions/20618368/getting-an-error-when-sending-email-through-gmail-smtp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!