问题 使用Gmail发送电子邮件会导致超时错误


我们正在测试一些代码,以便从表单中使用Gmail发送电子邮件,但会收到超时错误。

您能否告诉我们此代码中缺少什么来发送电子邮件?

    Try
        Dim SmtpServer As New SmtpClient()
        Dim mail As New MailMessage()

        SmtpServer.EnableSsl = True
        SmtpServer.Credentials = New Net.NetworkCredential("ouremail@gmail.com", "MyPasswordGoesHere")
        SmtpServer.Port = 465
        SmtpServer.Host = "smtp.gmail.com"

        mail.From = New MailAddress("ouremail@gmail.com")
        mail.To.Add("ouremail@gmail.com")
        mail.Subject = "Test Mail"
        mail.Body = "This is for testing SMTP mail from GMAIL"

        SmtpServer.Send(mail)

        MsgBox("mail sent")

    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

更新: 使用MailBee进行代码更改。这就是我们向所有客户发送电子邮件的方式:

    Dim strSqlStatement As String = "Select CustomerName, Email " & _
                               "From Customers " & _
                              "Where Email Is Not Null"
    If IsConnected() Then

        ' Set up the sql command and lookup the parent.
        '----------------------------------------------
        Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)

            With objSqlCommand

                ' Open the SqlConnection before executing the query.
                '---------------------------------------------------
                Cursor = Cursors.WaitCursor

                ObjConnection.Open()

                Dim objDataReader As SqlDataReader = .ExecuteReader()

                ' Go through all the customers and send out the promotion emails.
                '----------------------------------------------------------------
                If objDataReader.HasRows Then

                    MailBee.Global.LicenseKey = "My license key goes here."

                    Dim objSMTP As New Smtp
                    Dim server As New SmtpServer(TextBoxSMTPServer.Text, TextBoxUserName.Text, TextBoxPassword.Text)

                    'SmtpServer.Host = TextBoxSMTPServer.Text
                    'SmtpServer.Port = TextBoxPort.Text
                    'SmtpServer.Timeout = 100

                    'If TextBoxUseSSL.Text = "Yes" Then
                    '    SmtpServer.EnableSsl = True
                    'Else
                    '    SmtpServer.EnableSsl = False
                    'End If

                    'If TextBoxUseDefaultCredentials.Text = "Yes" Then
                    '    SmtpServer.UseDefaultCredentials = True
                    'Else
                    '    SmtpServer.UseDefaultCredentials = False
                    'End If

                    'SmtpServer.Credentials = New Net.NetworkCredential(TextBoxUserName.Text, TextBoxPassword.Text)


                    objSMTP.SmtpServers.Clear()
                    objSMTP.SmtpServers.Add(server)

                    While objDataReader.Read()
                        If objDataReader("Email").ToString <> "" Then

                            objSMTP.Message.From.AsString = TextBoxEmailFrom.Text
                            objSMTP.Message.To.AsString = objDataReader("Email").ToString
                            objSMTP.Message.Subject = "Promotion: " & TextBoxID.Text
                            objSMTP.Message.BodyPlainText = "Dear " & objDataReader("CustomerName") & "," & vbCrLf & vbCrLf & TextBoxPromotionBodyText.Text

                            Try
                                objSMTP.Send()

                            Catch exBadPassword As MailBeeSmtpLoginBadCredentialsException
                                MsgBox("The login name or password is not correct.", MsgBoxStyle.Exclamation, "Email")
                                blnThereWereErrors = True

                            Catch exBadFromAddress As MailBeeSmtpRefusedSenderException
                                MsgBox("The sender email must be the same as the user's email address.", MsgBoxStyle.Exclamation, "Email")
                                blnThereWereErrors = True

                            Catch ex As Exception
                                MsgBox(ex.Message)
                                blnThereWereErrors = True
                            End Try
                        End If

                        If blnThereWereErrors Then
                            Exit While
                        End If
                    End While

                    If blnThereWereErrors = False Then
                        MessageBox.Show("Mass emailing has completed." & vbCrLf, _
                                "Email Message.", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Information)
                    End If
                End If

                objDataReader.Close()
                ObjConnection.Close()

                Cursor = Cursors.Default
            End With ' objSqlCommand
        End Using ' objSqlCommand

7269
2018-05-21 17:45


起源

代码看起来不错。也许这是一个UAC问题? - Keith Beard
谢谢回复。我尝试过但它仍然超时。 - Emad-ud-deen
事实上尝试连接命令行telnet smtp.gmail.com 465,如果你可以连接而不是尝试设置UseDefaultCredentials = False - Keith Beard
谢谢kcbeard的回复。我在telnet上尝试过o smtp.gmail.com 465,它似乎卡住了显示:连接到smtp.gmail.com ...只是显示一个闪烁的光标然后它就是sais:连接到主机丢失了。我能够连接到towel.blinkenlights.nl,这很有效。不确定为什么gmail有问题。我一直在我的电子邮件客户端软件中使用gmail而没有任何问题。我的表单中是否有可以使用电子邮件的开源控件? - Emad-ud-deen
我猜它可能不是谷歌,发生这种奇怪。你在做什么样的环境。你们有防火墙,还是某种类型的硬墙? - Keith Beard


答案:


尝试使用其他端口号。你不能使用端口465 System.Net.Mail 因为它只支持“显式SSL”。看一下 这一页 有关这方面的更多信息。

当通过VB.NET发送邮件时,Gmail将接受端口25或587但是使用端口465超时。

还要确保你有 UseDefaultCredentials = False

另外看看 这个例子 关于如何使用C#中的GMail发送邮件,它可能会给你一些更多的线索。


14
2018-06-28 12:21



谢谢您的帮助。 :-) - Emad-ud-deen
是的我在使用端口465时遇到了同样的问题。我使用587修复了它 - Yohannis


我有类似的问题,在我的情况下,我只是忘了指定协议,所以而不是 smtp.gmail.com 我不得不放 ssl://smtp.gmail.com


-1
2018-06-23 14:08