如何在VB.NET中发送Gmail电子邮件?

5

我想发送一封电子邮件,但它出现了错误。

这是我的代码:

Sub sendMail(ByVal title As String, ByVal content As String)
    Dim SmtpServer As New SmtpClient("smtp.gmail.com", 25)
    SmtpServer.Credentials = New Net.NetworkCredential("name@gmail.com", "password")
    Dim mail As New MailMessage("name@gmail.com", "name@gmail.com", title, content)
    SmtpServer.Send(mail)
End Sub

我有一个try catch语句,尝试调用这个方法,但是失败了,所以catch语句执行并抛出异常:System.Net.Mail.SmtpException: SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.0必须首先发出STARTTLS命令。B6SM3176487LAE.0 - GSMTP为什么会出现这个错误?如何解决它?

6个回答

8

Gmail使用端口465上的SMTP over SSL。

尝试执行以下操作:


  Dim SmtpServer As New SmtpClient("smtp.gmail.com", 465)
  ...
  SmtpServer.EnableSsl = True
  ...

是的,那就是我认为端口的问题,谢谢。 - Arbitur
好的,我使用的端口是正确的,但我需要的是EnableSsl。 - Arbitur

5
尝试这个 - 我知道它有效。
    Dim Mail As New MailMessage
    Dim SMTP As New SmtpClient("smtp.gmail.com")

    Mail.Subject = "Security Update"
    Mail.From = New MailAddress("name@gmail.com")
    SMTP.Credentials = New System.Net.NetworkCredential("name@gmail.com", "password") '<-- Password Here

    Mail.To.Add(address & "@gmail.com") 'I used ByVal here for address

    Mail.Body = "" 'Message Here

    SMTP.EnableSsl = True
    SMTP.Port = "587"
    SMTP.Send(Mail)

1
需要注意的是SMTP端口。我原以为它是587,而不是465或25。 - nick
那么应该是 587 还是 465? - David Doria

0

谷歌账户出现了一些问题,您需要关闭一些安全设置。在反复发送电子邮件后,我在我的一个支持账户(用于谷歌)上收到了电子邮件,内容如下:

You recently changed your security settings so that your Google Account [trdjoko@gmail.com] is no longer protected by modern security standards. 

If you did not make this change 
Please review your Account Activity page at https://security.google.com/settings/security/activity to see if anything looks suspicious. Whoever made the change knows your password; we recommend that you change it right away. 

If you made this change 
Please be aware that it is now easier for an attacker to break into your account. You can make your account safer again by undoing this change at https://www.google.com/settings/security/lesssecureapps then switching to apps made by Google such as Gmail to access your account.   
Sincerely,
The Google Accounts team 

所以我关闭了额外的安全性,它就正常工作了。


0

一个非常简单的方法(不需要更改任何安全设置)是使用IFTTT和我的IFTTT Maker.net库

首先,在IFTTT中创建一个新的recipe,由Maker频道触发,并将事件命名为“send_gmail”。

然后,选择Gmail引擎,点击“发送电子邮件”,并用{{value1}}替换To,{{value2}}替换主题,{{value3}}替换消息/正文。

之后,在Visual Studio中,将ifttt.vb添加到您的项目中。现在是代码:

      Try
    makechannel.scode = "your account ID"
    makechannel.fireevent("send_gmail", "TO", "SUBJECT", "MESSAGE")
   'code goes here if done
    Catch ex As Exception
        'code goes here if it fails
    End Try

然后填写您的帐户ID。您可以在ifttt.com/maker上找到它。

就这样!


0

将端口更改为587。端口25不支持SSL。


0

我已经编写了一个可以轻松执行此任务的类。

Imports System.Net.Mail
Public Class GGSMTP_GMAIL
    Dim Temp_GmailAccount As String
    Dim Temp_GmailPassword As String
    Dim Temp_SMTPSERVER As String
    Dim Temp_ServerPort As Int32
    Dim Temp_ErrorText As String = ""
    Dim Temp_EnableSSl As Boolean = True
    Public ReadOnly Property ErrorText() As String
        Get
            Return Temp_ErrorText
        End Get
    End Property
    Public Property EnableSSL() As Boolean
        Get
            Return Temp_EnableSSl
        End Get
        Set(ByVal value As Boolean)
            Temp_EnableSSl = value
        End Set
    End Property
    Public Property GmailAccount() As String
        Get
            Return Temp_GmailAccount
        End Get
        Set(ByVal value As String)
            Temp_GmailAccount = value
        End Set
    End Property
    Public Property GmailPassword() As String
        Get
            Return Temp_GmailPassword
        End Get
        Set(ByVal value As String)
            Temp_GmailPassword = value
        End Set
    End Property
    Public Property SMTPSERVER() As String
        Get
            Return Temp_SMTPSERVER
        End Get
        Set(ByVal value As String)
            Temp_SMTPSERVER = value
        End Set
    End Property
    Public Property ServerPort() As Int32
        Get
            Return Temp_ServerPort
        End Get
        Set(ByVal value As Int32)
            Temp_ServerPort = value
        End Set
    End Property
    Public Sub New(ByVal GmailAccount As String, ByVal GmailPassword As String, Optional ByVal SMTPSERVER As String = "smtp.gmail.com", Optional ByVal ServerPort As Int32 = 587, Optional ByVal EnableSSl As Boolean = True)
        Temp_GmailAccount = GmailAccount
        Temp_GmailPassword = GmailPassword
        Temp_SMTPSERVER = SMTPSERVER
        Temp_ServerPort = ServerPort
        Temp_EnableSSl = EnableSSl
    End Sub
    Public Function SendMail(ByVal ToAddressies As String(), ByVal Subject As String, ByVal BodyText As String, Optional ByVal AttachedFiles As String() = Nothing) As Boolean
        Temp_ErrorText = ""
        Dim Mail As New MailMessage
        Dim SMTP As New SmtpClient(Temp_SMTPSERVER)
        Mail.Subject = Subject
        Mail.From = New MailAddress(Temp_GmailAccount)
        SMTP.Credentials = New System.Net.NetworkCredential(Temp_GmailAccount, Temp_GmailPassword) '<-- Password Here
        Mail.To.Clear()
        For i As Int16 = 0 To ToAddressies.Length - 1
            Mail.To.Add(ToAddressies(i))
        Next i
        Mail.Body = BodyText
        Mail.Attachments.Clear()

        If AttachedFiles IsNot Nothing Then
            For i As Int16 = 0 To AttachedFiles.Length - 1
                Mail.Attachments.Add(New Attachment(AttachedFiles(i)))
            Next
        End If

        SMTP.EnableSsl = Temp_EnableSSl
        SMTP.Port = Temp_ServerPort

        Try
            SMTP.Send(Mail)
            Return True
        Catch ex As Exception
            Me.Temp_ErrorText = ex.Message.ToString
            Return False
        End Try

    End Function
End Class

这是关于如何使用类的方法:

 Dim GGmail As New GGSMTP_GMAIL("MyFromAddress1@gmail.com", "AccPassword", )

        Dim ToAddressies As String() = {"ToAddress1@gmail.com", "ToAddress2@gmail.com"}
        Dim attachs() As String = {"d:\temp_Excell226.xlsx", "d:\temp_Excell224.xlsx", "d:\temp_Excell225.xlsx"}
        Dim subject As String = "My TestSubject"
        Dim body As String = "My text goes here ...."
        Dim result As Boolean = GGmail.SendMail(ToAddressies, subject, body, attachs)
        If result Then
            MsgBox("mails sended successfully", MsgBoxStyle.Information)
        Else
            MsgBox(GGmail.ErrorText, MsgBoxStyle.Critical)
        End If 

希望这可以帮助到您。编程愉快!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接