using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Web.Mail; using System.Net.Mail; using System.Text; public partial class Bulkmail_SendTextMailFinal : System.Web.UI.Page { static DataTable dt = new DataTable(); static int NoOfMail; protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) { if (Session["Bulkmail"] == null) { Response.Redirect("BulkmailLogin.aspx"); } try { ModCommon.Fill_Combo("Select Category_KId, Category_Name from BEM_Category", ddlCategory); ModCommon.Fill_Combo("Select Server_Kid,Server_ServerName from BEM_Server",ddlServer); //NoOfMail = count(); lblCount.Text = ""; btnStop.Visible = false; lblTotal.Text = ""; dt = null; Timer1.Enabled = false; Image1.Visible = false; NoOfMail = 0; } catch { } } } public int count() { try { SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "Select Count(Email) from BEM_Prospect_Detail where Categoryid ='" + ddlCategory.SelectedItem.Value + "'"; cmd.CommandType = CommandType.Text; cmd.Connection = con; if (con.State == ConnectionState.Open) { con.Close(); } else { con.Open(); } return (int)cmd.ExecuteScalar(); } catch { return 0; } } protected void btnSend_Click(object sender, EventArgs e) { try { dt = null; lblCount.Text = ""; lblDear.Text = ""; lblTotal.Text = ""; btnStop.Visible = true; Timer1.Enabled = true; btnStart.Visible = false; Image1.Visible = true; btnReset.Visible = false; } catch { } } protected void btnStop_Click(object sender, EventArgs e) { try { Timer1.Enabled = false; btnStart.Visible = true; btnStop.Visible = false; Image1.Visible = false; btnReset.Visible = true; dt = null; } catch { } } protected void Timer1_Tick(object sender, EventArgs e) { try { if (lblCount.Text == "") { SendMail(0); } else { SendMail(int.Parse(lblCount.Text)); } } catch { } } public void SendMail(int index) { try { if(dt==null) { dt = ModCommon.ReturnDataTable("Select Email,ContactPerson From BEM_Prospect_Detail where Categoryid ='"+ ddlCategory.SelectedItem.Value + "' order by Email "); } //lblDear.Text = Convert.ToString(dt.Rows[index]["ContactPerson"]); if (dt.Rows[index]["ContactPerson"].ToString() == "") { lblDear.Text = " Sir,"; } else { lblDear.Text = dt.Rows[index]["ContactPerson"].ToString()+" ,"; } ////System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage("contact@atcomaart.com", dt.Rows[index]["Email"].ToString()); ////MyMailMessage.Subject = txtSubject.Text; ////MyMailMessage.IsBodyHtml = false; //////if (lblDear.Text == "") //////{ ////// MyMailMessage.Body = "Dear, Sir \n\n\n" + txtMsgBody.Text + "\n\n\n\nRegards," + "\n " + txtSenderName.Text + "\n " + txtSenderMobileNo.Text + "\n " + txtSenderCompany.Text + "\n\n" + txtSenderAddress.Text; //////} //////else //////{ //// MyMailMessage.Body = "Dear, " + lblDear.Text + "\n\n\n" + txtMsgBody.Text + "\n\n\n\nRegards," + "\n " + txtSenderName.Text + "\n " + txtSenderMobileNo.Text + "\n " + txtSenderCompany.Text + "\n\n" + txtSenderAddress.Text; //////} ////SmtpClient sc = new SmtpClient("smtp.rediffmailpro.com"); ////sc.Credentials = new System.Net.NetworkCredential("contact@atcomaart.com", "passw123"); System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage(ModCommon.ReturnValue("Select Server_UserName from BEM_Server where Server_Kid='"+ ddlServer.SelectedItem.Value.ToString()+"'"), dt.Rows[index]["Email"].ToString()); MyMailMessage.Subject = txtSubject.Text; MyMailMessage.IsBodyHtml = false; //if (lblDear.Text == "") //{ // MyMailMessage.Body = "Dear, Sir \n\n\n" + txtMsgBody.Text + "\n\n\n\nRegards," + "\n " + txtSenderName.Text + "\n " + txtSenderMobileNo.Text + "\n " + txtSenderCompany.Text + "\n\n" + txtSenderAddress.Text; //} //else //{ MyMailMessage.Body = "Dear " + lblDear.Text + "\n\n\n" + txtMsgBody.Text + "\n\n\n\nRegards," + "\n " + txtSenderName.Text + "\n " + txtSenderMobileNo.Text + "\n " + txtSenderCompany.Text + "\n\n" + txtSenderAddress.Text; //} SmtpClient sc = new SmtpClient(); sc.Host = ModCommon.ReturnValue("Select Server_Name from BEM_Server where Server_Kid='" + ddlServer.SelectedItem.Value.ToString() + "'"); sc.UseDefaultCredentials = false; sc.EnableSsl = Convert.ToBoolean(ModCommon.ReturnValue("Select Server_SSL from BEM_Server where Server_Kid='"+ddlServer.SelectedItem.Value.ToString()+"'")); sc.Credentials = new System.Net.NetworkCredential(ModCommon.ReturnValue("Select Server_UserName from BEM_Server where Server_Kid='" + ddlServer.SelectedItem.Value.ToString() + "'"), ModCommon.ReturnValue("Select Server_Password from BEM_Server where Server_Kid='" + ddlServer.SelectedItem.Value.ToString() + "'")); //sc.Port = 465; // sc.Host = "smtp.gmail.com"; //sc.Timeout = 9000; sc.Send(MyMailMessage); int j; if (lblCount.Text == "") { lblCount.Text = "1"; if (count() == 1) { Timer1.Enabled = false; btnReset.Visible = true; btnStart.Visible = true; btnStop.Visible = false; Image1.Visible = false; } } else { j = int.Parse(lblCount.Text); //while (i < count()) //{ //} if (j < count() - 1) { j = j + 1; lblCount.Text = j.ToString(); } else { j = j + 1; lblCount.Text = j.ToString(); Timer1.Enabled = false; btnReset.Visible = true; btnStart.Visible = true; btnStop.Visible = false; Image1.Visible = false; return; } } lblTotal.Text = "Out Of " + NoOfMail + " Mail Sent"; } catch (System.Exception e) { lblDear.Text = e.Message; int j; j=(int.Parse(lblCount.Text) + 1); lblCount.Text = Convert.ToString(j); } } protected void btnReset_Click(object sender, EventArgs e) { try { ddlCategory.ClearSelection(); ddlServer.ClearSelection(); // ddlTemplate.ClearSelection(); txtSubject.Text = ""; lblCount.Text = ""; //Literal1.Text = ""; txtMsgBody.Text = ""; txtSenderAddress.Text = ""; txtSenderMobileNo.Text = ""; txtSubject.Text = ""; btnStop.Visible = false; btnStart.Visible = true; lblTotal.Text = ""; lblDear.Text = ""; Image1.Visible = false; } catch { } } protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e) { try { NoOfMail = count(); } catch { } } protected void ddlServer_SelectedIndexChanged(object sender, EventArgs e) { if (ModCommon.ReturnValue("Select Server_Name from BEM_Server where Server_Kid='" + ddlServer.SelectedItem.Value + "'") == "smtp.gmail.com") { Timer1.Interval = 15000; } else { Timer1.Interval = 7000; } } }