#pragma warning restore SA1306 // Field names must begin with lower-case letter

            internal Mailer(IVssRequestContext requestContext)
            {
                this.Enabled = false;
                try
                {
                    TeamFoundationRegistryService service = requestContext.GetService <TeamFoundationRegistryService>();
                    Microsoft.TeamFoundation.Framework.Server.RegistryEntryCollection registryEntryCollection = service.ReadEntriesFallThru(requestContext, this.NotificationRootPath + "/*");
                    if (registryEntryCollection["EmailEnabled"].GetValue <bool>(true))
                    {
                        this.SmtpServer  = registryEntryCollection["SmtpServer"].GetValue(string.Empty);
                        this.SmtpPort    = registryEntryCollection["SmtpPort"].GetValue <int>(-1);
                        this.EnableSsl   = registryEntryCollection["SmtpEnableSsl"].GetValue <bool>(false);
                        this.FromAddress = null;

                        string value = registryEntryCollection["EmailNotificationFromAddress"].GetValue(string.Empty);
                        if (!string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(this.SmtpServer))
                        {
                            this.FromAddress = new MailAddress(value);
                            this.Enabled     = true;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine($"SendMail failed: {ex.Message}");
                }
            }
Beispiel #2
0
        protected override void Execute(CodeActivityContext context)
        {
            TeamFoundationRequestContext requestContextCollection = context.GetValue(this.RequestContext);
            string argToAdress   = context.GetValue(this.ToAdress);
            string argSubject    = context.GetValue(this.Subject);
            bool   argIsBodyHtml = context.GetValue(this.IsBodyHtml);
            string argBody       = context.GetValue(this.Body);

            // services on the configuration
            #if UsingOrganizationServiceHost
            TeamFoundationRequestContext requestContextConfiguration = requestContextCollection.ServiceHost.OrganizationServiceHost.CreateServicingContext();
            #else
            TeamFoundationRequestContext requestContextConfiguration = requestContextCollection.ServiceHost.ApplicationServiceHost.CreateServicingContext();
            #endif
            TeamFoundationRegistryService srvRegistryConfiguration = requestContextConfiguration.GetService <TeamFoundationRegistryService>();

            // read the parameters from TFS registry
            var arrRegEntries = srvRegistryConfiguration.ReadEntries(requestContextConfiguration, "/Service/Integration/Settings/*").ToArray();
            var keyvalues     = new List <string>()
            {
                "EmailEnabled",
                "EmailNotificationFromAddress",
                // "SmtpAnonymousAuth",
                "SmtpCertThumbprint",
                "SmtpEnableSsl",
                "SmtpPassword",
                "SmtpPort",
                "SmtpServer",
                "SmtpUser"
            }.Select(name =>
            {
                string path  = "/Service/Integration/Settings/" + name;
                string value = null;
                try
                {
                    // value = srvRegistryConfiguration.ReadEntries(requestContextConfiguration, path).First().Value;
                    value = arrRegEntries.Where(regEntry => regEntry.Name == name).Select(regEntry => regEntry.Value).FirstOrDefault();
                }
                catch (Exception e)
                {
                    this.LogInfo(string.Format("registry value '{0}' not found", path));
                    this.LogInfo(e.Message);
                }
                return(new { key = name, value = value });
            });

            // dispose the request context
            requestContextConfiguration.Dispose();

            foreach (var keyvalue in keyvalues)
            {
                if (keyvalue.value == null)
                {
                    this.LogInfo(string.Format("registry value '{0}' not found", keyvalue.key));
                }
            }

            bool TfsEmailEnabled = bool.Parse(keyvalues.Where(kv => kv.key == "EmailEnabled").First().value);
            if (!TfsEmailEnabled)
            {
                this.LogInfo(string.Format("TFS Email Alter Settings are disabled"));
                return;
            }

            string TfsSmtpServer = keyvalues.Where(kv => kv.key == "SmtpServer").First().value;
            int    TfsSmtpPort   = int.Parse(keyvalues.Where(kv => kv.key == "SmtpPort").First().value);
            string TfsEmailNotificationFromAddress = keyvalues.Where(kv => kv.key == "EmailNotificationFromAddress").First().value;
            string TfsSmtpUser     = keyvalues.Where(kv => kv.key == "SmtpUser").First().value;
            string TfsSmtpPassword = keyvalues.Where(kv => kv.key == "SmtpPassword").First().value;

            this.LogInfo(string.Format("TFS SmptServer: {0}:{1}", TfsSmtpServer, TfsSmtpPort));
            this.LogInfo(string.Format("TFS SmptUser: {0} (from address: {1})", TfsSmtpUser, TfsEmailNotificationFromAddress));

            // setup connection to the SMTP host.
            SmtpClient client = new SmtpClient(TfsSmtpServer, TfsSmtpPort);
            if (string.IsNullOrWhiteSpace(TfsSmtpUser))
            {
                client.UseDefaultCredentials = true;
            }
            else
            {
                client.Credentials = new System.Net.NetworkCredential(TfsSmtpUser, TfsSmtpPassword);
            }

            // Specify the e-mail sender.
            // Create a mailing address that includes a UTF8 character
            // in the display name.
            MailAddress from = new MailAddress(TfsEmailNotificationFromAddress, TfsEmailNotificationFromAddress);
            // Set destinations for the e-mail message.
            MailAddress to = new MailAddress(argToAdress);
            // Specify the message content.
            MailMessage message = new MailMessage(from, to);
            message.Subject         = argSubject;
            message.SubjectEncoding = System.Text.Encoding.UTF8;
            message.Body            = argBody;
            message.BodyEncoding    = System.Text.Encoding.UTF8;
            message.IsBodyHtml      = argIsBodyHtml;

            // send the mail
            client.Send(message);
        }