问题
I have implemented a Send Mail Task in my package which sends a mail notification on Success or Failure. The send mail task fails sometimes due to the below error.
Task failed: Send Mail Task with Success
Error Code: -1073548540
ErrorMessage: An error occurred with the following error message: "Failure sending mail.
System.IO.IOException: Unable to read data from the transport connection:
An existing connection was forcibly closed by the remote host.
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host"
I have reported the issue to network admin but they suggested the following.
The errors you are receiving from Mailhub can happen occasionally when trying to open a connection.
The only way to resolve this issue is to force multiple retries. If you can, please try to code in ~3-4 retries in your app.
I am sure that it can be done through a script task. I am not sure whether I can implement the multiple tries in case of failure using send mail task.
I have already implemented 20 plus packages with send mail task. I try to implement this approach with minimal change.
I tried the with SQL Server Agent job step configuration, the user has the option of configuring the Retry attempts and the Retry intervals but it runs the whole package on failure which is not suitable for my scenario. I have to run only send mail task alone in case if it failed to send email with multiple tries.
回答1:
You mentioned C# option:
here is the logic you are looking for:
int retryCount = 0;
retry:
try
{
[Build and send your email]
}
catch
{
retryCount++;
if(retryCount<4) goto retry; //will try 4 times no matter what caused it to fall in to the catch
}
回答2:
To not modify your project much.
Create a new package with a for container where you control the number of repetitions.
Create a variable of type int.
Add a package execution task.
In case of correct execution, assign a value to the variable that breaks the for loop.
In case of error you can register it in database or whatever you want.
The image below is for ssis-2008 but it is the same for ssis-2012
来源:https://stackoverflow.com/questions/59576097/implement-send-mail-task-with-multiple-tries-in-case-if-its-failed-to-send-mail