Exemplo n.º 1
0
        protected Error SendMessage(CompanyModel company,
                                    UserModel sender,
                                    MessageTemplateType templateId,
                                    TaskType notificationType,
                                    List <UserModel> recipients,
                                    Dictionary <string, string> dict,
                                    List <string> attachments = null)
        {
            // Send as email
            var message = new EMailMessage(sender, templateId);

            message.AddRecipients(recipients);
            message.AddProperties(dict);
            message.AddAttachments(attachments);


            EMailService.EMailService es = new EMailService.EMailService(_db, company);
            var error = es.SendEMail(message);

            //if (!error.IsError) {
            // Now send as a notification
            //    TaskManagerService.TaskManagerService ts = new TaskManagerService.TaskManagerService(_db, company);
            //    error = ts.SendTask(templateId, notificationType, recipients, dict);
            //}
            return(error);
        }
Exemplo n.º 2
0
        public void SendEMailTest()
        {
            // Get some test users and test company
            List <UserModel> users = new List <UserModel>();

            for (int i = 0; i < 10; i++)
            {
                users.Add(GetTestUser());
            }

            var testCompany = GetTestCompany(users[0]);

            EMailService.EMailService emailService = new EMailService.EMailService(db, testCompany);


            // Tests using email templates
            var sender = new UserModel {
                EMail = RandomEMail()
            };
            var message = new EMailMessage(sender, MessageTemplateType.TestMessage);

            // Single user
            var recipient = new UserModel {
                EMail = RandomEMail()
            };

            message.AddRecipient(users[0]);

            var result = emailService.SendEMail(message);

            Assert.IsTrue(!result.IsError, result.Message);

            var msgs     = getMessages(sender);
            int actual   = msgs.Count(),
                expected = 1;

            Assert.IsTrue(actual == expected, $"Error: {actual} records were found when {expected} were expected");

            var emailTemplate = LookupService.FindMessageTemplateModel(testCompany.Id, MessageTemplateType.TestMessage);

            checkEMailContent(msgs, sender, emailTemplate.Subject);

            // Multiple users
            sender = new UserModel {
                EMail = RandomEMail()
            };
            message = new EMailMessage(sender, MessageTemplateType.TestMessage);
            message.AddRecipients(users);

            result = emailService.SendEMail(message);
            Assert.IsTrue(!result.IsError, result.Message);

            msgs     = getMessages(sender);
            actual   = msgs.Count();
            expected = 1;       // One message is sent to all recipients/cc;s/bcc's
            Assert.IsTrue(actual == expected, $"Error: {actual} records were found when {expected} were expected");

            emailTemplate = LookupService.FindMessageTemplateModel(testCompany.Id, MessageTemplateType.TestMessage);
            checkEMailContent(msgs, sender, emailTemplate.Subject);


            // Tests using straight text
            string subject = RandomString(),
                   text    = LorumIpsum();

            // Single user
            sender = new UserModel {
                EMail = RandomEMail()
            };
            message = new EMailMessage(sender, subject, text);
            message.AddRecipient(users[0]);

            var result2 = emailService.SendEMail(message);

            Assert.IsTrue(!result2.IsError, result.Message);

            msgs     = getMessages(sender);
            actual   = msgs.Count();
            expected = 1;
            Assert.IsTrue(actual == expected, $"Error: {actual} records were found when {expected} were expected");
            checkEMailContent(msgs, sender, subject);

            // Multiple users
            sender = new UserModel {
                EMail = RandomEMail()
            };
            message = new EMailMessage(sender, subject, text);
            message.AddRecipients(users);

            result = emailService.SendEMail(message);
            Assert.IsTrue(!result.IsError, result.Message);

            msgs     = getMessages(sender);
            actual   = msgs.Count();
            expected = 1;       // One message is sent to all recipients/cc;s/bcc's
            Assert.IsTrue(actual == expected, $"Error: {actual} records were found when {expected} were expected");
            checkEMailContent(msgs, sender, subject);
        }