Пример #1
0
		private void OnMailSent(SendMailEventArgs e)
		{
			var sent = MailSendCompleted;
			if (sent != null) sent(this, e);
		}
Пример #2
0
		private SendMailEventArgs SendMail(DeliveryMessagePo deliveryMessage)
		{
			var message = CreateMailMessage(deliveryMessage);
		    if (message.To.Count > 0 || message.CC.Count > 0 || message.Bcc.Count > 0)
		    {
		        try
		        {
		            if (UseAuth)
		            {
		                Smtpclient.Username = SmtpLogin.UserName;
		                Smtpclient.Password = SmtpLogin.Password;
		                _log.DebugFormat("Using credential UserName:{0} Password:{1}", SmtpLogin.UserName, SmtpLogin.Password);
		            }

		            Smtpclient.Send(message);
		            _log.Debug("Send mail sync");
		            IsBusy = false;

		            var sendMailEventArgs = new SendMailEventArgs(message, SmtpServer, null);
		            OnMailSent(sendMailEventArgs);

		            return sendMailEventArgs;
		        }
		        catch (SmtpException ex)
		        {
		            var destinatario = string.Join(",", message.To) + string.Join(",", message.CC) + string.Join(",", message.Bcc);
		            var exmessage = $"Srv:{SmtpServer}{Environment.NewLine}UseAuth:{UseAuth}{Environment.NewLine}AuthMthd:{Smtpclient.AuthenticationMethod}{Environment.NewLine}Usr:{Smtpclient.Username}{Environment.NewLine}Pwd:{Smtpclient.Password}{Environment.NewLine}F:{message.From}{Environment.NewLine}T:{destinatario}{Environment.NewLine}Sbj:{message.Subject}{Environment.NewLine}Msg:{message.Body}";

		            var backupSent = false;
		            if (UseBackupServer)
		            {
		                _log.WarnFormat("Using SmtpBackupServer: {0}", ex, exmessage);
		                backupSent = SendMailByBackupServer(message);
		            }
		            else
		            {
		                IsBusy = false;
		                _log.Error(exmessage, ex);
		            }

		            if (!backupSent)
		            {
		                var sendMailEventArgs = new SendMailEventArgs(message, null, ex);

		                if (ThrowException)
		                    throw new Exception(exmessage, ex);
		                else
		                    OnMailNotSent(sendMailEventArgs);

		                return sendMailEventArgs;
		            }
		            else
		            {
		                var sendMailEventArgs = new SendMailEventArgs(message, Config.BackupSmtpServer, null);
		                OnMailSent(sendMailEventArgs);
		                return sendMailEventArgs;
		            }
		        }
		        finally
		        {
		            message.Dispose();
		        }
		    }
		    else
		    {
                return new SendMailEventArgs(null, null, new InvalidDataException("Non è presente nessun destinatario"));
            }
		}
Пример #3
0
		private void OnMailNotSent(SendMailEventArgs e)
		{
			var sent = MailNotSent;
			if (sent != null) sent(this, e);
		}