Aggiunta gestione tentativo re-invio email smtp

This commit is contained in:
Samuele Locatelli
2022-06-13 10:26:18 +02:00
parent 0e71c32823
commit 1bebccfb49
+37 -31
View File
@@ -12,6 +12,7 @@ using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
namespace SteamWare
{
@@ -1067,26 +1068,6 @@ namespace SteamWare
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void compEvent(object sender, AsyncCompletedEventArgs e)
{
if (e.UserState != null)
{
logger.lg.scriviLog(e.UserState.ToString());
}
if (e.Cancelled)
{
logger.lg.scriviLog("Invio cancellato");
}
Console.Out.WriteLine("is it canceled? " + e.Cancelled);
if (e.Error != null)
{
logger.lg.scriviLog("Invio con errori: " + e.Error.Message);
}
}
// A simple call back function:
private void OnMailSent(object sender, AsyncCompletedEventArgs e)
{
if (e.UserState != null)
@@ -1148,11 +1129,19 @@ namespace SteamWare
emailer.SslType = SslMode.Ssl;
emailer.User = (_username);
emailer.Password = _password;
emailer.AuthenticationMode = AuthenticationType.Base64;
emailer.Timeout = 5000;
// The authentication types depends on your server, it can be plain, base 64 or none.
//if you do not need user name and password means you are using default credentials
// In this case, your authentication type is none
emailer.AuthenticationMode = AuthenticationType.Base64;
// verifico se ci sia timeout...
if (memLayer.ML.CRI("_smtpTimeout") > 0)
{
emailer.Timeout = memLayer.ML.CRI("_smtpTimeout");
}
else
{
emailer.Timeout = 5000;
}
emailer.MailMessage = mymessage;
emailer.SendCompleted += OnMailSent;
emailer.SendMailAsync();
@@ -1183,7 +1172,6 @@ namespace SteamWare
//_smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
_smtpClient.Credentials = new System.Net.NetworkCredential(_username, _password);
_smtpClient.EnableSsl = memLayer.ML.CRB("_enableSSL");
// verifico se ci sia timeout...
if (memLayer.ML.CRI("_smtpTimeout") > 0)
{
@@ -1303,15 +1291,32 @@ namespace SteamWare
public bool mandaEmail(string _mailFrom, string _mailTo, string _oggetto, string _corpo, AlternateView[] _allegati)
{
bool fatto = false;
//manda email...
try
// setup numero tentativi (default 3)
var reqRetry = memLayer.ML.CRI("_smtpMaxRetry");
int numTry = reqRetry > 0 ? reqRetry : 3;
// ciclo fino a che non viene inviato con successo...
while (!fatto || numTry > 0)
{
fatto = mandaEmailNoLog(_mailFrom, _mailTo, _oggetto, _corpo, _allegati);
logger.lg.scriviLog(string.Format("Email inviata: oggetto: {0}, corpo:{1}{1}{2}", _oggetto, Environment.NewLine, _corpo), tipoLog.INFO);
}
catch (Exception e)
{
logger.lg.scriviLog(string.Format("ERRORE! Email NON INVIATA!oggetto: {0} corpo: {1} eccezione:{2}{3}", _oggetto, _corpo, Environment.NewLine, e), tipoLog.EXCEPTION);
numTry--;
//manda email...
try
{
fatto = mandaEmailNoLog(_mailFrom, _mailTo, _oggetto, _corpo, _allegati);
logger.lg.scriviLog(string.Format("Email inviata: oggetto: {0}, corpo:{1}{1}{2}", _oggetto, Environment.NewLine, _corpo), tipoLog.INFO);
}
catch (Exception e)
{
logger.lg.scriviLog(string.Format("ERRORE! Email NON INVIATA!oggetto: {0} corpo: {1} eccezione:{2}{3}", _oggetto, _corpo, Environment.NewLine, e), tipoLog.EXCEPTION);
fatto = false;
}
// se non inviato pausa 250-750 msec...
if (!fatto)
{
Random rnd = new Random();
int nextWait = 250 + rnd.Next(500);
logger.lg.scriviLog($"Errore email non inviata, attesa di {nextWait}ms prima di riprovare, restano {numTry} tentativi");
Thread.Sleep(nextWait);
}
}
return fatto;
}
@@ -1330,6 +1335,7 @@ namespace SteamWare
// sostituisco eventuali a capo nel corpo messaggio...
_corpo = _corpo.Replace("\r", "<br />");
_corpo = _corpo.Replace("\n", "<br />");
//_useAIMSmtp
if (memLayer.ML.CRB("_useAIMSmtp"))
{