예제 #1
0
        private static string GetRecipientOverrideMessage(EmailInfo emailInformation)
        {
            var originalTo  = emailInformation.To.ToList();
            var originalCC  = emailInformation.CarbonCopy.ToList();
            var originalBCC = emailInformation.BlindCarbonCopy.ToList();

            var builder = new StringBuilder();

            builder.Append("<p style=\"color:red; font-weight: bold; font-size: 20px;\">This email was send while overriding the recipients in order to not accidentally spam people who should not receive emails from a development environment.</p>");
            builder.Append("<table>");
            builder.Append("<tr>");
            builder.Append("<th>Original To:</th>");
            builder.Append("<th>Original CC:</th>");
            builder.Append("<th>Original BCC:</th>");
            builder.Append("</tr>");
            builder.Append("<tr>");

            AppendEmailList(builder, originalTo);
            AppendEmailList(builder, originalCC);
            AppendEmailList(builder, originalBCC);

            builder.Append("</tr>");
            builder.Append("</table>");
            builder.Append("<hr/>");
            builder.Append("<p style=\"text-align: center;\">Start of original message contents</p>");
            builder.Append("<hr/>");

            return(builder.ToString());
        }
예제 #2
0
        /// <summary>
        /// Send an email depending on the specified email information.
        /// </summary>
        /// <param name="web">The SharePoint Web.</param>
        /// <param name="emailInformation">The email information.</param>
        public void SendEmail(SPWeb web, EmailInfo emailInformation)
        {
            if (this.IsRecipientOverrideEnabled(web.Site.WebApplication))
            {
                var RecipientOverrideEmail = this.propertyBagHelper.GetWebApplicationValue(web.Site.WebApplication, RecipientOverridePropertyBagKey);
                emailInformation.Body = GetRecipientOverrideMessage(emailInformation) + emailInformation.Body;
                emailInformation.To.Clear();
                emailInformation.To.Add(RecipientOverrideEmail);
                emailInformation.CarbonCopy.Clear();
                emailInformation.BlindCarbonCopy.Clear();

                this.logger.Warn("An email with the subject line '{0}' is being sent with the recipient override email address '{1}'.", emailInformation.Subject, RecipientOverrideEmail);
            }

            var headers = EmailHelper.GetEmailHeaders(emailInformation);

            web.RunAsSystem(elevatedWeb =>
            {
                var logMsg = string.Format(
                    CultureInfo.InvariantCulture,
                    "Sending email using the web '{0}' with the following headers... to: '{1}', cc: '{2}', bcc: '{3}', subject: '{4}'",
                    elevatedWeb.Url,
                    headers["to"],
                    headers["cc"],
                    headers["bcc"],
                    headers["subject"]);

                this.logger.Info(logMsg);

                // Send the email
                SPUtility.SendEmail(elevatedWeb, headers, emailInformation.Body);

                this.logger.Info("Email Sent.");
            });
        }
예제 #3
0
        /// <summary>
        /// Adds the group members (including AD group members) to the 'To' property of the email information.
        /// Only users who have emails are added.
        /// </summary>
        /// <param name="group">The SharePoint group.</param>
        /// <param name="emailInformation">The email information.</param>
        public void AddGroupMembersToRecipients(SPGroup group, EmailInfo emailInformation)
        {
            var users      = this.userHelper.GetUsersInPrincipal(group);
            var userEmails = users.Where(u => !string.IsNullOrEmpty(u.Email)).Select(u => u.Email).ToList();

            userEmails.ForEach(ue => emailInformation.To.Add(ue));
        }
예제 #4
0
        /// <summary>
        /// Send an email depending on the specified email information.
        /// </summary>
        /// <param name="web">The SharePoint Web.</param>
        /// <param name="emailInformation">The email information.</param>
        public void SendEmail(SPWeb web, EmailInfo emailInformation)
        {
            if (this.IsRecipientOverrideEnabled(web.Site.WebApplication))
            {
                var RecipientOverrideEmail = this.propertyBagHelper.GetWebApplicationValue(web.Site.WebApplication, RecipientOverridePropertyBagKey);
                emailInformation.Body = GetRecipientOverrideMessage(emailInformation) + emailInformation.Body;
                emailInformation.To.Clear();
                emailInformation.To.Add(RecipientOverrideEmail);
                emailInformation.CarbonCopy.Clear();
                emailInformation.BlindCarbonCopy.Clear();

                this.logger.Warn("An email with the subject line '{0}' is being sent with the recipient override email address '{1}'.", emailInformation.Subject, RecipientOverrideEmail);
            }

            var headers = EmailHelper.GetEmailHeaders(emailInformation);
            web.RunAsSystem(elevatedWeb =>
            {
                var logMsg = string.Format(
                    CultureInfo.InvariantCulture,
                    "Sending email using the web '{0}' with the following headers... to: '{1}', cc: '{2}', bcc: '{3}', subject: '{4}'",
                    elevatedWeb.Url,
                    headers["to"],
                    headers["cc"],
                    headers["bcc"],
                    headers["subject"]);

                this.logger.Info(logMsg);

                // Send the email
                SPUtility.SendEmail(elevatedWeb, headers, emailInformation.Body);

                this.logger.Info("Email Sent.");
            });
        }
예제 #5
0
        public void SendEmail_WhenRecipientOverrideEnabled_ShouldSendEmailOnlyToRecipientOverrideAddressAndModifyEmailBody()
        {
            using (var testScope = SiteTestScope.BlankSite())
            {
                // Arrange
                var web = testScope.SiteCollection.RootWeb;
                var webApplication = testScope.SiteCollection.WebApplication;

                var RecipientOverrideEmail = "*****@*****.**";
                var emailInformation = new EmailInfo();
                emailInformation.To.Add("*****@*****.**");
                emailInformation.To.Add("*****@*****.**");
                emailInformation.CarbonCopy.Add("*****@*****.**");
                emailInformation.BlindCarbonCopy.Add("*****@*****.**");
                emailInformation.Subject = "Quoi faire à Barcelone?";
                emailInformation.Body = "J'ai un ami qui me propose un hike! :)";

                var originalBody = emailInformation.Body;

                // Actual values
                StringDictionary actualHeaders = null;
                string actualBody = null;
                bool? actualIsRecipientOverrideEnabled = null;

                using (ShimsContext.Create())
                {
                    // Mock the SendEmail method so no emails are actualy sent.
                    ShimSPUtility.SendEmailSPWebStringDictionaryString = (pWeb, pHeaders, pBody) =>
                    {
                        actualHeaders = pHeaders;
                        actualBody = pBody;
                        return true;
                    };

                    using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope())
                    {
                        var emailHelper = injectionScope.Resolve<IEmailHelper>();

                        // Act
                        emailHelper.EnableRecipientOverride(webApplication, RecipientOverrideEmail);
                        actualIsRecipientOverrideEnabled = emailHelper.IsRecipientOverrideEnabled(webApplication);
                        emailHelper.SendEmail(web, emailInformation);

                        // Assert
                        Assert.IsTrue(actualIsRecipientOverrideEnabled.HasValue && actualIsRecipientOverrideEnabled.Value, "Recipient override should have been enabled.");
                        Assert.IsTrue(actualHeaders["to"] == RecipientOverrideEmail, "The email should have been sent only to the recipient override email address.");
                        Assert.IsTrue(!actualHeaders.ContainsKey("cc"), "No carbon copy should have been in the email headers.");
                        Assert.IsTrue(!actualHeaders.ContainsKey("bcc"), "No blind carbon copy should have been in the email headers.");
                        Assert.IsTrue(actualBody.Length > originalBody.Length, "Text should have been added to the body of the email.");
                    }
                }
            }
        }
예제 #6
0
        private static StringDictionary GetEmailHeaders(EmailInfo emailInformation)
        {
            // Make sure the email will be sent to someone
            if (!emailInformation.To.Any() && !emailInformation.CarbonCopy.Any() && !emailInformation.BlindCarbonCopy.Any())
            {
                throw new InvalidOperationException("When sending an email make sure to include one of the following pieces of information: To, Carbon Copy, or BlindCarbonCopy.");
            }

            var headers = emailInformation.OtherHeaders;

            if (emailInformation.To.Any())
            {
                headers.Add("to", string.Join(",", emailInformation.To));
            }

            if (emailInformation.CarbonCopy.Any())
            {
                headers.Add("cc", string.Join(",", emailInformation.CarbonCopy));
            }

            if (emailInformation.BlindCarbonCopy.Any())
            {
                headers.Add("bcc", string.Join(",", emailInformation.BlindCarbonCopy));
            }

            if (!string.IsNullOrEmpty(emailInformation.From))
            {
                headers.Add("from", emailInformation.From);
            }

            if (!string.IsNullOrEmpty(emailInformation.Subject))
            {
                headers.Add("subject", emailInformation.Subject);
            }

            switch (emailInformation.Priority)
            {
            case EmailPriorityType.High:
                headers.Add("Importance", "high");
                headers.Add("X-Priority", "1");
                break;

            case EmailPriorityType.Low:
                headers.Add("Importance", "low");
                headers.Add("X-Priority", "5");
                break;
            }

            return(headers);
        }
        private void SendEmail(SPWeb web, string emailTo, string emailTitle, string body)
        {
            var emailInfo = new EmailInfo();
            emailInfo.To.Add(emailTo);
            emailInfo.Subject = emailTitle;
            emailInfo.Body = body;
            emailInfo.Priority = EmailPriorityType.High;

            this.emailHelper.SendEmail(web, emailInfo);
        }
예제 #8
0
        public void SendEmail_WhenRecipientOverrideDisabled_ShouldSendEmailWithoutManipulatingTheReceiversOrChangingTheEmailContent()
        {
            using (var testScope = SiteTestScope.BlankSite())
            {
                // Arrange
                var web = testScope.SiteCollection.RootWeb;
                var webApplication = testScope.SiteCollection.WebApplication;

                var emailInformation = new EmailInfo();
                emailInformation.To.Add("*****@*****.**");
                emailInformation.To.Add("*****@*****.**");
                emailInformation.CarbonCopy.Add("*****@*****.**");
                emailInformation.BlindCarbonCopy.Add("*****@*****.**");
                emailInformation.Subject = "Quoi faire à Barcelone?";
                emailInformation.Body = "J'ai un ami qui me propose un hike! :)";

                // Expected values
                string expectedBody = emailInformation.Body;
                StringDictionary expectedHeaders = new StringDictionary();
                expectedHeaders.Add("to", string.Join(",", emailInformation.To));
                expectedHeaders.Add("cc", string.Join(",", emailInformation.CarbonCopy));
                expectedHeaders.Add("bcc", string.Join(",", emailInformation.BlindCarbonCopy));
                expectedHeaders.Add("subject", emailInformation.Subject);

                // Actual values
                StringDictionary actualHeaders = null;
                string actualBody = null;
                bool? actualIsRecipientOverrideEnabled = null;

                using (ShimsContext.Create())
                {
                    // Mock the SendEmail method so no emails are actualy sent.
                    ShimSPUtility.SendEmailSPWebStringDictionaryString = (pWeb, pHeaders, pBody) =>
                    {
                        actualHeaders = pHeaders;
                        actualBody = pBody;
                        return true;
                    };

                    using (var injectionScope = IntegrationTestServiceLocator.BeginLifetimeScope())
                    {
                        var emailHelper = injectionScope.Resolve<IEmailHelper>();

                        // Act
                        actualIsRecipientOverrideEnabled = emailHelper.IsRecipientOverrideEnabled(webApplication);
                        emailHelper.SendEmail(web, emailInformation);

                        // Assert
                        Assert.IsTrue(actualIsRecipientOverrideEnabled.HasValue && !actualIsRecipientOverrideEnabled.Value, "Recipient override should not have been enabled.");
                        Assert.IsTrue(actualHeaders.Count == expectedHeaders.Count, "The headers should not have changed.");
                        Assert.IsTrue(actualBody == expectedBody, "The email body should not have changed.");

                        foreach (string key in actualHeaders.Keys)
                        {
                            Assert.IsTrue(actualHeaders[key] == expectedHeaders[key], "Header with key '{0}' should not have changed.", key);
                        }
                    }
                }
            }
        }
예제 #9
0
        private static string GetRecipientOverrideMessage(EmailInfo emailInformation)
        {
            var originalTo = emailInformation.To.ToList();
            var originalCC = emailInformation.CarbonCopy.ToList();
            var originalBCC = emailInformation.BlindCarbonCopy.ToList();

            var builder = new StringBuilder();
            builder.Append("<p style=\"color:red; font-weight: bold; font-size: 20px;\">This email was send while overriding the recipients in order to not accidentally spam people who should not receive emails from a development environment.</p>");
            builder.Append("<table>");
            builder.Append("<tr>");
            builder.Append("<th>Original To:</th>");
            builder.Append("<th>Original CC:</th>");
            builder.Append("<th>Original BCC:</th>");
            builder.Append("</tr>");
            builder.Append("<tr>");

            AppendEmailList(builder, originalTo);
            AppendEmailList(builder, originalCC);
            AppendEmailList(builder, originalBCC);

            builder.Append("</tr>");
            builder.Append("</table>");
            builder.Append("<hr/>");
            builder.Append("<p style=\"text-align: center;\">Start of original message contents</p>");
            builder.Append("<hr/>");

            return builder.ToString();
        }
예제 #10
0
        private static StringDictionary GetEmailHeaders(EmailInfo emailInformation)
        {
            // Make sure the email will be sent to someone
            if (!emailInformation.To.Any() && !emailInformation.CarbonCopy.Any() && !emailInformation.BlindCarbonCopy.Any())
            {
                throw new InvalidOperationException("When sending an email make sure to include one of the following pieces of information: To, Carbon Copy, or BlindCarbonCopy.");
            }

            var headers = emailInformation.OtherHeaders;

            if (emailInformation.To.Any())
            {
                headers.Add("to", string.Join(",", emailInformation.To));
            }

            if (emailInformation.CarbonCopy.Any())
            {
                headers.Add("cc", string.Join(",", emailInformation.CarbonCopy));
            }

            if (emailInformation.BlindCarbonCopy.Any())
            {
                headers.Add("bcc", string.Join(",", emailInformation.BlindCarbonCopy));
            }

            if (!string.IsNullOrEmpty(emailInformation.From))
            {
                headers.Add("from", emailInformation.From);
            }

            if (!string.IsNullOrEmpty(emailInformation.Subject))
            {
                headers.Add("subject", emailInformation.Subject);
            }

            switch (emailInformation.Priority)
            {
                case EmailPriorityType.High:
                    headers.Add("Importance", "high");
                    headers.Add("X-Priority", "1");
                    break;

                case EmailPriorityType.Low:
                    headers.Add("Importance", "low");
                    headers.Add("X-Priority", "5");
                    break;
            }

            return headers;
        }
예제 #11
0
 /// <summary>
 /// Adds the group members (including AD group members) to the 'To' property of the email information.
 /// Only users who have emails are added.
 /// </summary>
 /// <param name="group">The SharePoint group.</param>
 /// <param name="emailInformation">The email information.</param>
 public void AddGroupMembersToRecipients(SPGroup group, EmailInfo emailInformation)
 {
     var users = this.userHelper.GetUsersInPrincipal(group);
     var userEmails = users.Where(u => !string.IsNullOrEmpty(u.Email)).Select(u => u.Email).ToList();
     userEmails.ForEach(ue => emailInformation.To.Add(ue));
 }