Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#smtpfailedrecipientsexception;如果发生这种情况,如何退出函数_C#_Email_Unity3d - Fatal编程技术网

C#smtpfailedrecipientsexception;如果发生这种情况,如何退出函数

C#smtpfailedrecipientsexception;如果发生这种情况,如何退出函数,c#,email,unity3d,C#,Email,Unity3d,下面是我的代码,用于使用C#脚本从Unity发送电子邮件: 此代码在发送电子邮件时运行良好。但是,如果我输入了错误的电子邮件,我将收到“smtpfailedrecipientsexception”消息 在此之后,即使输入正确的电子邮件地址也不起作用。SMTPFailedRecipientSexException将继续发生,除非您第一次键入更正 我想添加一些If语句,比如我用伪代码编写的: If smtpserver.send(mail)returns smtp error { Exit this

下面是我的代码,用于使用C#脚本从Unity发送电子邮件:

此代码在发送电子邮件时运行良好。但是,如果我输入了错误的电子邮件,我将收到“smtpfailedrecipientsexception”消息

在此之后,即使输入正确的电子邮件地址也不起作用。SMTPFailedRecipientSexException将继续发生,除非您第一次键入更正

我想添加一些If语句,比如我用伪代码编写的:

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

我只是不知道如何实现这一点

SmtpClient使用池来减少创建到服务器的新连接的开销。(见:)

我的假设是
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
        }
}

使用异常处理方法来处理运行时异常:

        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

        }

以下是有效的代码,供将来参考:

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
}

非常感谢你!你的评论引导我找到了解决办法。try-catch有效,但smtpServer.Dispose()无效;他没有工作。但是,我尝试了mail.Dispose();谢谢你的帮助,mail.Dispose();正是我所需要的,我把你的答案和约翰的帖子结合起来,得出了一个解决方案!最好在finally块中使用disposing实现。最好使用
using
语句,而不是手动处理
MailMessage
对象
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
}