简体   繁体   English

通过Gmail SMTP发送电子邮件时出现错误

[英]Getting an error when sending email through Gmail SMTP

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'. 通过以下代码,我收到一条错误消息,内容为“ SMTP服务器需要安全连接或客户端未通过身份验证”。

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

You're very close, you need to set the following properties as well. 您非常接近,还需要设置以下属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM