Introduction
EasyMail is an easy mailing component to send e-mails using an SMTP client. All that you need to do is make sure that all of the SMTP settings are correctly configured.
This is only an example of how to send emails. To extend this component is very easy so I have left it up to the user to modify and improve it to your needs.
Building the Component DLL
- Create a new "Windows Control Library".
- In the Solution Explorer, delete UserControl1.cs.
- Add a new item to the Control Library.
- Create a new Component class with the name
EasyMail
.
- Replace the newly created
EasyMail
class with the following code:
public partial class EasyMail : Component
{
private string mMailFrom;
private string mMailTo;
private string mMailSubject;
private string mMailBody;
private string mSMTPServer;
private int mSMTPPort;
private string mSMTPUsername;
private string mSMTPPassword;
private bool mSMTPSSL;
private MailMessage MailObject;
public string MailFrom { set { mMailFrom = value; }
get { return mMailFrom; } }
public string MailTo { set { mMailTo = value; }
get { return mMailTo; } }
public string MailSubject
{ set { mMailSubject = value; }
get { return mMailSubject; } }
public string MailBody { set { mMailBody = value; }
get { return mMailBody; } }
public string SMTPServer { set { mSMTPServer = value; }
get { return mSMTPServer; } }
public int SMTPPort { set { mSMTPPort = value; }
get { return mSMTPPort; } }
public string SMTPUsername
{ set { mSMTPUsername = value; }
get { return mSMTPUsername; } }
public string SMTPPassword {
set { mSMTPPassword = value; }
get { return mSMTPPassword; } }
public Boolean SMTPSSL { set { mSMTPSSL = value; }
get { return mSMTPSSL; } }
public Boolean Send()
{
MailMessage Email = new MailMessage();
MailAddress MailFrom =
new MailAddress(mMailFrom,mMailFrom);
Email.From = MailFrom;
Email.To.Add(mMailTo);
Email.Subject = mMailSubject;
Email.Body = mMailBody;
SmtpClient SmtpMail =
new SmtpClient(mSMTPServer,mSMTPPort);
SmtpMail.Credentials =
new NetworkCredential(mSMTPUsername,mSMTPPassword);
SmtpMail.EnableSsl = mSMTPSSL;
Boolean bTemp = true;
try
{
SmtpMail.Send(Email);
return true;
}
catch (SmtpFailedRecipientsException ex)
{
MessageBox.Show("The message was not sent!!!");
bTemp = false;
}
return bTemp;
}
public EasyMail()
{
InitializeComponent();
MailObject = new MailMessage();
mMailFrom = "";
mMailTo = "";
mMailSubject = "";
mMailBody = "";
mSMTPServer = "";
mSMTPPort = 25;
mSMTPUsername = "";
mSMTPPassword = "";
mSMTPSSL = false;
}
public EasyMail(IContainer container)
{
container.Add(this);
InitializeComponent();
}
}
Make sure that all of the following "using
" clauses are at the top of the file, and then build the component.
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
Install the Email Component onto the Toolbox
- Go to the Design view of a form.
- Open the Toolbox.
- Right click on the tab in the Toolbox where you want to add the Email control to (Components in this example).
- Select "Choose items..." from the popup menu.
- The "Choose Toolbox Items" window will open. Click on Browse, and select the email component's DLL file. Click OK to close the Choose Toolbox Item window. The new component will now be in the toolbox.
Using the Component
- Select the EasyMail component and drop it onto your form. An
EasyMail1
object will be created.
- To use it, add the following code to a button event or wherever you want to run the send event from. (Replace the XXXs with the correct info.)
easyMail1.MailFrom = " XXX@XXX.XXX";
easyMail1.MailTo = " XXX@XXX.XXX";
easyMail1.MailSubject = "Subject";
easyMail1.MailBody = "Mail body";
easyMail1.SMTPServer = "smtp.mail.yahoo.com";
easyMail1.SMTPUsername = "XXX";
easyMail1.SMTPPassword = "XXX";
if (easyMail1.Send())
{
MessageBox.Show("Sent...");
}
Sending messages asynchronously or synchronously
Sending messages synchronously is not always desired. It could cause your user interface to freeze or hang. To solve this, we have a separate function in the SMTPClient
class called sendasync
. This allows messages to be sent in their own thread.
To add this capability, we need to add a new boolean property to enable/disable this option. We also need to add the code to execute the right function depending on the value of the new property.
New property:
private bool mSendAsync;
public bool SendAsync { set { mSendAsync = value; }
get { return mSendAsync; } }
Code that will react upon the new property. This will be added to the Send
function.
string sTemp = "";
if (mSendAsync)
SmtpMail.SendAsync(Email,sTemp);
else
SmtpMail.Send(Email);
Better Exception Handling
Mail services may not always be available. So we will have to add a few exception handlers to catch unexpected events. To do this, I will test if a message was sent. If it was not sent, I will wait a few seconds before I try to resend it again.
I created two new properties: TryAgianOnFailure
and TryAgainDelayTime
.
private bool mTryAgianOnFailure;
private int mTryAgainDelayTime;
public bool TryAgianOnFailure
{ set { mTryAgianOnFailure = value; }
get { return mTryAgianOnFailure; } }
public int TryAgainDelayTime
{ set { mTryAgainDelayTime = value; }
get { return mTryAgainDelayTime; }
Here is the code to make it work... this will be added to the Send
function.
catch (SmtpFailedRecipientsException e)
{
for (int k=0;k < e.InnerExceptions.Length;k++)
{
bTemp = false;
SmtpStatusCode StatusCode =
e.InnerExceptions[k].StatusCode;
if (StatusCode==SmtpStatusCode.MailboxUnavailable||
StatusCode==SmtpStatusCode.MailboxBusy)
{
try
{
if (mTryAgianOnFailure)
{
Thread.Sleep(mTryAgainDelayTime);
string sTemp = "";
if (mSendAsync)
SmtpMail.SendAsync(Email, sTemp);
else
SmtpMail.Send(Email);
bTemp = true;
}
}
catch { bTemp = false}
}
}
}
Conclusion
This is only a simple example of how to make things easy in Visual Studio. I like components very much because everything is packaged together. If any thing should go wrong, you directly know where to find the problem. Creating controls is also a good way to improve your object orientation skills. I hope this has helped you in some way.
For more info, please go to my website... www.ChatBert.com.
History
Update(2008/04/22)
I added a string[]
property (MailAttachments
) that will store a list of attachment file paths.
Example:
easyMail1.MailAttachments = new string[2];
easyMail1.MailAttachments[0] = "c:\\temp.txt";
easyMail1.MailAttachments[1] = "c:\\log.txt";