public void Null_Email_Subject_Throws_Exception()
 {
     List<string> to = new List<string>();
     to.Add("to");
     List<string> cc = new List<string>();
     EmailArguments emailArguments = new EmailArguments(null, "message", to, "from", null, false, cc);
 }
 public void Email_Arguments_Can_Have_Null_SmtpServer()
 {
     List<string> to = new List<string>();
     to.Add("to");
     List<string> cc = new List<string>();
     EmailArguments emailArguments = new EmailArguments("subject", "message", to, "From", null, false, cc);
     Assert.IsNotNull(emailArguments);
 }
        public EmailSendingResult SendEmail(EmailArguments emailArguments)
        {
            EmailSendingResult sendResult = new EmailSendingResult();
            sendResult.EmailSendingFailureMessage = string.Empty;
            SmtpClient client = null;
            try
            {
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress(emailArguments.From);
                if (emailArguments.To.Count > 0)
                {
                    foreach (string toAddress in emailArguments.To)
                    {
                        if (toAddress.Contains(";"))
                        {

                            foreach (string sTo in toAddress.Split(';'))
                            {
                                mailMessage.To.Add(new MailAddress(sTo));
                            }
                        }
                        else
                        {
                            mailMessage.To.Add(new MailAddress(toAddress));
                        }

                    }
                }
                mailMessage.Subject = emailArguments.Subject;
                mailMessage.Body = emailArguments.Message;
                mailMessage.IsBodyHtml = emailArguments.IsBodyHtml;
                if (emailArguments.CC.Count > 0)
                {
                    foreach (string ccAddress in emailArguments.CC)
                    {
                        if (ccAddress.Contains(";"))
                        {

                            foreach (string sCC in ccAddress.Split(';'))
                            {
                                mailMessage.CC.Add(new MailAddress(sCC));
                            }
                        }
                        else
                        {
                            mailMessage.CC.Add(new MailAddress(ccAddress));
                        }

                    }
                }
                client = new SmtpClient(GetSmtpServerFromConfigIfItIsNotInEmailArguments(emailArguments));

                if (emailArguments.EmbeddedResources != null && emailArguments.EmbeddedResources.Count > 0)
                {
                    AlternateView alternateViewHtml = AlternateView.CreateAlternateViewFromString(emailArguments.Message, Encoding.UTF8, MediaTypeNames.Text.Html);
                    foreach (EmbeddedEmailResource resource in emailArguments.EmbeddedResources)
                    {
                        LinkedResource linkedResource = new LinkedResource(resource.ResourceStream, resource.ResourceType.ToSystemNetResourceType());
                        linkedResource.ContentId = resource.EmbeddedResourceContentId;
                        alternateViewHtml.LinkedResources.Add(linkedResource);
                    }
                    mailMessage.AlternateViews.Add(alternateViewHtml);
                }

                if (mailMessage.IsBodyHtml)
                {
                    AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mailMessage.Body, new ContentType("text/html"));

                    mailMessage.AlternateViews.Add(htmlView);

                }

                _loggingService.LogInfo(this, "Starting sending email to : " + emailArguments.To);
                client.Send(mailMessage);
                sendResult.EmailSentSuccessfully = true;
                _loggingService.LogInfo(this, "Successfully sent email: " + DisplayEmailInfo(emailArguments));
            }
            catch (Exception ex)
            {
                sendResult.EmailSentSuccessfully = false;
                sendResult.EmailSendingFailureMessage = ex.Message;
                _loggingService.LogError(this, "Exception in sending email: " + DisplayEmailInfo(emailArguments), ex);
            }

            return sendResult;
        }
 private string GetSmtpServerFromConfigIfItIsNotInEmailArguments(EmailArguments emailArguments)
 {
     string smtpServer = string.Empty;
     if (!String.IsNullOrEmpty(emailArguments.SmtpServer))
     {
         smtpServer = emailArguments.SmtpServer;
     }
     else
     {
         smtpServer = _configurationRepository.GetConfigurationValue<string>("SmtpServer");
     }
     return smtpServer;
 }
        private string DisplayEmailInfo(EmailArguments emailArguments)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("From: ");
            stringBuilder.AppendLine(emailArguments.From);
            stringBuilder.Append("To: ");
            stringBuilder.AppendLine(string.Join(";", emailArguments.To));
            if (emailArguments.CC.Count > 0)
            {
                stringBuilder.Append("CC: ");
                stringBuilder.AppendLine(string.Join(";", emailArguments.CC));
            }
            stringBuilder.Append("Submect: ");
            stringBuilder.AppendLine(emailArguments.Subject);
            stringBuilder.Append("SmtpServer: ");
            stringBuilder.AppendLine(emailArguments.SmtpServer);
            stringBuilder.Append("Body: ");
            stringBuilder.AppendLine(emailArguments.Message);

            return stringBuilder.ToString();
        }
        public async Task<bool> UpdateRewardStatusAsync(int id, string statusCode)
        {
            int previousStatusId = 0;
            int statusId = 0;
            try
            {
                Reward reward = _context.Rewards.Where(x => x.RewardID == id).SingleOrDefault();
                if (reward != null)
                {
                    previousStatusId = reward.RewardStatusID;

                    RewardStatus rewardStatus = _context.RewardStatus.Where(s => s.Code.Equals(statusCode)).SingleOrDefault();

                    if (rewardStatus != null)
                    {
                        statusId = rewardStatus.RewardStatusID;
                        if (previousStatusId != statusId)
                        {
                            if (rewardStatus.Code.Equals("R"))  //Redemption
                            { 
                                reward.RedeemedBy = _contextService.GetUserName().Split('\\')[1];
                                reward.RedeemedDate = DateTime.Now;
                            }
                            reward.RewardStatusID = statusId;

                            reward.ChangedBy = _contextService.GetUserName().Split('\\')[1];
                            reward.LastChangedDate = DateTime.Now;

                            _context.Entry(reward).State = EntityState.Modified;
                            await _context.SaveChangesAsync();
                            _loggingService.LogInfo(this, "Upate status to " + statusId + " for reward id " + reward.RewardID);

                            string subject = "2020 Vision Request - Decision";
                            string message = string.Empty;
                            string fromUserAddress = string.Empty;
                            List<string> toUserAddress = new List<string>();
                            List<string> ccUserAddress = new List<string>();
                            string leaderName = string.Empty;
                            string siteLink = string.Empty;
                            string recipientUser = string.Empty;
                            string fromUserId = reward.Supervisor;
                            string toUserId = reward.CreatedBy;

                            string url = _contextService.GetContextProperties().BaseSiteUrl;
                            siteLink = "<a href='" + url + "/#/myRewards" + "'>here</a>";
                            ccUserAddress.Add(_configurationRepository.GetConfigurationValue<string>("TestEmailCopy"));

                            RewardUserModel fromUser = _rewardUserService.GetUserDetails(fromUserId);
                            if (fromUser != null)
                            {
                                fromUserAddress = fromUser.EmailAddress;
                                leaderName = fromUser.UserFullName;
                            }
                            RewardUserModel toUser = _rewardUserService.GetUserDetails(toUserId);
                            if (toUser != null)
                            {
                                toUserAddress.Add(toUser.EmailAddress);
                            }
                            RewardUserModel recipient = _rewardUserService.GetUserDetails(reward.Recipient);
                            if (recipient != null)
                            {
                                recipientUser = recipient.UserFullName;
                            }
                            if (rewardStatus.Code.Equals("A"))  //need to send email upon approval
                            {
                                message = "Congratulations! Your request to award " + recipientUser + " has been approved by your leader " + leaderName + ". Click " + siteLink + " to retrieve your voucher and follow next steps.";
                            }
                            if (rewardStatus.Code.Equals("D"))  //need to send email upon denial
                            {
                                message = "Your request to award " + recipientUser + " has been  denied by your leader " + leaderName + ". Please get in touch with your leader for more information.";
                            }

                            EmailArguments arguments = new EmailArguments(subject, message, toUserAddress, fromUserAddress, string.Empty, true, ccUserAddress);
                            EmailSendingResult result = _emailService.SendEmail(arguments);
                        }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                _loggingService.LogError(this, "Exception in updating reward status", ex);
                return false;
            }
        }
        private async Task<bool> EmailSupervisorForApprovalAsync(List<Reward> rewards)
        {
            string message = string.Empty;
            string fromUserAddress = string.Empty;
            List<string> toUserAddress = null;
            List<string> ccUserAddress = new List<string>();
            string senderName = string.Empty;
            string siteLink = string.Empty;
            string toUserId = string.Empty;
            string fromUserId = string.Empty;
            string url = _contextService.GetContextProperties().BaseSiteUrl;
            siteLink = "<a href='" + url + "/#/approval" + "'>here</a>";
            ccUserAddress.Add(_configurationRepository.GetConfigurationValue<string>("TestEmailCopy"));

            try
            {
                foreach (Reward reward in rewards)
                {
                    string subject = "Reward Approval Request from ";
                    toUserAddress = new List<string>();
                    RewardType rewardType = _context.RewardTypes.Where(t => t.RewardTypeID == reward.RewardTypeID).SingleOrDefault();
                    if (rewardType != null && reward.RewardStatusID == 1)
                    {
                        toUserId = reward.Supervisor;
                        fromUserId = reward.CreatedBy;
                        RewardUserDataModel fromUser = _rewardUserService.GetUserDetails(fromUserId);
                        if (fromUser != null)
                        {
                            fromUserAddress = fromUser.EmailAddress;
                            senderName = fromUser.UserFullName;
                            subject = subject + senderName;
                        }
                        RewardUserDataModel toUser = _rewardUserService.GetUserDetails(toUserId);
                        if (toUser != null)
                        {
                            toUserAddress.Add(toUser.EmailAddress);
                        }

                        message = "You have received an approval request from " + senderName + ". Please click " + siteLink + " to retrieve the request, and then select approve or deny to complete the request.";

                        EmailArguments arguments = new EmailArguments(subject, message, toUserAddress, fromUserAddress, string.Empty, true, ccUserAddress);
                        EmailSendingResult result = _emailService.SendEmail(arguments);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                _loggingService.LogError(this, "Exception in sending email to supervisor for Approval", ex);
                return false;
            }
        }
 public void SystemNetEmailService_Will_Get_SmtpServer_From_Config_If_Client_Not_Provide_Through_EmailArguments()
 {
     List<string> to = new List<string>();
     to.Add("to");
     List<string> cc = new List<string>();
     EmailArguments emailArguments = new EmailArguments("subject", "message", to, "From", null, false, cc);
     SystemNetEmailService emailService = (SystemNetEmailService)_emailService;
     PrivateObject obj = new PrivateObject(emailService);
     var retVal = obj.Invoke("GetSmtpServerFromConfigIfItIsNotInEmailArguments", emailArguments);
     Assert.AreEqual("mySmtpServer", retVal);
 }
 public void Null_Email_To_Throws_Exception()
 {
     List<string> to = new List<string>();
     List<string> cc = new List<string>();
     EmailArguments emailArguments = new EmailArguments("subject", "message", to, "from", null, false, cc);
 }