Выключение Windows сервера по звонку. Клиентская часть. v3.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinSCP;
using System.Diagnostics;
using System.Net;
using System.Net.Mail;
using System.IO;

namespace RedButtonChecker
{
    class Program
    {
        public static void SendMail(string smtpServer, string from, string userName, string password, string mailto, string caption, string message, string attachFile = null)
        {
            try
            {
                MailMessage mail = new MailMessage();
                mail.From = new MailAddress(from);
                mail.To.Add(new MailAddress(mailto));
                mail.Subject = caption;
                mail.Body = message;
                if (!string.IsNullOrEmpty(attachFile))
                    mail.Attachments.Add(new Attachment(attachFile));
                SmtpClient client = new SmtpClient();
                client.Host = smtpServer;
                client.Port = 587;
                client.EnableSsl = false;
                client.Credentials = new NetworkCredential(userName, password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Send(mail);
                mail.Dispose();
                StringBuilder sbmail = new StringBuilder();
                sbmail.Append(String.Format("{0} Message sent to {1}", DateTime.Now, mailto) + System.Environment.NewLine);
                File.AppendAllText("log.txt", sbmail.ToString());
                sbmail.Clear();
            }
            catch (Exception e)
            {
                throw new Exception("Mail.Send: " + e.Message);
            }
        }

        static void Main(string[] args)
        {
            StringBuilder sbmain = new StringBuilder();

            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                HostName = "ftpip",
                UserName = "ftpuser",
                Password = "ftppass",
            };

            Session session = new Session();
            session.Open(sessionOptions);

            if (session.FileExists("test.tmp"))
            {
                sbmain.Append(String.Format("{0} Flag exists, deleting and doing our work!{1}", DateTime.Now, System.Environment.NewLine));
                File.AppendAllText("log.txt", sbmain.ToString());
                sbmain.Clear();
                session.RemoveFiles("test.tmp");
                Process.Start("shutdown", "/s /t 10");
                SendMail("mailsrv", "from", "login", "password", "to", "Test server is shutting down", "Shutdown report");
            }
            return;
        }
    }
}
Поделиться
Отправить
2018   c#