使用Gmail发送电子邮件给出超时错误 [英] Sending email using Gmail gives a time out error

查看:224
本文介绍了使用Gmail发送电子邮件给出超时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We are testing some code to send email messages using Gmail from a form, but get a time out error.

你能告诉我们这个代码缺少什么发送电子邮件?

Can you tell us what is missing from this code to get the email message sent?

    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进行代码更改。这是我们向所有客户发送电子邮件的方式:

Update: Code change using MailBee. This is how we are doing emails to all the customers:

    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


推荐答案

尝试使用其他端口号。您不能使用端口465与 System.Net.Mail ,因为它只支持显式SSL。有关这方面的更多信息,请查看此页面

Try using a different port number. You cannot use port 465 with System.Net.Mail as it only supports "Explicit SSL". Have a look at this page for more info on this.

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

Gmail will accept port 25 or 587 when sending mail via VB.NET but times out using port 465.

还要确保您有 UseDefaultCredentials = False

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

Also have a look at this example on how to send mail using GMail in C# it might give you some more clue.

这篇关于使用Gmail发送电子邮件给出超时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆