Click here to Skip to main content
15,879,074 members
Articles / Programming Languages / C#

Gmail Agent API v0.5 / Mail Notifier & Address Importer

Rate me:
Please Sign up or sign in to vote.
4.97/5 (64 votes)
6 Jul 20046 min read 353.9K   11.5K   198  
Open source Gmail API in C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Johnvey.GmailAgent;
using Microsoft.Win32;

namespace Johnvey.GmailAgent.Applet
{
	/// <summary>
	/// Summary description for Accounts.
	/// </summary>
	public class Accounts : System.Windows.Forms.Form
	{

		private DataTable accounts;
		private RegistryKey workingRegistry;
		private RegistryKey accountsKey;
		private bool hasUpdatedAccounts;
		public BaseWindow RealParentForm;

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button okButton;
		private System.Windows.Forms.Button cancelButton;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox accountCombo;
		private System.Windows.Forms.TextBox usernameBox;
		private System.Windows.Forms.TextBox passwordBox;
		private System.Windows.Forms.Button deleteButton;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Accounts()
		{
			InitializeComponent();


			// get the account logins
			workingRegistry = Application.UserAppDataRegistry;
			accountsKey = workingRegistry.CreateSubKey("Accounts");

			// create table of accounts
			accounts = new DataTable();
			accounts.Columns.Add(new DataColumn("Username",typeof(string)));
			accounts.Columns.Add(new DataColumn("Password",typeof(string)));

			RefreshAccountsTable();

			hasUpdatedAccounts = false;

			accountCombo.DataSource = accounts;
			accountCombo.DisplayMember = "Username";

		}

		protected void RefreshAccountsTable() {

			this.accounts.Rows.Clear();

			// insert "add" account type
			DataRow addRow = accounts.NewRow();
			addRow["Username"] = "<Add>";
			addRow["Password"] = null;
			accounts.Rows.Add(addRow);

			// add accounts to drop down box
			foreach(string accountName in accountsKey.GetValueNames()) {
				DataRow row = accounts.NewRow();
				row["Username"] = accountName;
				row["Password"] = SimpleEncryption.Decrypt(accountsKey.GetValue(accountName).ToString(), "GmailAgent0500");
				accounts.Rows.Add(row);
			}

			this.accountCombo.Refresh();
			this.usernameBox.Text = "";
			this.passwordBox.Text = "";
			
			this.hasUpdatedAccounts = true;

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.okButton = new System.Windows.Forms.Button();
			this.cancelButton = new System.Windows.Forms.Button();
			this.accountCombo = new System.Windows.Forms.ComboBox();
			this.usernameBox = new System.Windows.Forms.TextBox();
			this.passwordBox = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.deleteButton = new System.Windows.Forms.Button();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(16, 19);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 19);
			this.label1.TabIndex = 1;
			this.label1.Text = "Account";
			// 
			// okButton
			// 
			this.okButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.okButton.Location = new System.Drawing.Point(96, 96);
			this.okButton.Name = "okButton";
			this.okButton.TabIndex = 2;
			this.okButton.Text = "Add";
			this.okButton.Click += new System.EventHandler(this.add_onClick);
			// 
			// cancelButton
			// 
			this.cancelButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.cancelButton.Location = new System.Drawing.Point(213, 200);
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.TabIndex = 2;
			this.cancelButton.Text = "Close";
			this.cancelButton.Click += new System.EventHandler(this.close_onClick);
			// 
			// accountCombo
			// 
			this.accountCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.accountCombo.Location = new System.Drawing.Point(80, 16);
			this.accountCombo.Name = "accountCombo";
			this.accountCombo.Size = new System.Drawing.Size(208, 21);
			this.accountCombo.TabIndex = 0;
			this.accountCombo.SelectedIndexChanged += new System.EventHandler(this.accountCombo_SelectedIndexChanged);
			// 
			// usernameBox
			// 
			this.usernameBox.Location = new System.Drawing.Point(96, 32);
			this.usernameBox.MaxLength = 100;
			this.usernameBox.Name = "usernameBox";
			this.usernameBox.Size = new System.Drawing.Size(136, 20);
			this.usernameBox.TabIndex = 0;
			this.usernameBox.Text = "";
			this.usernameBox.TextChanged += new System.EventHandler(this.username_onChange);
			// 
			// passwordBox
			// 
			this.passwordBox.Location = new System.Drawing.Point(96, 64);
			this.passwordBox.MaxLength = 100;
			this.passwordBox.Name = "passwordBox";
			this.passwordBox.PasswordChar = '*';
			this.passwordBox.Size = new System.Drawing.Size(136, 20);
			this.passwordBox.TabIndex = 1;
			this.passwordBox.Text = "";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.deleteButton);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.passwordBox);
			this.groupBox1.Controls.Add(this.usernameBox);
			this.groupBox1.Controls.Add(this.okButton);
			this.groupBox1.Location = new System.Drawing.Point(16, 48);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(272, 136);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Login Information";
			// 
			// deleteButton
			// 
			this.deleteButton.Enabled = false;
			this.deleteButton.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.deleteButton.Location = new System.Drawing.Point(176, 96);
			this.deleteButton.Name = "deleteButton";
			this.deleteButton.TabIndex = 3;
			this.deleteButton.Text = "Delete";
			this.deleteButton.Click += new System.EventHandler(this.delete_onClick);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 66);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 23);
			this.label3.TabIndex = 8;
			this.label3.Text = "Password";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 34);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 23);
			this.label2.TabIndex = 7;
			this.label2.Text = "Username";
			// 
			// Accounts
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(306, 240);
			this.Controls.Add(this.accountCombo);
			this.Controls.Add(this.cancelButton);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.groupBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Accounts";
			this.ShowInTaskbar = false;
			this.Text = "Accounts";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion



		private void add_onClick(object sender, System.EventArgs e) {
			if(this.usernameBox.Text != "" && this.passwordBox.Text != "") {
				this.accountsKey.SetValue(this.usernameBox.Text, SimpleEncryption.Encrypt(this.passwordBox.Text, "GmailAgent0500"));
				RefreshAccountsTable();

			}
		}

		private void close_onClick(object sender, System.EventArgs e) {
			if(this.hasUpdatedAccounts) {
				this.RealParentForm.RefreshLoginInfo();
			}
			this.Close();
		}

		private void delete_onClick(object sender, System.EventArgs e) {
			if(this.usernameBox.Text != "") {
				this.accountsKey.DeleteValue(this.usernameBox.Text, false);
				RefreshAccountsTable();
			}
		}

		private void accountCombo_SelectedIndexChanged(object sender, System.EventArgs e) {
			if(this.accountCombo.SelectedIndex == 0) {
				this.usernameBox.Text = "";
				this.passwordBox.Text = "";
			} else {
				DataRowView account = (DataRowView)this.accountCombo.SelectedItem;
				this.usernameBox.Text = (string)account["Username"];
				this.passwordBox.Text = (string)account["Password"];
			}
		}

		private void username_onChange(object sender, System.EventArgs e) {
			if(this.usernameBox.Text != "") {
				this.deleteButton.Enabled = true;
			} else {
				this.deleteButton.Enabled = false;
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions