Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- using System.Threading;
- using ActiveUp.Net.Mail;
- using System.ComponentModel;
- using System.Drawing;
- namespace GmailNotifier
- {
- public partial class Form1 : Form
- {
- private readonly String user = "****************";
- private readonly String pass = "************";
- private BackgroundWorker bgw;
- private Imap4Client imap;
- private Mailbox inbox;
- private int sleeptime = 5;
- public Form1()
- {
- InitializeComponent();
- this.WindowState = FormWindowState.Minimized;
- this.notifyIcon1.Icon = SystemIcons.Shield;
- bgw = new BackgroundWorker();
- bgw.DoWork += (obj, sen) =>
- {
- this.imap = new Imap4Client();
- this.add("Connecting to imap.google.com:993");
- string resp = imap.ConnectSsl("imap.gmail.com", 993);
- this.add("Response: " + resp);
- this.add("Logging in with user " + user);
- resp = imap.Login(user, pass);
- this.add("Response: " + resp);
- this.add("Fetching INBOX");
- this.inbox = imap.SelectMailbox("INBOX");
- this.add("Init DONE");
- int t = this.sleeptime * 1000 + 1;
- while (!((DoWorkEventArgs)sen).Cancel)
- {
- if (t > this.sleeptime * 1000)
- {
- this.imap.Check();
- this.inbox = imap.ExamineMailbox("INBOX");
- MessageCollection unseens = this.inbox.SearchParse("UNSEEN");
- try
- {
- this.drawIcon(unseens.Count > 9 ? "9+" : unseens.Count.ToString());
- }
- catch (Exception ex)
- {
- add(ex.ToString());
- }
- /*
- if (unseens.Count > 0)
- {
- this.notifyIcon1.BalloonTipText = unseens.Count.ToString() + " unseen message in your inbox";
- this.notifyIcon1.ShowBalloonTip(5000);
- }
- else
- {
- this.drawIcon(0);
- }*/
- t = 0;
- }
- t += 100;
- Thread.Sleep(100);
- }
- };
- bgw.RunWorkerAsync();
- }
- private void add(string t)
- {
- if (this.InvokeRequired)
- {
- this.Invoke((MethodInvoker)delegate()
- {
- listView1.Items.Add(new System.Windows.Forms.ListViewItem(t));
- });
- }
- else
- {
- listView1.Items.Add(new System.Windows.Forms.ListViewItem(t));
- }
- }
- private void exitToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Minimized;
- this.bgw.CancelAsync();
- while (this.bgw.IsBusy)
- {
- Thread.Sleep(50);
- }
- this.notifyIcon1.Dispose();
- this.imap.Disconnect();
- Application.Exit();
- }
- private void drawIcon(string c)
- {
- Bitmap square = new Bitmap(16, 16);
- Graphics G = Graphics.FromImage(square);
- Rectangle TextR = new Rectangle(0, 0, 16, 16);
- SolidBrush B = new SolidBrush(Color.DarkBlue);
- G.FillRectangle(B, TextR);
- G.DrawString(c,
- new Font("Comic Sans MS", 8),
- Brushes.YellowGreen, TextR, StringFormat.GenericDefault);
- this.notifyIcon1.Icon = Icon.FromHandle(square.GetHicon());
- B.Dispose();
- }
- }
- }
Add Comment
Please, Sign In to add comment