This is the code I am using for sending single e-mail: I am using the dll version 4.1.2.0
DnsMailClient dns = new DnsMailClient();
MailMessage msg = CreateMessage();
SendMX(msg, dns);
private MailMessage CreateMessage()
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress(“sajeesh@yahoo.co.in”);
msg.To.Add("sajeesh007s@gmail.com
");
msg.Subject = “test mail”;
msg.HtmlBody = General.MsgHtmlBody ; /// passing variable
msg.Date = DateTime.Now;
return msg;
}
private void SendMX(MailMessage msg, DnsMailClient dnsclient)
{
try
{
LoadInfo LI = new LoadInfo();
if (InvokeRequired)
{
delMX d = new delMX(SendMX); //delegate
this.BeginInvoke(d, new object[] { msg, dnsclient });
// MessageBox.Show(msg.ToString());
}
else
{
if (General.deliveryLog)
{
LI.WriteToLog(“Sending to : " + msg.To[0].Address);
}
IAsyncResult irs = dnsclient.BeginSend(msg, null, null);
// MessageBox.Show(msg.To.ToString());
Application.DoEvents();
// dnsclient.EndSend(irs);
// Thread.Sleep(1000);
//if (!irs.IsCompleted)
//{
// failedMessages.Add(msg);
//}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private delegate void delMX(MailMessage mm, DnsMailClient d1);
This is the code that i am using for sending bulk emails:
MailMessage template = CreateMessageSend();
TemplateEngine engine = new TemplateEngine(template);
InitializeDt();
MailMessageCollection messages = engine.Instantiate(dt);
SendMX(messages, messages);
private MailMessage CreateMessageSend()
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("sajeesh@yahoo.com
”);
msg.ReplyTo = new MailAddress("sajeesh@yahoo.com
");
msg.To.Add(new MailAddress("#EmailAddress#", “#DisplayName#”, true));
msg.Subject = “Bulk sending”;
msg.HtmlBody = General.MsgHtmlBody;
msg.Date = DateTime.Now;
return msg;
}
private void InitializeDt()
{
try
{
string frmName = " ";
dt = new DataTable();
this.dt.Columns.Add(“EmailAddress”, typeof(string));
this.dt.Columns.Add(“DisplayName”, typeof(string));
this.dt.Columns.Add(“FirstName”, typeof(string));
this.dt.Columns.Add(“LastName”, typeof(string));
this.dt.Columns.Add(“Company”, typeof(string));
this.dt.Columns.Add(“FromName”, typeof(string));
this.dt.Columns.Add(“FromEmailAddress”, typeof(string));
this.dt.Columns.Add(“Date”, typeof(string));
this.dt.Columns.Add(“Time”, typeof(string));
if (General.FromName != null)
{
frmName = General.FromName;
}
if (General.FromCompany == null)
{
General.FromCompany = " ";
}
this.dt.Rows.Add(new object[] {General.FromEI, frmName,frmName,frmName,General.FromCompany,
General.FromEI,DateTime.Now.Date.ToShortDateString(),DateTime.Now.ToShortTimeString()});
foreach (EmailList EL in General.MailList) // for bulk email id’s
{
if (EL.Subscribe)
this.dt.Rows.Add(new object[] { EL.EmailId,EL.DisplayName,
EL.FirstName,EL.LastName,EL.Company,frmName,EL.EmailId,
DateTime.Now.Date.ToShortDateString(),DateTime.Now.ToShortTimeString()});
}
private void SendMX(MailMessageCollection messages, MailMessageCollection RemMessages)
{
failedMessages = new MailMessageCollection();
try
{
dnsclient = new DnsMailClient();
for (int i = intVal; i < messages.Count; i++)
{
if (!MXSender.Paused)
{
intVal = i;
SendMX(messages[i], dnsclient);
}
}
if (failedMessages.Count > 1)
{
MessageBox.Show(“No of messages failed :” + failedMessages.Count.ToString());
}
}
catch
{
MessageBox.Show(“error”);
}
}
private void SendMX(MailMessage msg, DnsMailClient dnsclient)
{
try
{
LoadInfo LI = new LoadInfo();
if (InvokeRequired)
{
delMX d = new delMX(SendMX);
this.BeginInvoke(d, new object[] { msg, dnsclient });
}
else
{
if (General.deliveryLog)
{
LI.WriteToLog("Sending to : " + msg.To[0].Address);
}
IAsyncResult irs = dnsclient.BeginSend(msg, null, null);
Application.DoEvents();
// dnsclient.EndSend(irs);
// Thread.Sleep(1000);
//if (!irs.IsCompleted)
//{
// failedMessages.Add(msg);
//}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}