11

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

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
Emad-ud-deen
  • 4,734
  • 21
  • 87
  • 152
  • Code looks good. Maybe it is a UAC problem? – Keith Beard May 21 '12 at 18:14
  • Thanks for the reply. I tried it but it still times out. – Emad-ud-deen May 21 '12 at 18:27
  • 1
    matter a fact try to connect via command line telnet smtp.gmail.com 465, If you can connect than try setting UseDefaultCredentials = False – Keith Beard May 21 '12 at 18:38
  • Thanks kcbeard for the reply. I tried o smtp.gmail.com 465 in telnet and it seems to get stuck showing: connecting to smtp.gmail.com... and just showing a flashing cursor then it sais: Connection to host lost. I was able to connect to towel.blinkenlights.nl and that worked. Not sure why there is a problem with gmail. I use gmail in my email client software all the time without any problems. Is there an open source control I can use in my form that works with email? – Emad-ud-deen May 21 '12 at 21:02
  • I was able to get it working by using another email server. Not sure why there's a problem with Gmail. – Emad-ud-deen May 22 '12 at 01:34
  • 1
    I am guessing its probably not Google, It is strange that happened. What kind of environment are you working in. Do you guys have a firewall, or hard wall of some sorts? – Keith Beard May 22 '12 at 12:16
  • I'm working in a Parallels virtual machine running Windows7 on a Mac. I used bluebottle.com as the email server at that works perfectly. At least I know the code works. – Emad-ud-deen May 23 '12 at 13:26
  • We tried using MailBee and all email problems disappeared. Not sure why but I think it cycles through port numbers and finds the best one to use. I'm updating the code in the post to show MailBee in use. – Emad-ud-deen Jun 29 '12 at 09:13

2 Answers2

17

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.

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

Also make sure you have UseDefaultCredentials = False

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

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
-2

I had similar problem, in my case I just forgot to specify the protocol, so instead of smtp.gmail.com I had to put ssl://smtp.gmail.com.

dav
  • 8,931
  • 15
  • 76
  • 140