2012-05-21 11 views
11

हम किसी फॉर्म से जीमेल का उपयोग करके ईमेल संदेश भेजने के लिए कुछ कोड का परीक्षण कर रहे हैं, लेकिन समय निकालने में त्रुटि प्राप्त करें।जीमेल का उपयोग करके ईमेल भेजना त्रुटि का समय देता है

क्या आप हमें बता सकते हैं कि ईमेल संदेश प्राप्त करने के लिए इस कोड से क्या गुम है?

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 

अद्यतन: मेलबी का उपयोग करके कोड परिवर्तन। इस प्रकार हम सभी ग्राहकों को ईमेल कर रहे हैं:

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

कोड अच्छा दिखता है। शायद यह एक यूएसी समस्या है? –

+0

उत्तर के लिए धन्यवाद। मैंने कोशिश की लेकिन यह अभी भी समय समाप्त हो गया। –

+1

कोई फर्क नहीं पड़ता कमांड लाइन टेलनेट smtp.gmail.com 465 के माध्यम से कनेक्ट करने का प्रयास करें, यदि आप UseDefaultCredentials = False –

उत्तर

13

एक अलग पोर्ट नंबर का उपयोग करने का प्रयास करें। आप पोर्ट 465 का उपयोग System.Net.Mail के साथ नहीं कर सकते क्योंकि यह केवल "स्पष्ट एसएसएल" का समर्थन करता है। इस पर अधिक जानकारी के लिए this page पर एक नज़र डालें।

जीमेल 465

यह भी सुनिश्चित करें आप UseDefaultCredentials = False

भी कैसे मेल भेजने के लिए पर this example पर एक नजर है बनाने के पोर्ट 25 या 587 को स्वीकार करेंगे जब VB.NET लेकिन कई बार के माध्यम से मेल भेजने पोर्ट का उपयोग कर बाहर सी # में जीमेल का उपयोग करके यह आपको कुछ और सुराग दे सकता है।

+0

सहायता के लिए धन्यवाद। :-) –

+0

हाँ पोर्ट 465 का उपयोग करते समय मुझे एक ही समस्या थी। मैंने इसे 587 का उपयोग करके तय किया – Yohannes

-1

मुझे इसी तरह की समस्या थी, मेरे मामले में मैं बस प्रोटोकॉल निर्दिष्ट करना भूल गया था, इसलिए smtp.gmail.com के बजाय मुझे ssl://smtp.gmail.com डालना पड़ा।

संबंधित मुद्दे