2012-05-21 15 views
11

Testujemy kod do wysyłania wiadomości e-mail przy użyciu Gmaila z formularza, ale otrzymujemy błąd przekroczenia limitu czasu.Wysyłanie wiadomości e-mail przy użyciu Gmaila daje błąd przekroczenia limitu czasu

Czy możesz nam powiedzieć, czego brakuje w tym kodzie, aby wysłać wiadomość e-mail?

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 

Aktualizacja: Zmiana kodu za pomocą MailBee. W ten sposób wysyłamy wiadomości e-mail do wszystkich klientów:

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

Kod wygląda dobrze. Może to problem UAC? –

+0

Dzięki za odpowiedź. Próbowałem, ale nadal nie działa. –

+1

ważne jest, aby połączyć się poprzez linię poleceń telnet smtp.gmail.com 465, Jeśli możesz się połączyć, spróbuj ustawić UseDefaultCredentials = False –

Odpowiedz

13

Spróbuj użyć innego numeru portu. Nie można używać portu 465 z System.Net.Mail, ponieważ obsługuje on tylko "jawny SSL". Zajrzyj na stronę this page, aby uzyskać więcej informacji na ten temat.

Gmail zaakceptuje port 25 lub 587 podczas wysyłania poczty za pośrednictwem VB.NET ale czasy się za pomocą portu 465.

także upewnić się, że UseDefaultCredentials = False

Również spojrzeć na this example, w jaki sposób do wysyłania poczty przy użyciu GMail w C# może dać ci więcej wskazówek.

+0

Dzięki za pomoc. :-) –

+0

Tak, miałem ten sam problem podczas korzystania z portu 465. Naprawiłem to za pomocą 587 – Yohannes

-1

Miałem podobny problem, w moim przypadku po prostu zapomniałem podać protokół, więc zamiast smtp.gmail.com musiałem wstawić ssl://smtp.gmail.com.

Powiązane problemy