Guest User

Untitled

a guest
Dec 16th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Threading;
  4. using ActiveUp.Net.Mail;
  5. using System.ComponentModel;
  6. using System.Drawing;
  7.  
  8. namespace GmailNotifier
  9. {
  10. public partial class Form1 : Form
  11. {
  12. private readonly String user = "****************";
  13. private readonly String pass = "************";
  14. private BackgroundWorker bgw;
  15.  
  16. private Imap4Client imap;
  17. private Mailbox inbox;
  18.  
  19.  
  20. private int sleeptime = 5;
  21.  
  22. public Form1()
  23. {
  24. InitializeComponent();
  25. this.WindowState = FormWindowState.Minimized;
  26. this.notifyIcon1.Icon = SystemIcons.Shield;
  27. bgw = new BackgroundWorker();
  28. bgw.DoWork += (obj, sen) =>
  29. {
  30. this.imap = new Imap4Client();
  31. this.add("Connecting to imap.google.com:993");
  32. string resp = imap.ConnectSsl("imap.gmail.com", 993);
  33. this.add("Response: " + resp);
  34. this.add("Logging in with user " + user);
  35. resp = imap.Login(user, pass);
  36. this.add("Response: " + resp);
  37. this.add("Fetching INBOX");
  38. this.inbox = imap.SelectMailbox("INBOX");
  39. this.add("Init DONE");
  40. int t = this.sleeptime * 1000 + 1;
  41. while (!((DoWorkEventArgs)sen).Cancel)
  42. {
  43. if (t > this.sleeptime * 1000)
  44. {
  45. this.imap.Check();
  46. this.inbox = imap.ExamineMailbox("INBOX");
  47. MessageCollection unseens = this.inbox.SearchParse("UNSEEN");
  48. try
  49. {
  50. this.drawIcon(unseens.Count > 9 ? "9+" : unseens.Count.ToString());
  51. }
  52. catch (Exception ex)
  53. {
  54. add(ex.ToString());
  55. }
  56. /*
  57. if (unseens.Count > 0)
  58. {
  59. this.notifyIcon1.BalloonTipText = unseens.Count.ToString() + " unseen message in your inbox";
  60. this.notifyIcon1.ShowBalloonTip(5000);
  61. }
  62. else
  63. {
  64. this.drawIcon(0);
  65. }*/
  66. t = 0;
  67. }
  68. t += 100;
  69. Thread.Sleep(100);
  70. }
  71. };
  72. bgw.RunWorkerAsync();
  73. }
  74.  
  75.  
  76. private void add(string t)
  77. {
  78. if (this.InvokeRequired)
  79. {
  80. this.Invoke((MethodInvoker)delegate()
  81. {
  82. listView1.Items.Add(new System.Windows.Forms.ListViewItem(t));
  83. });
  84. }
  85. else
  86. {
  87. listView1.Items.Add(new System.Windows.Forms.ListViewItem(t));
  88. }
  89. }
  90.  
  91. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  92. {
  93. this.WindowState = FormWindowState.Minimized;
  94. this.bgw.CancelAsync();
  95. while (this.bgw.IsBusy)
  96. {
  97. Thread.Sleep(50);
  98. }
  99. this.notifyIcon1.Dispose();
  100. this.imap.Disconnect();
  101. Application.Exit();
  102. }
  103.  
  104. private void drawIcon(string c)
  105. {
  106. Bitmap square = new Bitmap(16, 16);
  107. Graphics G = Graphics.FromImage(square);
  108. Rectangle TextR = new Rectangle(0, 0, 16, 16);
  109. SolidBrush B = new SolidBrush(Color.DarkBlue);
  110.  
  111. G.FillRectangle(B, TextR);
  112. G.DrawString(c,
  113. new Font("Comic Sans MS", 8),
  114. Brushes.YellowGreen, TextR, StringFormat.GenericDefault);
  115. this.notifyIcon1.Icon = Icon.FromHandle(square.GetHicon());
  116. B.Dispose();
  117. }
  118. }
  119. }
Add Comment
Please, Sign In to add comment