コード例 #1
0
        private static void SendEmail(EmailSettings emailSettings, RunResult result, IEnumerable <string> args)
        {
            var cmdLine      = MakeCommandLine();
            var status       = (result.ExitCode == 0) ? "succeeded" : "failed";
            var computerName = Environment.MachineName;
            var subject      = $"[{computerName}] Task {status} : {cmdLine}";

            var bodyLines = new List <string>()
            {
                $"Computer: {computerName}",
                $"Username: {Environment.UserDomainName}\\{Environment.UserName}",
                $"Command line: {cmdLine}",
            };

            if (result.Output.Length > 0)
            {
                const string sep = "------------------------------------------------------------------------";
                bodyLines.AddRange(new[] { "", "Output:", sep, result.Output.Trim(), sep });
            }

            var body   = String.Join("\r\n", bodyLines);
            var client = new SmtpClient(emailSettings.Host);

            var msg = new MailMessage()
            {
                From    = emailSettings.From,
                Subject = subject,
                Body    = body
            };

            msg.To.Add(emailSettings.To);
            msg.Headers.Add("X-TaskRunner-Source", Environment.MachineName);
            client.Send(msg);

            string MakeCommandLine()
            {
                return(String.Join(" ", args.Select(a => a.Contains(" ") ? "\"" + a + "\"" : a)));
            }
        }