发送使用Gmail电子邮件给超时错误(Sending email using Gmail gives

2019-06-24 15:50发布

我们正在测试一些代码来使用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

Answer 1:

尝试使用不同的端口号。 不能使用端口465, System.Net.Mail ,因为它仅支持“显式SSL”。 看看这个页面以获取更多的信息。

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

另外,还要确保你有UseDefaultCredentials = False

也看看这个例子就如何在C#中使用Gmail它可能给你一些线索来发送邮件。



Answer 2:

我有类似的问题,在我来说,我只是忘了指定协议,所以不是smtp.gmail.com我只好把ssl://smtp.gmail.com



文章来源: Sending email using Gmail gives a time out error