/// <summary> /// send mail messages /// </summary> /// <param name="campaigns"></param> public void SendMessage(Campaigns campaigns) { SendGridMailer obj = new SendGridMailer(); MailComposer composer = new MailComposer(); try { UpdateExecutedDate(campaigns.IdentifierCampaign); campaign = new CampaignDetails(); subs = new List <SubscriberDetails>(); campaign = getCampaignDetails(campaigns.CampaignID); subs = new List <SubscriberDetails>(); subs = getSubscriberDetails(campaigns.IdentifierCampaign); string content = null; foreach (var email in subs) { content = composer.ComposeHTMLMail(campaign.EmailContent, email.FirstName + " " + email.LastName, email.IdentifierSubscriber); obj.SendHtmlMail(email.EmailAddress, email.FirstName + " " + email.LastName, campaign.FromEmail, campaign.FromName, campaign.EmailSubject, content); } UpdateMailStatus(campaigns.IdentifierCampaign); removeCampaign(campaigns.IdentifierCampaign); } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } }
/// <summary> /// check campaigns available in queue and get all if available /// </summary> /// <returns></returns> private DataSet campaignChecker() { System.Data.DataSet ds = new System.Data.DataSet(); try { using (SqlConnection con = new SqlConnection(dbConnection)) { using (SqlCommand cmd = new SqlCommand("usp_getcampaignfromqueue", con)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(ds); return(ds); } } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } return(ds); }
/// <summary> /// Get Campaign Details /// </summary> /// <param name="campaignId"></param> /// <returns></returns> private CampaignDetails getCampaignDetails(int campaignId) { try { using (SqlConnection con = DataHelper.CreateOpenConnection()) { DataSet ds = new DataSet(); campaign = new CampaignDetails(); ds = DataHelper.ExecuteDataSet(con, CommandType.StoredProcedure, "usp_getemailcontent", new SqlParameter[] { new SqlParameter("@campaignId", campaignId) }); if (ds.Tables[0].Rows.Count > 0) { foreach (DataRow row in ds.Tables[0].Rows) { campaign.Cid = (int)row["Cid"]; campaign.Name = row["Name"].ToString(); campaign.EmailContent = row["EmailContent"].ToString(); campaign.EmailSubject = row["EmailSubject"].ToString(); campaign.FromEmail = row["FromEmail"].ToString(); campaign.ListId = (int)row["ListId"]; campaign.FromName = row["FromName"].ToString(); campaign.CTypeId = (int)row["CTypeId"]; if (row["ScheduleCampaign"].ToString() == "") { var MyReader = new AppSettingsReader(); campaign.ScheduleCampaign = Convert.ToInt32(MyReader.GetValue("timeinterval", typeof(string))); } else { campaign.ScheduleCampaign = (int)row["ScheduleCampaign"]; } if (row["noOfEmailSendPerInterval"].ToString() == "") { var MyReader = new AppSettingsReader(); campaign.noOfEmailSendPerInterval = Convert.ToInt32(MyReader.GetValue("NoOfSubscriber", typeof(string))); } else { campaign.noOfEmailSendPerInterval = (int)row["noOfEmailSendPerInterval"]; } } } } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } return(campaign); }
/// <summary> /// Check for campaigns and start them /// </summary> private void InitiateCampaign() { CancellationToken cancellation = cts.Token; TimeSpan interval = TimeSpan.Zero; CampaignManager camp = new CampaignManager(); while (!cancellation.WaitHandle.WaitOne(interval)) { try { if (camp.CheckCampaignAvailability()) { camp.StartCampaign(); using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "LogsForService.txt", FileMode.Append, FileAccess.Write)) using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("Success Campaign Successfuly Send : " + DateTime.Now); } interval = WaitAfterSuccessInterval; } else { interval = WaitForCampaign; } if (cancellation.IsCancellationRequested) { Trace.Write("Service Cancelled : " + DateTime.Now); OnStop(); } } catch (Exception caught) { // Log the exception. EMTException ex = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, caught.Message, caught.StackTrace, EMTException.ErorrTypes.SMTPExceptions.ToString()); ex.LogException(); interval = WaitAfterErrorInterval; Trace.Write("Service Failure : " + DateTime.Now); Trace.Write("Failure Reason : " + caught.InnerException.Message); using (FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "LogsForService.txt", FileMode.Append, FileAccess.Write)) using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine("Service Failure : " + DateTime.Now); sw.WriteLine("Failure Reason : " + caught.Message); } } } }
/// <summary> /// Get Subscriber Details /// </summary> /// <param name="campaignId"></param> /// <returns></returns> private List <SubscriberDetails> getSubscriberDetails(string cidef) { subs = new List <SubscriberDetails>(); try { using (SqlConnection con = DataHelper.CreateOpenConnection()) { using (SqlCommand cmd = new SqlCommand("usp_getsubscribersdetails", con)) { System.Data.DataSet ds = new System.Data.DataSet(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("@campaignIdentifier", SqlDbType.NVarChar).Value = cidef; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(ds); //rows = cmd.ExecuteNonQuery(); if (ds.Tables[0].Rows.Count > 0) { foreach (DataRow row in ds.Tables[0].Rows) { sub = new SubscriberDetails(); sub.SubscriberID = (int)row["SubscriberID"]; sub.FirstName = row["FirstName"].ToString(); sub.LastName = row["LastName"].ToString(); sub.EmailAddress = row["EmailAddress"].ToString(); sub.IdentifierSubscriber = row["IdentifierSubscriber"].ToString(); sub.IdentifierCampaign = row["IdentifierCampaign"].ToString(); sub.userID = row["UserID"].ToString(); subs.Add(sub); } return(subs); } } } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } return(subs); }
public SendGridMailer() { try { AppSettingsReader MyReader = new AppSettingsReader(); Key = MyReader.GetValue("SendGridKey", typeof(string)).ToString(); transportWeb = new Web(Key); } catch (SmtpException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SMTPExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SMTPExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } }
/// <summary> /// updates the exceution date for campaign in m_tracking table /// </summary> /// <param name="campaignIdentifier"></param> private void UpdateExecutedDate(string campaignIdentifier) { try { using (SqlConnection con = DataHelper.CreateOpenConnection()) { DataHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "usp_updateexecutedateforcampaign", new SqlParameter[] { new SqlParameter("@campaignIdentifier", campaignIdentifier) }); } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } }
/// <summary> /// remove campaign from queue after completion /// </summary> /// <param name="identifier"></param> public void removeCampaign(string identifier) { try { using (SqlConnection con = DataHelper.CreateOpenConnection()) { DataHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "usp_removecampaignfromque", new SqlParameter[] { new SqlParameter("@campaignidentifier", identifier) }); } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } }
/// <summary> /// before recording the campaign check if he has opened the campaign. /// </summary> /// <param name="cId"></param> /// <param name="sid"></param> /// <returns></returns> public bool isUserAvailable(string SIdentifier) { using (SqlConnection con = new SqlConnection(dbConnectionString)) { try { using (SqlCommand cmd = new SqlCommand("usp_tracking_checkIsmailOpend", con)) { System.Data.DataSet ds = new System.Data.DataSet(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("@SIdentifier", System.Data.SqlDbType.NVarChar).Value = SIdentifier; //cmd.Parameters.Add("@SubsciberId", System.Data.SqlDbType.Int).Value = sid; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(ds); //rows = cmd.ExecuteNonQuery(); return(Convert.ToBoolean(ds.Tables[0].Rows[0]["IsOpened"])); //if (ds.Tables[0].Rows.Count > 0) //{ // return false; //} //else //{ // return true; //} } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); throw; } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); throw; } } }
/// <summary> /// get the logo of the user. /// /// </summary> /// <param name="campaignID"></param> /// <returns></returns> public string GetLogo(string subscriberidentifier) { try { using (SqlConnection con = new SqlConnection(dbConnectionString)) { using (SqlCommand cmd = new SqlCommand("usp_tracking_getcompanylogo", con)) { System.Data.DataSet ds = new System.Data.DataSet(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("@subscriberidentifier", System.Data.SqlDbType.NVarChar).Value = subscriberidentifier; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { return(ds.Tables[0].Rows[0][0].ToString()); } else { return("row_Logo.png"); } } } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); throw; } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); throw; } }
public List <string> getSubscribersIdentifier(string campaignidentifier) { sidentifiers = new List <string>(); try { using (SqlConnection con = DataHelper.CreateOpenConnection()) { using (SqlCommand cmd = new SqlCommand("usp_getsubscriberidentifier", con)) { System.Data.DataSet ds = new System.Data.DataSet(); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("@campaignidentifier", System.Data.SqlDbType.NVarChar).Value = campaignidentifier; SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cmd; da.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { foreach (DataRow row in ds.Tables[0].Rows) { sidentifiers.Add(row["IdentifierSubscriber"].ToString()); } } } } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } return(sidentifiers); }
/// <summary> /// changes status for subscriber to sent after completion of campaign /// </summary> /// <param name="identifier"></param> public void UpdateMailStatus(string identifier) { try { using (SqlConnection con = DataHelper.CreateOpenConnection()) { //System.Data.DataSet ds = new System.Data.DataSet(); //cmd.CommandType = System.Data.CommandType.StoredProcedure; //cmd.Parameters.Add("@identifier", System.Data.SqlDbType.NVarChar).Value = identifier; DataHelper.ExecuteNonQuery(con, CommandType.StoredProcedure, "usp_changemailstatus", new SqlParameter[] { new SqlParameter("@identifier", identifier) }); } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } }
public void SendPlainTextMail(string ToEmail, string ToName, string FromEmail, string FromName, string EmailSubject, string EmailBody) { var SGMsg = new SendGridMessage(); SGMsg.From = new System.Net.Mail.MailAddress(FromName + "<" + FromEmail + ">"); SGMsg.Subject = EmailSubject; SGMsg.Text = EmailBody; SGMsg.AddTo(ToName + "<" + ToEmail + ">"); try { transportWeb.DeliverAsync(SGMsg); } catch (SmtpException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SMTPExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SMTPExceptions.ToString()); exp.LogException(); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } }
/// <summary> /// start campaign threads /// </summary> public void StartCampaign() { MailUtility obj = new MailUtility(); campaigns = new List <Campaigns>(); campaigns = getCampaigns(); #region ParallelTask try { Parallel.ForEach(campaigns, (camp) => { obj.SendMessage(camp); }); } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); } #endregion }
/// <summary> /// Save the subsbscriber and campaign on the date which he has opened. /// </summary> /// <param name="cId">Camapign </param> /// <param name="sid">Subscriber </param> public void record(string SIdentifier) { try { using (SqlConnection con = new SqlConnection(dbConnectionString)) { //bool = if (isUserAvailable(SIdentifier)) { return; } else { using (SqlCommand cmd = new SqlCommand("usp_tracking_recordmailopend", con)) { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("@SIdentifier", System.Data.SqlDbType.NVarChar).Value = SIdentifier; con.Open(); cmd.ExecuteNonQuery(); } } } } catch (SqlException ex) { exp = new EMTException((int)EMTException.ErorrTypes.SqlExceptions, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); throw; } catch (Exception ex) { exp = new EMTException((int)EMTException.ErorrTypes.others, ex.InnerException.Message, ex.StackTrace, EMTException.ErorrTypes.SqlExceptions.ToString()); exp.LogException(); throw; } }