
Introduction
This is a windows control, by which you can send an Email. Just drag and drop it onto your form, and call the properties to get fields values.
I use the class Mail which is existed in Net namespace. You can send anonymous Email with virtual Email account. If you have dialup connection, you don't need to set the SMTP server name, but If you have a proxy, set your proxy IP or name at the server name field.
Let's do it
First, as we see the control here, it has 4 fields and the server one is optional, that's in case you have a dialup connection.
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace MailWinControl
{
public class MailWinControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Label lblFrom;
private System.Windows.Forms.TextBox txtBoxFrom;
private System.Windows.Forms.Label lblTo;
private System.Windows.Forms.Label lblServer;
private System.Windows.Forms.Label lblMessage;
private System.Windows.Forms.TextBox txtTo;
private System.Windows.Forms.TextBox txtServer;
private System.Windows.Forms.TextBox txtMessage;
private System.Windows.Forms.Button btnSend;
private System.ComponentModel.Container components = null;
public event EventHandler OnSend;
public MailWinControl()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
private void InitializeComponent()
{
this.lblFrom = new System.Windows.Forms.Label();
this.txtBoxFrom = new System.Windows.Forms.TextBox();
this.txtTo = new System.Windows.Forms.TextBox();
this.lblTo = new System.Windows.Forms.Label();
this.txtServer = new System.Windows.Forms.TextBox();
this.lblServer = new System.Windows.Forms.Label();
this.txtMessage = new System.Windows.Forms.TextBox();
this.lblMessage = new System.Windows.Forms.Label();
this.btnSend = new System.Windows.Forms.Button();
this.SuspendLayout();
this.lblFrom.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.lblFrom.Location = new System.Drawing.Point(24, 32);
this.lblFrom.Name = "lblFrom";
this.lblFrom.Size = new System.Drawing.Size(40, 23);
this.lblFrom.TabIndex = 0;
this.lblFrom.Text = "From:";
this.txtBoxFrom.Location = new System.Drawing.Point(88, 32);
this.txtBoxFrom.Name = "txtBoxFrom";
this.txtBoxFrom.Size = new System.Drawing.Size(224, 20);
this.txtBoxFrom.TabIndex = 4;
this.txtBoxFrom.Text = "";
this.txtTo.Location = new System.Drawing.Point(88, 64);
this.txtTo.Name = "txtTo";
this.txtTo.Size = new System.Drawing.Size(224, 20);
this.txtTo.TabIndex = 6;
this.txtTo.Text = "";
this.lblTo.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.lblTo.Location = new System.Drawing.Point(24, 64);
this.lblTo.Name = "lblTo";
this.lblTo.Size = new System.Drawing.Size(40, 23);
this.lblTo.TabIndex = 5;
this.lblTo.Text = "To:";
this.txtServer.Location = new System.Drawing.Point(88, 96);
this.txtServer.Name = "txtServer";
this.txtServer.Size = new System.Drawing.Size(224, 20);
this.txtServer.TabIndex = 8;
this.txtServer.Text = "Optional";
this.txtServer.Enter += new System.EventHandler(this.txtServer_Enter);
this.lblServer.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.lblServer.Location = new System.Drawing.Point(24, 96);
this.lblServer.Name = "lblServer";
this.lblServer.Size = new System.Drawing.Size(40, 23);
this.lblServer.TabIndex = 7;
this.lblServer.Text = "Server:";
this.txtMessage.Location = new System.Drawing.Point(88, 128);
this.txtMessage.Multiline = true;
this.txtMessage.Name = "txtMessage";
this.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtMessage.Size = new System.Drawing.Size(224, 96);
this.txtMessage.TabIndex = 10;
this.txtMessage.Text = "";
this.lblMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(178)));
this.lblMessage.Location = new System.Drawing.Point(24, 152);
this.lblMessage.Name = "lblMessage";
this.lblMessage.Size = new System.Drawing.Size(56, 23);
this.lblMessage.TabIndex = 9;
this.lblMessage.Text = "Message:";
this.btnSend.Location = new System.Drawing.Point(128, 240);
this.btnSend.Name = "btnSend";
this.btnSend.TabIndex = 11;
this.btnSend.Text = "Send";
this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
this.Controls.Add(this.btnSend);
this.Controls.Add(this.txtMessage);
this.Controls.Add(this.lblMessage);
this.Controls.Add(this.txtServer);
this.Controls.Add(this.lblServer);
this.Controls.Add(this.txtTo);
this.Controls.Add(this.lblTo);
this.Controls.Add(this.txtBoxFrom);
this.Controls.Add(this.lblFrom);
this.Name = "MailWinControl";
this.Size = new System.Drawing.Size(336, 296);
this.ResumeLayout(false);
}
#endregion
public string FromValue
{
get
{
return txtBoxFrom.Text;
}
}
public string ToValue
{
get
{
return txtTo.Text;
}
}
public string ServerValue
{
get
{
return txtServer.Text;
}
}
public string MessageBody
{
get
{
return txtMessage.Text;
}
}
private void btnSend_Click(object sender, System.EventArgs e)
{
OnSend(sender, e);
}
private void txtServer_Enter(object sender, System.EventArgs e)
{
txtServer.Text = "";
}
}
}
as you see here, I have declared an event to wrap the Send Click button event in the consumer application. Then in the event handler function of Send button, I have called the event. This event will be like the interface to get access the event handler of the button inside the control. You may think about another approach like declaring the variables of controls public. It can works fine but it'll be bad approach where it'll not OO methodology.
Second, after building the control let's make a new windows application to consume our Mail control.
1- Start new windows application. Then go to the controls panel and choose any tab (it's preferred to make a new tab for your controls). Then right click and choose Add/Remove items.
2- The controls dialog will be fired.
3- Browse to the path of your Mail control by pressing browse button.
4- After adding the DLL control, you'll find that your control icon has appeared in the controls dialog.
5- Drag and drop your control onto your windows form:
6- Choose your Mail control, go to the properties panel and choose events. You'll find our public declared event. Choose it and double it click to get its handler function.
7- After getting in the handler function, if you'll not set the server name field, the code would be like that:
private void mailWinControl_OnSend(object sender, System.EventArgs e)
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = mailWinControl.FromValue;
mailMsg.To = mailWinControl.ToValue;
mailMsg.Subject = "Test Mail Sender.";
mailMsg.Body = mailWinControl.MessageBody;
mailMsg.BodyFormat = MailFormat.Html;
SmtpMail.Send(mailMsg);
}
Otherwise, the code would be like that:
private void mailWinControl_OnSend(object sender, System.EventArgs e)
{
MailMessage mailMsg = new MailMessage();
mailMsg.From = mailWinControl.FromValue;
mailMsg.To = mailWinControl.ToValue;
mailMsg.Subject = "Test Mail Sender.";
mailMsg.Body = mailWinControl.MessageBody;
mailMsg.BodyFormat = MailFormat.Html;
SmtpMail.SmtpServer = mailWinControl.ServerValue;
SmtpMail.Send(mailMsg);
}
That's all. Have fun.