public class MailHelper
{
public MailHelper()
{
this._AttachementsList = new List<string>();
}
#region Properties
//mail server to control the send action
private string _MailServer;
public string MailServer
{
get { return this._MailServer; }
set { this._MailServer = value; }
}
//addresser
private string _MailFrom;
public string MailFrom
{
get { return this._MailFrom; }
set { this._MailFrom = value; }
}
//subject of mail
private string _MailSubject;
public string MailSubject
{
get { return this._MailSubject; }
set { this._MailSubject = value; }
}
//content text of mail
private string _MailBody;
public string MailBody
{
get { return this._MailBody; }
set { this._MailBody = value; }
}
//attachments
private List<String> _AttachementsList;
public List<String> AttachementsList
{
get { return this._AttachementsList; }
set { this._AttachementsList = value; }
}
//receivers
private List<String> _MailTo;
public List<String> MailTo
{
get { return this._MailTo; }
set { this._MailTo = value; }
}
#endregion
//event open to public, when invoke send action async, it will be fired
public event SendCompletedEventHandler SendCompletedEvent;
/// <summary>
/// send mail in async state, not completed yet
/// </summary>
public void SendMailAsync()
{
//send email to each one in mailto list
if (EnsureMailProperties())
{
using (MailMessage message = new MailMessage(new MailAddress(_MailFrom), new MailAddress(_MailTo[0])))
{
//set common properties for a mail
SmtpClient client = new SmtpClient(MailServer);
message.Subject = _MailSubject;
message.Body = _MailBody;
for (int i = 1; i < _MailTo.Count; i++)
{
message.CC.Add(new MailAddress(_MailTo[i]));
}
/// <summary>
/// Send mail in sync state,this may block the current thread,
/// receivers will receive different emails
/// </summary>
public bool SendMailSeperately()
{
bool flag = true;
//send email to each one in mailto list
if (EnsureMailProperties())
{
foreach (String mailto in _MailTo)
{
using (MailMessage message = new MailMessage(new MailAddress(_MailFrom), new MailAddress(mailto)))
{
//set common properties for a mail
SmtpClient client = new SmtpClient(MailServer);
message.Subject = _MailSubject;
message.Body = _MailBody;
//add attachments
foreach (String attachment in _AttachementsList)
{
message.Attachments.Add(new Attachment(attachment));
}
try
{
client.Send(message);
}
catch (SmtpException)
{
flag = false;
}
}
}
}
else flag = false;
return flag;
}
/// <summary>
/// Send mail in sync state,this may block the current thread
/// receivers will receive the same email, add to cc
/// </summary>
public bool SendMailTogether()
{
bool flag = true;
//send email to each one in mailto list
if (EnsureMailProperties())
{
using (MailMessage message = new MailMessage(new MailAddress(_MailFrom), new MailAddress(_MailTo[0])))
{
//set common properties for a mail
SmtpClient client = new SmtpClient(MailServer);
message.Subject = _MailSubject;
message.Body = _MailBody;
for (int i = 1; i < _MailTo.Count; i++)
{
message.CC.Add(new MailAddress(_MailTo[i]));
}
//add attachments
foreach (String attachment in _AttachementsList)
{
message.Attachments.Add(new Attachment(attachment));
}
client.SendCompleted += new SendCompletedEventHandler(SendCompleteCallBack);
try
{
client.Send(message);
}
catch (SmtpException)
{
flag = false;
}
}
}
else flag = false;
return flag;
}
/// <summary>
/// Ensure the mail server, mailfrom and mailto is not null
/// </summary>
/// <returns></returns>
private bool EnsureMailProperties()
{
if (String.IsNullOrEmpty(MailServer))
return false;
if (String.IsNullOrEmpty(MailFrom))
return false;
if (MailTo == null || MailTo.Count == 0)
return false;
return true;
}
private void SendCompleteCallBack(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
//After send action completed, if event is not null, fire it
if (this.SendCompletedEvent != null)
SendCompletedEvent(sender, e);
}