private TeamFoundationJobService GetJobService()
        {
            TeamFoundationServiceHostProperties deploymentHostProperties = new TeamFoundationServiceHostProperties();

            deploymentHostProperties.HostType          = TeamFoundationHostType.Deployment | TeamFoundationHostType.Application;
            deploymentHostProperties.Id                = Guid.Empty;
            deploymentHostProperties.PlugInDirectory   = Path.Combine(tfsInstallPath, TFS_JOB_EXTENSIONS_PATH);
            deploymentHostProperties.PhysicalDirectory = string.Empty;
            deploymentHostProperties.VirtualDirectory  = string.Empty;

            DeploymentServiceHost host     = null;
            ISqlConnectionInfo    connInfo = SqlConnectionInfoFactory.Create(connectionString, null, null);

            deploymentHostProperties.ConnectionInfo = connInfo;
            host = new DeploymentServiceHost(deploymentHostProperties, true);

            if (collectionContext != null)
            {
                collectionContext.Dispose();
                collectionContext = null;
            }
            if (deploymentContext != null)
            {
                deploymentContext.Dispose();
            }

            deploymentContext = host.CreateSystemContext();

            if (rbServer.Checked)
            {
                currentContext = deploymentContext;
                return(deploymentContext.GetService <TeamFoundationJobService>());
            }
            else
            {
                var hms = deploymentContext.GetService <TeamFoundationHostManagementService>();
                collectionContext = hms.BeginRequest(deploymentContext, tpc.InstanceId, RequestContextType.SystemContext);
                currentContext    = collectionContext;
                return(collectionContext.GetService <TeamFoundationJobService>());
            }
        }
        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);
        }