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

时间:2013-12-16 18:41:51

标签: asp.net vb.net email smtp gmail

基本上我在按下按钮时尝试发送电子邮件。

使用以下代码,我收到一条错误消息,指出“SMTP服务器需要安全连接或客户端未经过身份验证”。

导致此错误的原因是什么?

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 个答案:

答案 0 :(得分:1)

你非常接近,你也需要设置以下属性。

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