/*
         * public bool Notify(ICollection<User> users, Notification notif)
         * {
         *
         * foreach (User user in users)
         * {
         *   SourceRecipient current = CurrentRecipient;
         *   try
         *   {
         *       MailMessage message = new MailMessage();
         *       SmtpClient smtp = new SmtpClient();
         *       message.From = new MailAddress(current.Address);// this.From);
         *       message.To.Add(new MailAddress(user.Email));
         *       message.Subject = notif.Title;
         *       message.IsBodyHtml = this.IsBodyHtml;
         *       message.Body = notif.Content;
         *       smtp.Port = current.SmtpPort;// this.SmtpPort;
         *       smtp.Host = current.SmtpServer;// this.SmtpServer;
         *       smtp.EnableSsl = this.IsSSL;
         *       smtp.UseDefaultCredentials = false;
         *       smtp.Credentials = new NetworkCredential(current.Address, current.Password);// this.From, this.Password);
         *       smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
         *       smtp.Send(message);
         *   }
         *   catch (Exception ex)
         *   {
         *       return false;
         *   }
         * }
         * return true;
         * }*/
        public bool NotifyTogether(ICollection <User> users)
        {
            SourceRecipient current = CurrentRecipient;

            /*
             *      try
             *      {
             *          MailMessage message = new MailMessage();
             *          SmtpClient smtp = new SmtpClient();
             *          message.From = new MailAddress(current.Address);// this.From);
             *          Array.ForEach(users.ToArray<User>(), t => message.To.Add(new MailAddress(t.Email)));
             *          message.Subject = this.Subject;
             *          message.IsBodyHtml = this.IsBodyHtml;
             *          message.Body = this.Body;
             *          smtp.Port = current.SmtpPort;// this.SmtpPort;
             *          smtp.Host = current.SmtpServer;// this.SmtpServer;
             *          smtp.EnableSsl = this.IsSSL;
             *          smtp.UseDefaultCredentials = false;
             *          smtp.Credentials = new NetworkCredential(current.Address, current.Password);// this.From, this.Password);
             *          smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
             *          smtp.Send(message);
             *      }
             *      catch (Exception ex)
             *      {
             *          return false;
             *      }*/
            return(true);
        }
        public async Task <NotificationResponse> NotifyAsync(User user, Notification notif)
        {
            SourceRecipient current = CurrentRecipient;

            try
            {
                var message = new MimeMessage();
                message.From.Add(new MailboxAddress(current.Name, current.Address));
                message.To.Add(new MailboxAddress(user.Email));
                message.Subject = notif.Title;
                message.Body    = new TextPart("html")
                {
                    Text = notif.Content
                };

                using (var client = new SmtpClient())
                {
                    client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                    //if (_env.IsDevelopment())
                    //{
                    //    await client.ConnectAsync(_smtpSettings.Server, _smtpSettings.Port, true);
                    //}
                    //else
                    //{
                    await client.ConnectAsync(current.SmtpServer, current.SmtpPort, SecureSocketOptions.StartTls);

                    //}

                    await client.AuthenticateAsync(current.Address, current.Password);

                    await client.SendAsync(message);

                    await client.DisconnectAsync(true);
                }
                return(await Task.FromResult(new NotificationResponse
                {
                    NotificationId = notif.Id,
                    UserId = user.UserId,
                    Status = 3//sent
                }));
            }
            catch (Exception e)
            {
                //throw new InvalidOperationException(e.Message);
                if (trials < SysRecipients.Count)
                {
                    trials++;
                    return(await NotifyAsync(user, notif));
                }
                trials = 0;
                return(await Task.FromResult(new NotificationResponse
                {
                    NotificationId = notif.Id,
                    UserId = user.UserId,
                    Status = 2//ready
                }));
            }
        }
        public IList <NotificationResponse> Notify(ICollection <User> users, Notification notif)
        {
            IList <NotificationResponse> responses = new List <NotificationResponse>();

            foreach (User user in users)
            {
                SourceRecipient current = CurrentRecipient;
                try
                {
                    var message = new MimeMessage();
                    message.From.Add(new MailboxAddress(current.Name, current.Address));
                    message.To.Add(new MailboxAddress(user.Email));
                    message.Subject = notif.Title;
                    message.Body    = new TextPart("html")
                    {
                        Text = notif.Content
                    };

                    using (var client = new SmtpClient())
                    {
                        client.ServerCertificateValidationCallback = (s, c, h, e) => true;

                        //if (_env.IsDevelopment())
                        //{
                        //    client.Connect(_smtpSettings.Server, _smtpSettings.Port, true);
                        //}
                        //else
                        //{
                        client.Connect(current.SmtpServer, current.SmtpPort);
                        //}

                        client.Authenticate(current.Address, current.Password);
                        client.Send(message);
                        client.Disconnect(true);
                        responses.Add(new NotificationResponse
                        {
                            NotificationId = notif.Id,
                            UserId         = user.UserId,
                            Status         = 3//sent
                        });
                    }
                }
                catch (Exception e)
                {
                    //throw new InvalidOperationException(e.Message);
                    responses.Add(new NotificationResponse
                    {
                        NotificationId = notif.Id,
                        UserId         = user.UserId,
                        Status         = 2//ready
                    });
                }
            }

            return(responses);
        }