Thanks for all the replies.
Here is some sample code I have. Feel free to take a look and make any
comments:
The Form:
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Messagin g;
using System.Threadin g;
using CardFinder;
namespace TestHandler
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.GroupBox grpSendMails;
private System.Windows. Forms.TextBox txtMailCount;
private System.Windows. Forms.Label label1;
private System.Windows. Forms.Button btnReceiveMail;
private System.Windows. Forms.Button btnSendMail;
private System.Windows. Forms.TextBox txtTo;
private System.Windows. Forms.GroupBox grpReceive;
private System.Windows. Forms.TextBox txtThreadCount;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
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 InitializeCompo nent()
{
this.btnReceive Mail = new System.Windows. Forms.Button();
this.grpSendMai ls = new System.Windows. Forms.GroupBox( );
this.label1 = new System.Windows. Forms.Label();
this.txtMailCou nt = new System.Windows. Forms.TextBox() ;
this.btnSendMai l = new System.Windows. Forms.Button();
this.txtTo = new System.Windows. Forms.TextBox() ;
this.grpReceive = new System.Windows. Forms.GroupBox( );
this.txtThreadC ount = new System.Windows. Forms.TextBox() ;
this.grpSendMai ls.SuspendLayou t();
this.grpReceive .SuspendLayout( );
this.SuspendLay out();
//
// btnReceiveMail
//
this.btnReceive Mail.Location = new System.Drawing. Point(248, 48);
this.btnReceive Mail.Name = "btnReceiveMail ";
this.btnReceive Mail.TabIndex = 1;
this.btnReceive Mail.Text = "Recieve";
this.btnReceive Mail.Click += new
System.EventHan dler(this.btnRe ceiveMail_Click );
//
// grpSendMails
//
this.grpSendMai ls.Controls.Add Range(new System.Windows. Forms.Control[] {
this.txtTo,
this.label1,
this.txtMailCou nt,
this.btnSendMai l});
this.grpSendMai ls.Location = new System.Drawing. Point(16, 16);
this.grpSendMai ls.Name = "grpSendMai ls";
this.grpSendMai ls.Size = new System.Drawing. Size(384, 112);
this.grpSendMai ls.TabIndex = 2;
this.grpSendMai ls.TabStop = false;
this.grpSendMai ls.Text = "Send Mails";
//
// label1
//
this.label1.Loc ation = new System.Drawing. Point(32, 64);
this.label1.Nam e = "label1";
this.label1.Siz e = new System.Drawing. Size(80, 24);
this.label1.Tab Index = 3;
this.label1.Tex t = "No of mails:";
//
// txtMailCount
//
this.txtMailCou nt.Location = new System.Drawing. Point(120, 64);
this.txtMailCou nt.Name = "txtMailCou nt";
this.txtMailCou nt.Size = new System.Drawing. Size(112, 20);
this.txtMailCou nt.TabIndex = 2;
this.txtMailCou nt.Text = "100000";
//
// btnSendMail
//
this.btnSendMai l.Location = new System.Drawing. Point(240, 64);
this.btnSendMai l.Name = "btnSendMai l";
this.btnSendMai l.TabIndex = 1;
this.btnSendMai l.Text = "Send";
this.btnSendMai l.Click += new
System.EventHan dler(this.btnSe ndMail_Click);
//
// txtTo
//
this.txtTo.Loca tion = new System.Drawing. Point(120, 32);
this.txtTo.Name = "txtTo";
this.txtTo.Size = new System.Drawing. Size(112, 20);
this.txtTo.TabI ndex = 4;
this.txtTo.Text = "sa****@rubbach icken.com";
//
// grpReceive
//
this.grpReceive .Controls.AddRa nge(new System.Windows. Forms.Control[] {
this.txtThreadC ount,
this.btnReceive Mail});
this.grpReceive .Location = new System.Drawing. Point(16, 136);
this.grpReceive .Name = "grpReceive ";
this.grpReceive .Size = new System.Drawing. Size(384, 128);
this.grpReceive .TabIndex = 3;
this.grpReceive .TabStop = false;
this.grpReceive .Text = "Receive Mails";
//
// txtThreadCount
//
this.txtThreadC ount.Location = new System.Drawing. Point(136, 48);
this.txtThreadC ount.Name = "txtThreadCount ";
this.txtThreadC ount.TabIndex = 2;
this.txtThreadC ount.Text = "5";
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(416, 317);
this.Controls.A ddRange(new System.Windows. Forms.Control[] {
this.grpReceive ,
this.grpSendMai ls});
this.Name = "Form1";
this.Text = "Form1";
this.grpSendMai ls.ResumeLayout (false);
this.grpReceive .ResumeLayout(f alse);
this.ResumeLayo ut(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}
/// <summary>
/// Creates test emails
/// </summary>
private void btnSendMail_Cli ck(object sender, System.EventArg s e)
{
// Create a new mailer instance
Mailer mailer = new Mailer();
try
{
// Create x number of test messages in the queue
mailer.SendMail (txtTo.Text.Tri m(),
Convert.ToInt32 (txtMailCount.T ext.Trim()));
MessageBox.Show ("Done");
}
catch(Exception ex)
{
MessageBox.Show (ex.Message);
}
}
/// <summary>
/// Starts threads to create emails from the messages in the queue
/// </summary>
private void btnReceiveMail_ Click(object sender, System.EventArg s e)
{
// How many threads
int THREAD_COUNT = Convert.ToInt32 (txtThreadCount .Text.Trim());
int loop;
Mailer mailer = new Mailer();
Thread[] ThreadArray = new Thread[THREAD_COUNT];
// Loop through and create listener threads that process the messages in
the queue
for(loop = 0; loop < THREAD_COUNT; loop++)
{
ThreadArray[loop] = new Thread(new ThreadStart(mai ler.Listen));
ThreadArray[loop].Start();
}
}
}
}
The Mailer Class:
using System;
using System.Messagin g;
using System.Threadin g;
using System.Web.Mail ;
namespace CardFinder
{
/// <summary>
/// Summary description for Mailer.
/// </summary>
public class Mailer
{
public Mailer()
{
}
public void SendMail(string Email)
{
SendMail(Email, 1);
}
/// <summary>
/// Creates messages in the queue
/// </summary>
/// <param name="Email">Em ail to send to</param>
/// <param name="MailCount ">No of messages to create</param>
public void SendMail(string Email, int MailCount)
{
// Get the queue
MessageQueue CardFinderQueue = new
MessageQueue(@" .\Private$\Card FinderMails");
// Create messages
for(int loop = 0;loop < MailCount;loop+ +)
{
MailMessage msg = new MailMessage();
msg.Email = Email;
CardFinderQueue .Send(msg, "Email Test");
}
}
/// <summary>
/// Processes the queue
/// </summary>
public void Listen()
{
// Get the queue
MessageQueue CardFinderQueue = new
MessageQueue(@" .\Private$\Card FinderMails");
CardFinderQueue .Formatter = new XmlMessageForma tter(new Type[]
{typeof(CardFin der.MailMessage )});
Message qmsg = null;
while (true)
{
try
{
System.Threadin g.Thread.Sleep( 100);
qmsg = CardFinderQueue .Receive(new TimeSpan(0,0,0, 1));
MailMessage msg = (MailMessage)qm sg.Body;
string email = msg.Email;
SmtpMail.Send(" so*****@myco.co m", email, "CardFinder Test",
"<html><body><h 1>Test</h1></body></html>");
}
// Not sure what the following is for :-)
catch(ThreadInt erruptedExcepti on threadEx)
{
qmsg.Dispose();
CardFinderQueue .Dispose();
break;
}
catch(Exception Ex)
{
}
}
}
}
}
The MailMessage Class:
using System;
namespace CardFinder
{
/// <summary>
/// Summary description for MailMessage.
/// </summary>
public class MailMessage
{
private string _email;
public string Email
{
get
{
return _email;
}
set
{
_email = value;
}
}
}
}
Thanks
S
"SamIAm" <sa****@rubbach icken.com> wrote in message
news:ey******** ******@tk2msftn gp13.phx.gbl...
Hi am creating a email application that needs to mail out a very large
amount of emails. I have created a multithreaded c# application that using
message queuing. I have created a threadpool of 5 threads and each thread
checks the queue, receives the message and sends an email. How do I
determine the right amount of threads to create?
Thanks,
S