Click here to Skip to main content
15,887,683 members
Articles / Programming Languages / C#

Quick Mail

Rate me:
Please Sign up or sign in to vote.
2.96/5 (24 votes)
7 Apr 2003CPOL1 min read 175.4K   2K   40  
Easy way to send mails using SMTP, in .NET.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;

//Author       : Mougamadou Acharaffaly Maricar
//Date Written : 17th March 2003
//Date Modified: 24th March 2003
//Version      :1.0
//Build	       : 
namespace QuickMail
{
	/// <summary>
	/// Summary description for AddRecipients.
	/// </summary>
	public class AddRecipients : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label LblShowNames;
		private System.Windows.Forms.Label LblSelectFromList;
		private System.Windows.Forms.Button btnTo;
		private System.Windows.Forms.Button btnCc;
		private System.Windows.Forms.Button btnBcc;
		private System.Windows.Forms.Label LblMessageRecipients;
		private System.Windows.Forms.Button txtMessageRecipientsOK;
		private System.Windows.Forms.Button txtMessageRecipientsCancel;
		private System.Windows.Forms.ComboBox cbAddRecipientsGroups;
		public string strAddrecipientsTo=String.Empty,strAddrecipientsCc=String.Empty,strAddrecipientsBcc=String.Empty ; 


		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.ListBox LstUserNames;
		protected System.Windows.Forms.TextBox txtAddRecipientsTo;
		private System.Windows.Forms.TextBox txtAddRecipientsCc;
		private System.Windows.Forms.TextBox txtAddRecipientsBcc;
		private DataBroker db=null;
		/// <summary>
		/// Show combo box values while form loads
		/// </summary>
		public AddRecipients()
		{
			InitializeComponent();
			db=new DataBroker(); 
			DataTable dtGroup=db.GetGroup(false);
			dtGroup.TableName="GroupNames";
			cbAddRecipientsGroups.DataSource =dtGroup;
			cbAddRecipientsGroups.DisplayMember="GroupName"; 
			cbAddRecipientsGroups.ValueMember="GroupName"; 
		}

		/// <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.LblShowNames = new System.Windows.Forms.Label();
			this.cbAddRecipientsGroups = new System.Windows.Forms.ComboBox();
			this.LblSelectFromList = new System.Windows.Forms.Label();
			this.btnTo = new System.Windows.Forms.Button();
			this.btnCc = new System.Windows.Forms.Button();
			this.btnBcc = new System.Windows.Forms.Button();
			this.txtAddRecipientsTo = new System.Windows.Forms.TextBox();
			this.txtAddRecipientsCc = new System.Windows.Forms.TextBox();
			this.txtAddRecipientsBcc = new System.Windows.Forms.TextBox();
			this.LblMessageRecipients = new System.Windows.Forms.Label();
			this.txtMessageRecipientsOK = new System.Windows.Forms.Button();
			this.txtMessageRecipientsCancel = new System.Windows.Forms.Button();
			this.LstUserNames = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// LblShowNames
			// 
			this.LblShowNames.Location = new System.Drawing.Point(40, 16);
			this.LblShowNames.Name = "LblShowNames";
			this.LblShowNames.Size = new System.Drawing.Size(136, 23);
			this.LblShowNames.TabIndex = 0;
			this.LblShowNames.Text = "Show Names from the :";
			// 
			// cbAddRecipientsGroups
			// 
			this.cbAddRecipientsGroups.Location = new System.Drawing.Point(184, 16);
			this.cbAddRecipientsGroups.Name = "cbAddRecipientsGroups";
			this.cbAddRecipientsGroups.Size = new System.Drawing.Size(121, 21);
			this.cbAddRecipientsGroups.TabIndex = 1;
			this.cbAddRecipientsGroups.SelectedIndexChanged += new System.EventHandler(this.cbAddRecipientsGroups_SelectedIndexChanged);
			// 
			// LblSelectFromList
			// 
			this.LblSelectFromList.Location = new System.Drawing.Point(16, 48);
			this.LblSelectFromList.Name = "LblSelectFromList";
			this.LblSelectFromList.TabIndex = 3;
			this.LblSelectFromList.Text = "Select from List";
			// 
			// btnTo
			// 
			this.btnTo.Location = new System.Drawing.Point(184, 80);
			this.btnTo.Name = "btnTo";
			this.btnTo.Size = new System.Drawing.Size(56, 23);
			this.btnTo.TabIndex = 4;
			this.btnTo.Text = "To ->";
			this.btnTo.Click += new System.EventHandler(this.btnTo_Click);
			// 
			// btnCc
			// 
			this.btnCc.Location = new System.Drawing.Point(184, 136);
			this.btnCc.Name = "btnCc";
			this.btnCc.Size = new System.Drawing.Size(56, 23);
			this.btnCc.TabIndex = 5;
			this.btnCc.Text = "Cc ->";
			this.btnCc.Click += new System.EventHandler(this.btnCc_Click);
			// 
			// btnBcc
			// 
			this.btnBcc.Location = new System.Drawing.Point(184, 192);
			this.btnBcc.Name = "btnBcc";
			this.btnBcc.Size = new System.Drawing.Size(56, 23);
			this.btnBcc.TabIndex = 6;
			this.btnBcc.Text = "Bcc ->";
			this.btnBcc.Click += new System.EventHandler(this.btnBcc_Click);
			// 
			// txtAddRecipientsTo
			// 
			this.txtAddRecipientsTo.Location = new System.Drawing.Point(256, 72);
			this.txtAddRecipientsTo.Multiline = true;
			this.txtAddRecipientsTo.Name = "txtAddRecipientsTo";
			this.txtAddRecipientsTo.Size = new System.Drawing.Size(152, 48);
			this.txtAddRecipientsTo.TabIndex = 7;
			this.txtAddRecipientsTo.Text = "";
			// 
			// txtAddRecipientsCc
			// 
			this.txtAddRecipientsCc.Location = new System.Drawing.Point(256, 128);
			this.txtAddRecipientsCc.Multiline = true;
			this.txtAddRecipientsCc.Name = "txtAddRecipientsCc";
			this.txtAddRecipientsCc.Size = new System.Drawing.Size(152, 48);
			this.txtAddRecipientsCc.TabIndex = 8;
			this.txtAddRecipientsCc.Text = "";
			// 
			// txtAddRecipientsBcc
			// 
			this.txtAddRecipientsBcc.Location = new System.Drawing.Point(256, 184);
			this.txtAddRecipientsBcc.Multiline = true;
			this.txtAddRecipientsBcc.Name = "txtAddRecipientsBcc";
			this.txtAddRecipientsBcc.Size = new System.Drawing.Size(152, 48);
			this.txtAddRecipientsBcc.TabIndex = 9;
			this.txtAddRecipientsBcc.Text = "";
			// 
			// LblMessageRecipients
			// 
			this.LblMessageRecipients.Location = new System.Drawing.Point(256, 48);
			this.LblMessageRecipients.Name = "LblMessageRecipients";
			this.LblMessageRecipients.Size = new System.Drawing.Size(128, 23);
			this.LblMessageRecipients.TabIndex = 10;
			this.LblMessageRecipients.Text = "Message Recipients";
			// 
			// txtMessageRecipientsOK
			// 
			this.txtMessageRecipientsOK.Location = new System.Drawing.Point(136, 240);
			this.txtMessageRecipientsOK.Name = "txtMessageRecipientsOK";
			this.txtMessageRecipientsOK.TabIndex = 11;
			this.txtMessageRecipientsOK.Text = "OK";
			this.txtMessageRecipientsOK.Click += new System.EventHandler(this.txtMessageRecipientsOK_Click);
			// 
			// txtMessageRecipientsCancel
			// 
			this.txtMessageRecipientsCancel.Location = new System.Drawing.Point(216, 240);
			this.txtMessageRecipientsCancel.Name = "txtMessageRecipientsCancel";
			this.txtMessageRecipientsCancel.TabIndex = 12;
			this.txtMessageRecipientsCancel.Text = "Cancel";
			this.txtMessageRecipientsCancel.Click += new System.EventHandler(this.txtMessageRecipientsCancel_Click);
			// 
			// LstUserNames
			// 
			this.LstUserNames.Location = new System.Drawing.Point(16, 72);
			this.LstUserNames.Name = "LstUserNames";
			this.LstUserNames.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
			this.LstUserNames.Size = new System.Drawing.Size(152, 160);
			this.LstUserNames.TabIndex = 15;
			// 
			// AddRecipients
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(424, 285);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.LstUserNames,
																		  this.txtMessageRecipientsCancel,
																		  this.txtMessageRecipientsOK,
																		  this.LblMessageRecipients,
																		  this.txtAddRecipientsBcc,
																		  this.txtAddRecipientsCc,
																		  this.txtAddRecipientsTo,
																		  this.btnBcc,
																		  this.btnCc,
																		  this.btnTo,
																		  this.LblSelectFromList,
																		  this.cbAddRecipientsGroups,
																		  this.LblShowNames});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "AddRecipients";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Add Recipients";
			this.ResumeLayout(false);

		}
		#endregion
		/// <summary>
		/// Fill appropriate values in list box when combox is changed
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cbAddRecipientsGroups_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			string ss=String.Empty;
			db=new DataBroker();
			if (cbAddRecipientsGroups.SelectedIndex >-1)
			{
				ss=cbAddRecipientsGroups.SelectedValue.ToString(); 
				DataTable dt;

				if (ss.Equals("All"))
				{
					dt=db.GetData();
				}
				else
				{
					dt=db.GetDataGroupWise(ss);
				}
				if (dt.Rows.Count > 0)
				{
					dt.TableName="Users"; 
					LstUserNames.DataSource=dt; 
					LstUserNames.DisplayMember="UserName"; 
					LstUserNames.ValueMember="MailId";
				}
				else
				{
					dt.TableName="Users"; 
					LstUserNames.DataSource=dt; 
				}
			}
		}
		/// <summary>
		/// Moveup Selected usernames to message recipients TO listbox
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnTo_Click(object sender, System.EventArgs e)
		{

			string strTemp=String.Empty;
			if (LstUserNames.SelectedIndex> -1)
			{
				if (LstUserNames.SelectedItems.Count >   1)
				{
					int[]  selected = new int[LstUserNames.SelectedIndices.Count];
					LstUserNames.SelectedIndices.CopyTo(selected, 0);
					int nIncrement=0;
					for(int i = 0;i<selected.Length ;i++)
					{
						nIncrement+=1;
						int ii=selected[i];
						DataRowView item= (DataRowView)LstUserNames.Items[ii];
						if (i == selected.Length-1 ) 
							strTemp+=item.Row["MailId"].ToString() ;
						else
							strTemp+=item.Row["MailId"].ToString() + ";"; 
					}
				}
				else
				{
					strTemp= LstUserNames.SelectedValue.ToString() ;
				}
			}
			if (txtAddRecipientsTo.Text.Equals(String.Empty))
				txtAddRecipientsTo.Text+=strTemp;
			else
				txtAddRecipientsTo.Text+=";" + strTemp;
		}
		/// <summary>
		/// Close the current dialog and set selected values to variable
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void txtMessageRecipientsOK_Click(object sender, System.EventArgs e)
		{
			strAddrecipientsTo+=txtAddRecipientsTo.Text; 
			strAddrecipientsCc+=txtAddRecipientsCc.Text; 
			strAddrecipientsBcc+=txtAddRecipientsBcc.Text; 
			this.Close();
						
		}
		/// <summary>
		/// Moveup Selected usernames to message recipients CC listbox
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnCc_Click(object sender, System.EventArgs e)
		{
			string strTemp=String.Empty;
			if (LstUserNames.SelectedIndex> -1)
			{
				if (LstUserNames.SelectedItems.Count >   1)
				{
					int[]  selected = new int[LstUserNames.SelectedIndices.Count];
					LstUserNames.SelectedIndices.CopyTo(selected, 0);
					int nIncrement=0;
					for(int i = 0;i<selected.Length ;i++)
					{
						nIncrement+=1;
						int ii=selected[i];
						DataRowView item= (DataRowView)LstUserNames.Items[ii];
						if (i == selected.Length-1 ) 
							strTemp+=item.Row["MailId"].ToString() ;
						else
							strTemp+=item.Row["MailId"].ToString() + ";"; 

					}
				}
				else
				{
					strTemp= LstUserNames.SelectedValue.ToString() ;
				}
			}
			if (txtAddRecipientsCc.Text.Equals(String.Empty))
				txtAddRecipientsCc.Text+=strTemp;
			else
				txtAddRecipientsCc.Text+=";" + strTemp;

		}

		/// <summary>
		/// Moveup Selected usernames to message recipients BCC listbox
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnBcc_Click(object sender, System.EventArgs e)
		{
			string strTemp=String.Empty;
			if (LstUserNames.SelectedIndex> -1)
			{
				if (LstUserNames.SelectedItems.Count >   1)
				{
					int[]  selected = new int[LstUserNames.SelectedIndices.Count];
					LstUserNames.SelectedIndices.CopyTo(selected, 0);
					int nIncrement=0;
					for(int i = 0;i<selected.Length ;i++)
					{
						nIncrement+=1;
						int ii=selected[i];
						DataRowView item= (DataRowView)LstUserNames.Items[ii];
						if (i == selected.Length-1 ) 
							strTemp+=item.Row["MailId"].ToString() ;
						else
							strTemp+=item.Row["MailId"].ToString() + ";"; 
					}
				}
				else
				{
					strTemp= LstUserNames.SelectedValue.ToString() ;
				}
			}
			if (txtAddRecipientsBcc.Text.Equals(String.Empty))
				txtAddRecipientsBcc.Text+=strTemp;
			else
				txtAddRecipientsBcc.Text+=";" + strTemp;

		}
		/// <summary>
		/// Close the current dialog
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>

		private void txtMessageRecipientsCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}	
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
I am a system analyst and have been with Microsoft & Sun Technologies for more than 7 years. I have always been fascinated by java and .NET. I take lot of technical articles and writing them.

I am a Sun Certified Java Programmer for Java 2 Platform 1.4 , Web component developer Java 2 Platform - Enterprise Edition 1.4 and Microsoft certified developer using C#.NET in Web Development ASP.NET.

Visit my web site www.mohamedashraf.tk

Comments and Discussions