Exemple #1
0
        private void QueueEmail(TeamFoundationRequestContext requestContext, string owner, XmlDocument doc)
        {
            var mailService = requestContext.To(TeamFoundationHostType.Deployment).GetService <TeamFoundationMailService>();


            try
            {
                var transform = new XslCompiledTransform();
                transform.Load(emailTemplate);

                using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture))
                {
                    using (XmlWriter xmlWriter = XmlTextWriter.Create(sw, transform.OutputSettings))
                    {
                        transform.Transform(doc, null, xmlWriter);
                        xmlWriter.Flush();

                        var message = new MailMessage();
                        message.Body            = sw.ToString();
                        message.BodyEncoding    = System.Text.Encoding.UTF8;
                        message.IsBodyHtml      = true;
                        message.Subject         = emailSubject;
                        message.SubjectEncoding = System.Text.Encoding.UTF8;
                        message.Priority        = emailPriority;

                        var toAddress = GetPreferredEmailAddress(requestContext, owner);
                        if (string.IsNullOrEmpty(toAddress))
                        {
                            if (warnIfNoEmailAddress)
                            {
                                var messageBuilder = new StringBuilder();
                                messageBuilder.AppendFormat("The email address for user {0} could not be found!", owner);
                                messageBuilder.AppendLine();
                                messageBuilder.AppendFormat("The following workspaces are older than {0} days:", warningAgeInDays);
                                messageBuilder.AppendLine();
                                foreach (XmlNode node in doc.SelectNodes("//Workspace"))
                                {
                                    messageBuilder.AppendFormat("  {0} on {1}, last accessed {2}", node.Attributes["name"].Value, node.Attributes["computer"].Value, node.Attributes["lastAccessed"].Value);
                                    messageBuilder.AppendLine();
                                }
                                LogWarning(messageBuilder.ToString());
                            }
                            return;
                        }

                        message.To.Add(toAddress);

                        mailService.QueueMailJob(requestContext, message);
                    }
                }
            }
            catch (Exception exception)
            {
                LogError("There was an error queuing a workspace cleanup email!", exception);
            }
        }