Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.18 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Mail;
  5. using System.Net.NetworkInformation;
  6. using System.Collections;
  7.  
  8. public class Test
  9. {
  10.     public static ArrayList globalList;
  11.  
  12.     static void Main(string[] args)
  13.     {
  14.  
  15.         //string bodydetails = "";
  16.         double percent = 0;
  17.  
  18.         string bodydetails = "";
  19.         ArrayList globalList = new ArrayList();
  20.  
  21.         Console.WriteLine(Environment.MachineName);
  22.         {
  23.             DriveInfo[] allDrives = DriveInfo.GetDrives();
  24.  
  25.             foreach (DriveInfo d in allDrives)
  26.  
  27.             {
  28.                 Console.WriteLine("Drive {0}", d.Name);
  29.                 Console.WriteLine("  Drive type: {0}", d.DriveType);
  30.                 if (d.IsReady == true)
  31.                 {
  32.                     Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
  33.                     Console.WriteLine("  File system: {0}", d.DriveFormat);
  34.                     Console.WriteLine(
  35.                         "  Available space to current user:{0, 15} bytes",
  36.                         d.AvailableFreeSpace, String.Format("Value: {0:P2}.", 0.100));
  37.                     //storage is binary not decimal
  38.                     Console.WriteLine(
  39.                         "  Total available space:          {0, 15} bytes",
  40.                         d.TotalFreeSpace);
  41.  
  42.                     Console.WriteLine(
  43.                         "  Total available space:          {0, 15} Gigabytes",
  44.                         d.TotalFreeSpace / 1024m / 1024m / 1024m);
  45.  
  46.                     Console.WriteLine(
  47.                         "  Total size of drive:            {0, 15} bytes ",
  48.                         d.TotalSize);
  49.  
  50.                     /*long total = d.TotalSize;
  51.  
  52.                     Console.WriteLine(
  53.                         "   Total size = {0}", total);*/
  54.  
  55.                     long half = 50 * d.TotalSize / 100;
  56.                     long quarter = 25 * d.TotalSize / 100;
  57.                     percent = ((double)d.TotalFreeSpace / (double)d.TotalSize) * 100;
  58.  
  59.  
  60.                     Console.WriteLine(
  61.                         "   current percentage = {0}", percent);
  62.                     Console.WriteLine(
  63.                         "   50% = {0}", half);
  64.                     Console.WriteLine(
  65.                         "   25% = {0}", quarter);
  66.  
  67.                     string drivelabel = d.VolumeLabel;
  68.                     bodydetails = Environment.MachineName + " " + d.Name + " on " + drivelabel + " has " + percent + "% remaining";
  69.                     globalList.Add(bodydetails);
  70.                 }
  71.             }
  72.             //globalList.Add(bodydetails);
  73.         }
  74.         htmlbody(globalList, percent, bodydetails);
  75.     }
  76.  
  77.     public static void htmlbody(ArrayList globalList, double percent, string bodydetails)
  78.     {
  79.         string body = string.Empty;
  80.         //string line = "replacementLine";
  81.         using (StreamReader reader = new StreamReader(("diskdiaghtml.html")))
  82.         {
  83.             body = reader.ReadToEnd();
  84.            
  85.             body = body.Replace("{userName}", bodydetails);
  86.            
  87.             //body = body.Replace("{userName}", bodydetails);
  88.             //body = body.Replace("{percent}", );
  89.             //body = body.Replace("{title}", "testline1");
  90.             //body = body.Replace("{url}", "testline2");
  91.             //body = body.Replace("{description}", "testline3");
  92.  
  93.             Console.WriteLine(body);
  94.                 //return body;
  95.                 //bodystring(body);
  96.            
  97.             email(globalList, percent, bodydetails, body, reader);
  98.         }
  99.     }
  100.  
  101.  
  102.     public static void email(ArrayList globalList, double percent, string bodydetails, string body, StreamReader reader)
  103.  
  104.     {
  105.         if (90 >= percent)
  106.         {
  107.             string myemailaddress = "Fromemail@gmail.com";
  108.             var fromAddress = new MailAddress(myemailaddress, "FromName");
  109.             var toAddress = new MailAddress("toemail@gmail.com", "del");
  110.             const string fromPassword = "password";
  111.             const string subject = "Subject";
  112.             //string body = globalList.ToString();
  113.             //string body = reader;
  114.             for (int i = 0; i < globalList.Count; i++)
  115.             {
  116.                 //body = body + globalList[i].ToString();
  117.                 Console.WriteLine(globalList[i].ToString());
  118.             }
  119.  
  120.             //Console.WriteLine(body);
  121.  
  122.  
  123.             var smtp = new SmtpClient
  124.             {
  125.                 Host = "smtp.gmail.com",
  126.                 Port = 587,
  127.                 EnableSsl = true,
  128.                 DeliveryMethod = SmtpDeliveryMethod.Network,
  129.                 UseDefaultCredentials = false,
  130.                 Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  131.             };
  132.             using (var message = new MailMessage(fromAddress, toAddress)
  133.             {
  134.                 Subject = subject,
  135.                 Body = body
  136.             })
  137.             {
  138.                 message.IsBodyHtml = true;
  139.                 smtp.Send(message);
  140.                 Console.WriteLine("email sent");
  141.             }
  142.             return;
  143.         }
  144.         {
  145.             //ping();
  146.             //Console.WriteLine("body");
  147.         }
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement