2012-05-21 24 views
11

Estamos probando algún código para enviar mensajes de correo electrónico usando Gmail desde un formulario, pero obtenemos un error de tiempo de espera.El envío de correo electrónico usando Gmail da un error de tiempo de espera

¿Puede decirnos qué falta en este código para que se envíe el mensaje de correo electrónico?

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

     SmtpServer.EnableSsl = True 
     SmtpServer.Credentials = New Net.NetworkCredential("[email protected]", "MyPasswordGoesHere") 
     SmtpServer.Port = 465 
     SmtpServer.Host = "smtp.gmail.com" 

     mail.From = New MailAddress("[email protected]") 
     mail.To.Add("[email protected]") 
     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 

Actualización: cambio de código usando MailBee. Así es como estamos enviando correos electrónicos a todos los clientes:

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 
+0

El código se ve bien. Tal vez es un problema de UAC? –

+0

Gracias por la respuesta. Lo intenté pero aún se agotó. –

+1

importa un hecho intente conectarse a través de la línea de comando telnet smtp.gmail.com 465, si puede conectarse, intente configurar UseDefaultCredentials = False –

Respuesta

13

Pruebe usar un número de puerto diferente. No puede usar el puerto 465 con System.Net.Mail ya que solo es compatible con "SSL explícito". Eche un vistazo a this page para más información sobre esto.

Gmail aceptará el puerto 25 o 587 al enviar el correo a través de VB.NET, pero el tiempo de espera utilizando el puerto 465.

También asegúrese de que tiene UseDefaultCredentials = False

también echar un vistazo a this example sobre cómo enviar correo usar GMail en C# podría darte más pistas.

+0

Gracias por la ayuda. :-) –

+0

Sí, tuve el mismo problema cuando uso el puerto 465. Lo arreglé usando 587 – Yohannes

-1

Tuve un problema similar, en mi caso me olvidé de especificar el protocolo, así que en lugar de smtp.gmail.com tuve que poner ssl://smtp.gmail.com.

Cuestiones relacionadas