public MailSendQueueAgent()
{
this.ServiceName = "Bulk Email Sending Agent V1.0";
this.EventLog.Log = "Application";
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
try
{
Thread T = new Thread(Execute);
T.Start();
}
catch (Exception Ex)
{
EventLog.WriteEntry("Bulk Email Sending Agent V1.0",
"Startup:Failed," + Ex.Message.ToString(),
EventLogEntryType.Warning, 1003);
writeLog(Ex.Message.ToString());
}
}
private void Execute()
{
DateTime sCurrentTime = Convert.ToDateTime("10:00:00 AM");
DateTime eCurrentTime = Convert.ToDateTime(sCurrentTime.AddHours(10D));
writeLog("Executing the Mail Sending service --- " + DateTime.Now);
try
{
while (true)
{
DateTime systemStartTime = DateTime.Now;
if (systemStartTime >= sCurrentTime && systemStartTime <= eCurrentTime)
{
ConnectionlHelper cnn = new ConnectionlHelper();
cnn.OpenConn();
try
{
SqlCommand cd = new SqlCommand("sp_FetchSettings", cnn.oConnection);
cd.ExecuteNonQuery();
DataTable dts = new DataTable();
SqlDataAdapter dp = new SqlDataAdapter(cd);
dp.Fill(dts);
if (dts.Rows.Count > 0)
{
writeLog("Read Settings Successfull.....");
ConnectionlHelper conn = new ConnectionlHelper();
conn.OpenConn();
try
{
SqlCommand cmd = new SqlCommand("sp_SelectMailFromQueue", conn.oConnection);
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
if (dt.Rows.Count > 0)
{
writeLog("New Mail to Sent at: " + DateTime.Now);
for (int i = 0; i < dt.Rows.Count; i++)
{
try
{
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient(
Convert.ToString(dts.Rows[0]["SmtpServer"]));
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.From = new MailAddress(
Convert.ToString(dts.Rows[0]["frm"]));
mail.Bcc.Add(dt.Rows[i]["Email"].ToString());
mail.Subject = dt.Rows[i]["MsgSubject"].ToString();
mail.Body = dt.Rows[i]["MsgBody"].ToString();
mail.IsBodyHtml = true;
smtp.Port = Convert.ToInt32(dts.Rows[0]["Port"]);
smtp.Credentials =
new System.Net.NetworkCredential(
Convert.ToString(dts.Rows[0]["DisplayName"]),
Convert.ToString(dts.Rows[0]["Password"]));
smtp.Send(mail);
writeLog("Sending Mail count: " + i);
UpdateMailStatus(Convert.ToInt32(dt.Rows[i]["MID"]));
UpdateScheduleDetails(Convert.ToInt32(dt.Rows[i]["ScID"]));
Thread.Sleep(5000);
}
catch (Exception Ex)
{
EventLog.WriteEntry(
Ex.StackTrace.ToString(),
EventLogEntryType.Error, 101);
writeLog("Mail not sent " + Ex.Message);
}
}
}
dt.Clear();
}
catch (Exception ex)
{
EventLog.WriteEntry(
"Error Connecting DB in con " + ex.Message.ToString());
writeLog("Error Connecting Database in con: " + ex.Message.ToString());
}
finally
{
conn.Close();
}
}
dts.Clear();
}
catch (Exception ex)
{
EventLog.WriteEntry(
"Error Connecting DB in cnn " + ex.Message.ToString());
writeLog("Error Connecting Database in cnn: " + ex.Message.ToString());
}
finally
{
cnn.Close();
}
}
}
}
catch (Exception ex)
{
EventLog.WriteEntry(
"Error Connecting DB Before While->" + ex.Message.ToString());
writeLog("Error Connecting Database: " + ex.Message.ToString());
}
}
Translate
Migrated from the original suvoBGD WordPress archive. View the original source.
legacy-blogwordpress-archiveSystem Design
