2017-09-13 106 views
0

因此,這裏是我的代碼,用於從統一發送電子郵件使用C#腳本:C#smtpfailedrecipientsexception;如何退出功能,如果發生這種情況

public void SendMail() // Mail send function 
    { 
     string emailAddress; // variable to store user inputted email 
     emailAddress = emailInput.text; // variable becomes the email the user types in 
     mail.From = new MailAddress("hiddenfornow"); 
     mail.To.Add(emailAddress); 
     SmtpClient smtpServer = new SmtpClient("smtp.gmail.com"); 
     smtpServer.Port = 587; 
     mail.Subject = "Test Subject" + currentDate; 
     mail.IsBodyHtml = true; // allows for html 
     mail.Body = "Testing testing"; 
     smtpServer.Credentials = new System.Net.NetworkCredential("hiddenfornow", "hiddenfornow") as ICredentialsByHost; 
     smtpServer.EnableSsl = true; 
     SceneManager.LoadScene("TestScene"); // reloads the scene after user clicks button   
     ServicePointManager.ServerCertificateValidationCallback = 
     delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
     { return true; }; 
     smtpServer.Send(mail); 
     Debug.Log("success"); 
} 

此代碼工作正常發送電子郵件。但是,如果我輸入了錯誤的電子郵件,我將收到「smtpfailedrecipientsexception」消息。

在此之後,即使輸入正確的電子郵件地址也不起作用。 smtpfailedrecipientsexception將繼續發生,除非您第一次輸入正確。

我想補充某種If語句,如這裏面我寫的僞代碼:

If smtpserver.send(mail)returns smtp error 
{ 
Exit this function 
} 
else 
{ 
success message 
} 

我只是不知道如何實現這一點。

謝謝你閱讀我的問題。

回答

0

SmtpClient使用池來減少創建到服務器的新連接的開銷。 (參見:https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx#Remarks

我的假設是,SmtpFailedRecientsException是把連接成一個狀態不好,所以你需要強制連接客戶端配置關閉:

public void SendMail() // Mail send function 
    { 
     //your code... 

     SmtpClient smtpServer = new SmtpClient("smtp.gmail.com"); 

     try { 
      //.... your code continues..... 
      smtpServer.Send(mail); 
      Debug.Log("success"); 
     } catch (SmtpFailedRecipientsException) { //or, perhaps any Exception 
      smtpServer.Dispose(); 
      throw; //rethrow the exception, assuming you're handling it in the calling code 
     } 
} 
+0

非常感謝!您的評論引導我走向我的解決方案。嘗試抓住工作,但smtpServer.Dispose();沒有工作。不過,我試過mail.Dispose();它的工作。 –

1

使用異常處理方法處理運行時異常:

 try 
     { 
      if (smtpserver.send(mail)) 
       return "successful"; 
     } 
     catch (SmtpFailedRecipientException ex) 
     { 
      // log your error 
      return ex.StatusCode; // return status code as you will know actual error code 
     } 
     finally 
     { 
      mail.Dispose(); // Dispose your mailmessage as it will clears any stream associated with your mail message such as attachment 

     } 

Available Status Codes

+0

感謝您的幫助,mail.Dispose();是我需要的,我將你的答案與John的帖子結合起來,以達成解決方案! –

+0

@DavidForster:很高興,它可以幫助你! –

0

以供將來參考,在這裏是工作的代碼:

Try 
{ 
smtpServer.Send(mail); // Attempts to send the email 
Debug.Log("success"); 
} 
catch (SmtpFailedRecipientsException) // Catches send failure 
{ 
mail.Dispose(); // ends the SMTP connection 
SceneManager.LoadScene("SceneName"); //Reloads the scene to clear textboxes 
} 
+0

最好在finally塊中配置實現。最好如果你使用'using'語句而不是手動配置'MailMessage'對象 –

相關問題