EDIT: this is a winform application, sorry for the inconvenience
Disclaimer: This is the assignment we received in college, and I am fixated on this particular section of code.
I have 2 solutions in Visual Studio 2008, one for the form and one for the DLL that the form can use for functionality. The idea is to send HTML emails from the client and use a delegate to confirm this.
One DLL class contains only one delegate:
namespace Console.Grand
{
public delegate void ObserverDelegate(string info);
}
In the file Delegate.cs
In the form, I have the following method that I will use for the delegate:
private void Update(string info)
{
this.logBox.Text += Environment.NewLine + info;
}
The variable logBox is a TextArea in the form.
When transferring, the following happens (BL stands for "Business Layer"):
BL_MailOut bm = new BL_MailOut(s1,ListViewAdresses());
ObserverDelegate deleg = new ObserverDelegate(Update);
bm.SendMail(deleg);
The constructor of BL_MailOut looks like this (we are now in the DLL):
public BL_MailOut(StandardPage page, List<MailAddress> list)
{
this.s = page;
this.adresslist = new List<MailAddress>();
foreach (MailAddress m in list)
{
this.adresslist.Add(m);
}
}
And the SendMail method:
public void SendMail(ObserverDelegate deleg)
{
IO_MailOut im = new IO_MailOut(s, adresslist, deleg);
Thread t = new Thread(new ThreadStart(im.Send));
t.Start();
}
, , Send():
public void Send()
{
SmtpClient sc;
MailMessage msg;
string info;
foreach (MailAddress adress in this.list)
{
try
{
sc = new SmtpClient(HOST);
msg = new MailMessage(SENDER, adress.Address);
msg.IsBodyHtml = true;
msg.Subject = "test";
msg.Body = page.ToString();
sc.Send(msg);
info = "(" + DateTime.Now + ") MAIL SENT TO" + Environment.NewLine + adress.Address;
deleg(info);
}
}
, , .
deleg(info);, , textBox . . . MSDN , .