Exemplo n.º 1
0
 public string Load()
 {
     try {
         connection.Open();
         SqlCommand     command = new SqlCommand(@"SELECT * FROM MailMessages ORDER BY [Id] DESC", connection);
         List <NewMail> xx      = new List <NewMail>();
         SqlDataReader  reader  = command.ExecuteReader();
         while (reader.Read())
         {
             NewMail x = new NewMail()
             {
                 id          = reader.GetInt32(0),
                 email       = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1),
                 groupEmails = JsonConvert.DeserializeObject <List <GroupEmails> >(reader.GetString(2)),
                 subject     = reader.GetValue(3) == DBNull.Value ? "" : reader.GetString(3),
                 message     = reader.GetValue(4) == DBNull.Value ? "" : reader.GetString(4),
                 date        = reader.GetValue(5) == DBNull.Value ? DateTime.Today : reader.GetDateTime(5)
             };
             xx.Add(x);
         }
         connection.Close();
         string json = JsonConvert.SerializeObject(xx, Formatting.Indented);
         return(json);
     }
     catch (Exception e) { return("Error: " + e); }
 }
Exemplo n.º 2
0
        public void WhenNewMailHaveNoTopic_ShouldHaveError()
        {
            var validator = new NewMailValidator();

            #region Init variables
            List <string> CCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            List <string> BCCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            string topic = "";
            string text  = "testtext";
            #endregion
            NewMail newMail = new NewMail
            {
                CCRecipientsAddresses  = CCRecipientsAddresses,
                BCCRecipientsAddresses = BCCRecipientsAddresses,
                Topic = topic,
                Text  = text,
            };
            var result = validator.TestValidate(newMail);
            #region Tests
            result.ShouldHaveValidationErrorFor(x => x.Topic);
            #endregion
        }
Exemplo n.º 3
0
        public async Task AddMail(int userID, NewMail newMail)
        {
            if (newMail.BCCRecipientsAddresses != null)
            {
                newMail.BCCRecipientsAddresses = newMail.BCCRecipientsAddresses.Distinct().ToList();
            }
            if (newMail.CCRecipientsAddresses != null)
            {
                newMail.CCRecipientsAddresses = newMail.CCRecipientsAddresses.Distinct().ToList();
            }
            CheckRecipientsAddressesCorrectness(newMail.BCCRecipientsAddresses, newMail.CCRecipientsAddresses);

            var transaction = _context.Database.BeginTransaction();

            try
            {
                int mailID = AddNewMailToDB(userID, newMail.Topic, newMail.Text);

                AddMailRecipientsToDB(newMail.CCRecipientsAddresses, RecipientType.CC, mailID);
                AddMailRecipientsToDB(newMail.BCCRecipientsAddresses, RecipientType.BCC, mailID);

                if (IfAttachments(newMail.Files))
                {
                    await StoreMailAttachmentsOnAzureBlob(mailID, newMail.Files);
                }

                transaction.Commit();

                await NotifyRecipients(newMail.BCCRecipientsAddresses, newMail.CCRecipientsAddresses, IfAttachments(newMail.Files));
            }
            catch (Exception)
            {
                transaction.Rollback();
            }
        }
Exemplo n.º 4
0
        public void WhenNewMailHaveToLongText_ShouldHaveError()
        {
            var validator = new NewMailValidator();

            #region Init variables
            List <string> CCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            List <string> BCCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            string        topic         = "testtopic";
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < validator.textMaxLength + 1; i++)
            {
                stringBuilder.Append('x');
            }
            string text = stringBuilder.ToString();
            #endregion
            NewMail newMail = new NewMail
            {
                CCRecipientsAddresses  = CCRecipientsAddresses,
                BCCRecipientsAddresses = BCCRecipientsAddresses,
                Topic = topic,
                Text  = text,
            };
            var result = validator.TestValidate(newMail);
            #region Tests
            result.ShouldHaveValidationErrorFor(x => x.Text);
            #endregion
        }
Exemplo n.º 5
0
    public string SendNewMail(NewMail mail, List <Group> groups, string sitename)
    {
        mail.groupEmails = GetEmailsFromGroups(groups);
        mail.date        = DateTime.Today;
        MailSettings settings = GetMailSettings(sitename);

        try {
            SmtpClient Smtp_Server = new SmtpClient();
            Smtp_Server.UseDefaultCredentials = false;
            Smtp_Server.Credentials           = new NetworkCredential(settings.email, settings.password);
            Smtp_Server.Port = Convert.ToInt32(settings.serverPort);


            Smtp_Server.EnableSsl = true;
            Smtp_Server.Host      = settings.serverHost;
            MailMessage mailMessage = new MailMessage();
            if (!string.IsNullOrWhiteSpace(mail.email))
            {
                mailMessage.To.Add(mail.email);
            }
            foreach (var x in mail.groupEmails)
            {
                mailMessage.To.Add(x.email);
            }
            mailMessage.From    = new MailAddress(settings.email);
            mailMessage.Subject = mail.subject;
            mailMessage.Body    = mail.message;
            Smtp_Server.Send(mailMessage);
            Save(mail);
            return("Poruka uspješno poslana.");
        }
        catch (Exception e) { return("Error: " + e); }
    }
Exemplo n.º 6
0
Arquivo: Mail.cs Projeto: Meigs2/BUZZ
        /// <summary>
        /// Create and send a new mail.
        /// <para>POST /characters/{character_id}/mail/</para>
        /// </summary>
        /// <param name="auth">The <see cref="AuthDTO"/> object.</param>
        /// <param name="mail">The mail to send.</param>
        /// <returns><see cref="ESIModelDTO{T}"/> containing the id of the sent mail.</returns>
        public async Task <ESIModelDTO <int> > SendNewMailV1Async(AuthDTO auth, NewMail mail)
        {
            CheckAuth(auth, Scopes.ESI_MAIL_SEND_MAIL_1);

            var responseModel = await PostAsync($"/v1/characters/{auth.CharacterId}/mail/", auth, mail);

            CheckResponse(nameof(SendNewMailV1Async), responseModel.Error, responseModel.Message, responseModel.LegacyWarning, logger);

            return(ReturnModelDTO <int>(responseModel));
        }
Exemplo n.º 7
0
        private void TimerCounter()
        {
            Thread.Sleep(Time);
            MailInfo mailInfo = new MailInfo()
            {
                From    = "Timer",
                To      = "User",
                Message = "Timer finished work"
            };

            NewMail?.Invoke(this, mailInfo);
            timerThread.Abort();
        }
Exemplo n.º 8
0
    public string Init()
    {
        NewMail x = new NewMail();

        x.id          = null;
        x.email       = null;
        x.groupEmails = new List <GroupEmails>();
        x.subject     = "";
        x.message     = "";
        x.date        = DateTime.Today;
        string json = JsonConvert.SerializeObject(x, Formatting.Indented);

        return(json);
    }
Exemplo n.º 9
0
 public void Save(NewMail mail)
 {
     try {
         string groupEmails = JsonConvert.SerializeObject(mail.groupEmails, Formatting.Indented);
         connection.Open();
         string     sql     = @"INSERT INTO MailMessages VALUES  
                    (@Email, @GroupEmails, @Subject, @Message, @Date)";
         SqlCommand command = new SqlCommand(sql, connection);
         command.Parameters.Add(new SqlParameter("Email", mail.email));
         command.Parameters.Add(new SqlParameter("GroupEmails", groupEmails));
         command.Parameters.Add(new SqlParameter("Subject", mail.subject));
         command.Parameters.Add(new SqlParameter("Message", mail.message));
         command.Parameters.Add(new SqlParameter("Date", mail.date));
         command.ExecuteNonQuery();
         connection.Close();
     } catch (Exception e) { }
 }
Exemplo n.º 10
0
        //Step 3:
        //Method Responsible for Raising Event (Notify registered objects that event occurred)
        //by convention protected virtual void (so inheritance is possible, if class is sealed - private, nonvirtual)
        protected virtual void OnNewMail(MailEventArgs e)
        {
            //variant 1
            //not thread safe
            //if (NewMail != null) NewMail(this, e);

            //variant 2
            //Thread safe in theory but compiler could optimize it
            //EventHandler<MailEventArgs> TempNewMail = NewMail;
            //if (TempNewMail != null) TempNewMail(this, e);

            //variant 3
            //according to Rihter
            //EventHandler<MailEventArgs> TempNewMail = Volatile.Read(ref NewMail);
            //if (TempNewMail != null) TempNewMail(this, e);

            //variant 4
            //according to C#6.0 - it is thread safe
            NewMail?.Invoke(this, e);
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Create(string topic, string text, List <string> ccRecipientsAddresses, List <string> bccRecipientsAddresses, List <IFormFile> files)
        {
            int           userID        = int.Parse(User.Claims.First(x => x.Type == ClaimTypes.NameIdentifier).Value);
            ErrorResponse errorResponse = new ErrorResponse();

            NewMail newMail = new NewMail
            {
                Topic = topic,
                Text  = text,
                CCRecipientsAddresses  = ccRecipientsAddresses,
                BCCRecipientsAddresses = bccRecipientsAddresses,
                Files = files
            };
            var newMailValidator = new NewMailValidator().Validate(newMail);

            if (!newMailValidator.IsValid)
            {
                foreach (var error in newMailValidator.Errors)
                {
                    errorResponse.Errors.Add(new ErrorModel {
                        FieldName = error.PropertyName, Message = error.ErrorMessage
                    });
                }
                Response.StatusCode = 400;
                return(new JsonResult(errorResponse));
            }

            try
            {
                await _mailService.AddMail(userID, newMail);
            }
            catch (Exception ex)
            {
                errorResponse.Errors.Add(new ErrorModel {
                    FieldName = ex.Message, Message = ex.InnerException.Message
                });
                Response.StatusCode = 400;
            }

            return(new JsonResult(errorResponse));
        }
Exemplo n.º 12
0
 public void ConstructorTest()
 {
     #region Init variables
     List <string> CCRecipientsAddresses  = new List <string>();
     List <string> BCCRecipientsAddresses = new List <string>();
     string        topic = "testtocpic";
     string        text  = "testtext";
     #endregion
     NewMail newMail = new NewMail
     {
         BCCRecipientsAddresses = BCCRecipientsAddresses,
         CCRecipientsAddresses  = CCRecipientsAddresses,
         Text  = text,
         Topic = topic
     };
     #region Tests
     Assert.NotNull(newMail);
     Assert.Equal(newMail.CCRecipientsAddresses, CCRecipientsAddresses);
     Assert.Equal(newMail.BCCRecipientsAddresses, BCCRecipientsAddresses);
     Assert.Equal(newMail.Topic, topic);
     Assert.Equal(newMail.Text, text);
     #endregion
 }
Exemplo n.º 13
0
 public string GetMessage(NewMail mail)
 {
     try {
         connection.Open();
         SqlCommand command = new SqlCommand(@"SELECT * FROM MailMessages WHERE [Id] = @Id", connection);
         command.Parameters.Add(new SqlParameter("Id", mail.id));
         NewMail       x      = new NewMail();
         SqlDataReader reader = command.ExecuteReader();
         while (reader.Read())
         {
             x.id          = reader.GetInt32(0);
             x.email       = reader.GetValue(1) == DBNull.Value ? "" : reader.GetString(1);
             x.groupEmails = JsonConvert.DeserializeObject <List <GroupEmails> >(reader.GetString(2));
             x.subject     = reader.GetValue(3) == DBNull.Value ? "" : reader.GetString(3);
             x.message     = reader.GetValue(4) == DBNull.Value ? "" : reader.GetString(4);
             x.date        = reader.GetValue(5) == DBNull.Value ? DateTime.Today : reader.GetDateTime(5);
         }
         connection.Close();
         string json = JsonConvert.SerializeObject(x, Formatting.Indented);
         return(json);
     }
     catch (Exception e) { return("Error: " + e); }
 }
Exemplo n.º 14
0
        public void WhenNewMailHaveAtLeastOneBCCRecipients_ShouldNotHaveAnyError()
        {
            var validator = new NewMailValidator();

            #region Init variables
            List <string> CCRecipientsAddresses  = new List <string>();
            List <string> BCCRecipientsAddresses = new List <string> {
                "*****@*****.**"
            };
            string topic = "testtopic";
            string text  = "testtext";
            #endregion
            NewMail newMail = new NewMail
            {
                CCRecipientsAddresses  = CCRecipientsAddresses,
                BCCRecipientsAddresses = BCCRecipientsAddresses,
                Topic = topic,
                Text  = text,
            };
            var result = validator.TestValidate(newMail);
            #region Tests
            result.ShouldNotHaveAnyValidationErrors();
            #endregion
        }
Exemplo n.º 15
0
 protected virtual void OnNewMail(NewMailEventArgs e)
 {
     NewMail?.Invoke(this, e);
 }
Exemplo n.º 16
0
 public virtual void OnNewMail(NewMailEvent e)
 {
     NewMail?.Invoke(this, e);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Notifies all subscrbers.
 /// </summary>
 protected virtual void OnNewMail(DataEventArgs info) =>
 NewMail?.Invoke(this, info);
Exemplo n.º 18
0
 /// <summary>
 /// Notifies all observers.
 /// </summary>
 protected virtual void OnNewMail(WeatherInfoEventArgs info)
 {
     NewMail?.Invoke(this, info);
 }
Exemplo n.º 19
0
 protected virtual void OnNewMail(MailMessage mail)
 {
     NewMail?.Invoke(mail);
 }