Get System Information and Send it in Email using C# .Net


Step for programme :
1.Read "systeminfo" command 2.Write it to string 3.Send Email

Sample Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GetSystemInfo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            // the cmd program
            startInfo.FileName = "cmd.exe";
            // set my arguments. date is just a dummy example. the real work isn't use date.
            startInfo.Arguments = "/c systeminfo";
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            process.StartInfo = startInfo;
            process.Start();
            // capture what is generated in command prompt
            string output = process.StandardOutput.ReadToEnd();
             

            process.WaitForExit();

            try
            {

                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                
                mail.From = new MailAddress("xyz@gmail.com");
                mail.To.Add("abc@gmail.com");
               
                mail.Subject = "System Configurations";
                mail.IsBodyHtml = true;
                mail.Body = output;

                SmtpServer.Port = 465;
                SmtpServer.Credentials = new System.Net.NetworkCredential("xtz@gmail.com", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                Application.Exit();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
    }
}

Comments

Popular posts from this blog

Create Short URL using C#