因此,這裏是我的代碼,用於從統一發送電子郵件使用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
}
我只是不知道如何實現這一點。
謝謝你閱讀我的問題。
非常感謝!您的評論引導我走向我的解決方案。嘗試抓住工作,但smtpServer.Dispose();沒有工作。不過,我試過mail.Dispose();它的工作。 –