Advertisement
Guest User

Untitled

a guest
Jul 21st, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.ServiceProcess;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Threading;
  11. using System.IO.Ports;
  12. using System.Xml;
  13. using System.Net;
  14.  
  15. namespace GmailNotifyDaemon
  16. {
  17.     public partial class Service1 : ServiceBase
  18.     {
  19.         public Service1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.  
  24.         protected override void OnStart(string[] args)
  25.         {
  26.             int unread;
  27.             try
  28.             {
  29.  
  30.                 SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
  31.                 port.Open();
  32.                 //string tostop = "0";
  33.                 unread = checkmail();
  34.                 while (true)
  35.                 {
  36.                     unread = checkmail();
  37.                     //Console.WriteLine("Unread Mails: " + unread.ToString());
  38.                     port.Write(unread.ToString());
  39.                     Thread.Sleep(5000);
  40.                     //Console.WriteLine(port.ReadLine());
  41.                 }
  42.  
  43.             }
  44.             catch (Exception er)
  45.             {
  46.                 //Console.WriteLine("Error on Connection to COM4: " + er);
  47.             }
  48.         }
  49.  
  50.         static int checkmail()
  51.         {
  52.             int nr = 0;
  53.             try
  54.             {
  55.                 System.Net.WebClient objClient = new System.Net.WebClient();
  56.                 string response;
  57.                 // string title;
  58.                 //string summary;
  59.  
  60.                 //Creating a new xml document
  61.                 XmlDocument doc = new XmlDocument();
  62.  
  63.                 //Logging in Gmail server to get data
  64.                 objClient.Credentials = new System.Net.NetworkCredential("litruv@gmail.com", "imD1ff3r3n+");
  65.                 //reading data and converting to string
  66.                 response = Encoding.UTF8.GetString(objClient.DownloadData(@"https://mail.google.com/mail/feed/atom"));
  67.  
  68.                 response = response.Replace(@"<feed version=""0.3"" xmlns=""http://purl.org/atom/ns#"">", @"<feed>");
  69.  
  70.                 //loading into an XML so we can get information easily
  71.                 doc.LoadXml(response);
  72.  
  73.                 //nr of emails
  74.  
  75.                 nr = Int32.Parse(doc.SelectSingleNode(@"/feed/fullcount").InnerText);
  76.                 // Console.WriteLine(nr);
  77.  
  78.  
  79.  
  80.             }
  81.  
  82.             catch (Exception e)
  83.             {
  84.                 //Console.WriteLine("Check your network connection. ERROR: " + e);
  85.             }
  86.  
  87.             return nr;
  88.         }
  89.  
  90.         protected override void OnStop()
  91.         {
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement