SenseNet.ContentRepository.SurveyItem.SendNotification C# (CSharp) Метод

SendNotification() приватный Метод

private SendNotification ( ) : void
Результат void
        private void SendNotification()
        {
            var parent = Content.Create(this.Parent);
            bool isNotificationEnabled;


            if (bool.TryParse(parent.Fields["EnableNotificationMail"].GetData().ToString(), out isNotificationEnabled) && isNotificationEnabled)
            {
                var mailTemplate = this.Parent.GetReference<Node>("MailTemplatePage");
                var senderAddress = parent.Fields["SenderAddress"].GetData().ToString();
                
                if (mailTemplate != null && !string.IsNullOrEmpty(senderAddress))
                {
                    var evaluators = parent.Fields["Evaluators"].GetData() as List<Node>;
                    var emailList = new Dictionary<string, string>();

                    if (evaluators != null)
                    {
                        foreach (var evaluator in evaluators)
                        {
                            var user = evaluator as IUser;

                            if (user != null && !string.IsNullOrEmpty(user.Email) && !emailList.ContainsKey(user.Email))
                            {
                                emailList.Add(user.Email, user.FullName);
                            }
                            else
                            {
                                var group = evaluator as Group;

                                if (group != null)
                                {
                                    foreach (var usr in group.GetAllMemberUsers())
                                    {
                                        if (!string.IsNullOrEmpty(usr.Email) && !emailList.ContainsKey(usr.Email))
                                        {
                                            emailList.Add(usr.Email, usr.FullName);
                                        }
                                    }
                                }
                            }
                        }

                        var mailTemplateCnt = Content.Create(mailTemplate);

                        var mailSubject = new StringBuilder(mailTemplateCnt.Fields["Subtitle"].GetData().ToString());

                        var mailBody = new StringBuilder(mailTemplateCnt.Fields["Body"].GetData().ToString());
                        var linkText = "<a href='{0}?action={1}'>{1}</a>";
                        var url = HttpContext.Current.Request.UrlReferrer.AbsoluteUri.Substring(0,
                                                                                                HttpContext.Current.
                                                                                                    Request.
                                                                                                    UrlReferrer.
                                                                                                    AbsoluteUri.
                                                                                                    IndexOf("?"))
                                    + "/" + this.Name;

                        mailBody = mailBody.Replace("{User}", (this.CreatedBy as IUser).FullName);
                        mailBody = mailBody.Replace("{SurveyName}", parent.DisplayName);
                        mailBody = mailBody.Replace("{Browse}", string.Format(linkText, url, "Browse"));
                        mailBody = mailBody.Replace("{Evaluate}", string.Format(linkText, url, "Evaluate"));
                        mailBody = mailBody.Replace("{Creator}", (this.Parent.CreatedBy as IUser).FullName);

                        var smtpClient = new SmtpClient(System.Web.Configuration.WebConfigurationManager.AppSettings["SMTP"]);
                        var smtpUser = System.Web.Configuration.WebConfigurationManager.AppSettings["SMTPUser"];
                        var smtpPassword = System.Web.Configuration.WebConfigurationManager.AppSettings["SMTPPassword"];

                        if (!string.IsNullOrEmpty(smtpUser) && !string.IsNullOrEmpty(smtpPassword))
                        {
                            smtpClient.UseDefaultCredentials = false;

                            var smtpDomain = System.Web.Configuration.WebConfigurationManager.AppSettings["SMTPDomain"];
                            smtpClient.Credentials = string.IsNullOrEmpty(smtpDomain) ? new NetworkCredential(smtpUser, smtpPassword) : new NetworkCredential(smtpUser, smtpPassword, smtpDomain);
                        }

                        foreach (var email in emailList)
                        {
                            mailBody = mailBody.Replace("{Addressee}", email.Value);
                            var mailMessage = new MailMessage(senderAddress, email.Key)
                                                  {
                                                      Subject = mailSubject.ToString(),
                                                      IsBodyHtml = true,
                                                      Body = mailBody.ToString()
                                                  };

                            try
                            {
                                smtpClient.Send(mailMessage);
                            }
                            catch (Exception ex) //logged
                            {
                                Logger.WriteException(ex);
                            }
                        }
                    }
                }
                else
                {
                    Logger.WriteError("Notification e-mail cannot be sent because the template content or the sender address is missing");
                }
            }
        }
    }