using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Globalization; using System.Text.RegularExpressions; /// <summary> /// Summary description for RegexUtilities /// </summary> public class RegexUtilities { bool InValid = false; public bool IsValidEmail(string strIn) { InValid = false; if (String.IsNullOrEmpty(strIn)) return false; // Use IdnMapping class to convert Unicode domain names. strIn = Regex.Replace(strIn, @"(@)(.+)$", this.DomainMapper); if (InValid) return false; // Return true if strIn is in valid e-mail format. return Regex.IsMatch(strIn, @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" + @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$", RegexOptions.IgnoreCase); } private string DomainMapper(Match match) { // IdnMapping class with default property values. IdnMapping idn = new IdnMapping(); string domainName = match.Groups[2].Value; try { domainName = idn.GetAscii(domainName); } catch (ArgumentException) { InValid = true; } return match.Groups[1].Value + domainName; } } private void GetSendEmInfo() { #region For Get All Type Email Informations..!! IPL.DoId = ddlName.SelectedValue; DataTable dt = IdBL.GetEmailS(IPL); if (dt.Rows.Count > 0) { hid_MailId.Value = dt.Rows[0]["MailId"].ToString(); hid_UsedPName.Value = dt.Rows[0]["UName"].ToString(); hid_EmailSubject.Value = dt.Rows[0]["EmailSubject"].ToString(); hid_EmailBody.Value = dt.Rows[0]["EmailBody"].ToString(); hid_EmailIdName.Value = dt.Rows[0]["EmailIdName"].ToString(); hid_EmPass.Value = dt.Rows[0]["EPass"].ToString(); hid_SeName.Value = dt.Rows[0]["SenName"].ToString(); hid_TNo.Value = dt.Rows[0]["TeNo"].ToString(); hid_EmaLimit.Value = dt.Rows[0]["EmailLimit"].ToString(); hidlink.Value = dt.Rows[0][link"].ToString(); } #endregion #region For Set Some Local Variables..!! int StartLmt, FinalLmt, SendCurrentMail; StartLmt = FinalLmt = SendCurrentMail = 0; bool Valid_LimitMail; Valid_LimitMail = true; /**For Get Finalize Limit For Send Mail**/ FinalLmt = Convert.ToInt32(hid_EmailmaxLimit.Value); #region For Check Email Valid Limits..!! if (FinalLmt > 0) { Valid_LimitMail = true; } else { Valid_LimitMail = false; } #endregion /**For Get Finalize Limit For Send Mail**/ #endregion if (Valid_LimitMail == true) { #region For Send Current Email Status..!! bool EmaiValid; string CreateFileName; string retmailflg = null; EmaiValid = false; #endregion #region For Set Start Limit And FinalLimit Send No Of Email..!! mPL.SendDate = DateTime.Now.ToString("dd-MMM-yyyy"); DataTable dtsendEmail = m1BL.GetEmailSendLog(mPL); if (dtsendEmail.Rows.Count > 0) { StartLmt = Convert.ToInt32(dtsendEmail.Rows[0]["SendNo_Of_Email"].ToString()); } else { StartLmt = 0; } #endregion #region For Find Grid View Controls..!! for (int i = 0; i < GrdEm.Rows.Count; i++) { #region For Find Grid view Controls..!! CheckBox Chk_SelectOne = (CheckBox)GrdEmp.Rows[i].FindControl("chkSingle"); Label lbl_No = (Label)GrdEmAtt.Rows[i].FindControl("lblGrdCode"); lblCode.Value = lbl_InNo.Text; Label lbl_EmailId = (Label)GrdEomAtt.Rows[i].FindControl("lblGrdEmpEmail"); #endregion /**Region For If Check Box Checked Then**/ if (Chk_SelectOne.Checked == true) { if (!string.IsNullOrEmpty(lbl_EmailId.Text)) { #region For When Check Box Checked..!! /**If Start Limit Less Or Equal To Then Condition Performs**/ if (StartLmt < FinalLmt) { StartLmt = StartLmt + 1; } else { Valid_LimitMail = false; EmaiValid = false; } /**End Region**/ string[] SplitClients_Email = lbl_EmailId.Text.Split(','); string Send_Email, Hold_Email; Send_Email = Hold_Email = ""; int CountEmail;/**Region For Count Total Email**/ CountEmail = 0;/**First Time Email Counts Zero**/ Hold_Email = SplitClients_Email[0].ToString().Trim().TrimEnd().TrimStart().ToString(); /**Region For If Clients Have One Email**/ #region For First Emails Send On Client..!! if (SplitClients_Email[0].ToString() != "") { if (EmailRegex.IsValidEmail(Hold_Email)) { Send_Email = Hold_Email; CountEmail = 1; EmaiValid = true; } else { EmaiValid = false; } } #endregion /**Region For If Clients Have One Email**/ /**Region For If Clients Have Two Email**/ /**Region For If Clients Have Two Email**/ if (EmaiValid == true) { #region For Create Email Body And Create File Name..!! //fofile = Server.MapPath("PDFs"); fofile = Server.MapPath("~/vvv/vvvv/") + "/"; CreateFileName = lbl_INo.Text.ToString() + "_1" + ".Pdf";/**Create File Name**/ string[] orimail = Send_Email.Split(','); string Billbody, TempInvoiceId; // DateTime dtLstdate = new DateTime(Convert.ToInt32(txtYear.Text), Convert.ToInt32(ddlMonth.SelectedValue), 16); // DateTime IndtLmt = dtLstdate.AddMonths(1); TempInvoiceId = ""; //byte[] Buffer = Encrypt.Encryptiondata(lbl_InvoiceNo.Text.ToString()); //TempInvoiceId = Convert.ToBase64String(Buffer); #region Create Encrypted Path byte[] EncCode = Encrypt.Encryptiondata(lbl_INo.Text); hidEncrypteCode.Value = Convert.ToBase64String(EncECode); #endregion //#region Create Email Body !! //body = hid_EmailBody.Value.Replace("@greeting", lbl_CoName.Text).Replace("@free", hid_ToNo.Value).Replace("@llnk", "<a style='font-family: Tahoma; font-size: 10pt; color: #800000; font-weight: bold' href='http://1ccccc/ccc/ccc/ccc.aspx?EC=" + hidEncryptedCode.Value+ "' > C cccccccc </a>"); body = hid_EmailBody.Value.Replace("@greeting", "Hii").Replace("@No", hid_No.Value);/*For Mail*/ //#endregion #region For Email Sender Informations..!! for (int j = 0; j < CountEmail; j++) { //if (File.Exists(fofile + "\\" + CreateFileName)) //{ #region lbl_EmailId.Text = orimail[j]; retmailflg = ""; /**Region For Send Email For Clients**/ //retmailflg = SendPreMail("Wp From " + lbl_CName.Text + "", body, lbl_EmailId.Text, lbl_IeNo.Text, hid_EmailIdName.Value, hid_EmailPassword.Value); retmailflg = SendPreMail(hid_EmailSubject.Value, Body, lbl_EmailId.Text, lbl_No.Text, hid_EmailIdName.Value, hid_EmailPassword.Value); /**End Region**/ /**Region For Create Send Email Log When Email Send Successfully**/ if (retmailflg == "True") { SendCurrentMail = Convert.ToInt32(SendCurrentMail) + 1; StartLmt = Convert.ToInt32(StartLmt) + 1; if (SendCurrentMail > 0) { CreateEmailLog(lbl_InNo.Text, StartLmt, hid_EmailIdName.Value, lbl_EmailId.Text); } } /**End Region**/ #endregion //} } #endregion } #endregion } } /**End Region**/ } #endregion } } private void CreateEmailLog(string UniqueId, int StartLmt, string FromEmailId, string TotxtEmailId) { FPL.EmailId_From = FromEmailId; FPL.To_EmailId = TotxtEmailId; FPL.SendDate = DateTime.Now.ToString("dd-MMM-yyyy"); FPL.EmailUniqueId = UniqueId; FPL.SendNo_Of_Email = StartLmt.ToString(); FPL.LoginUserId = Session["LoginUserId"].ToString(); int i = FBL.InsertEmaDoc(FPL); } public string SendPreMail(string emsub, string embody, string EmailId, string FileId, string EmailFromId, string Password) { string retval = "False"; try { string emailBody, emailSubject, emailToList, emailFrom, accountPassword, smtpServer; bool enableSSL; int port; emailBody = embody; emailSubject = emsub; emailToList = EmailId; emailFrom = EmailFromId; accountPassword = Password; smtpServer = "smtp.gmail.com"; enableSSL = true; port = 587; string crefilename; string fofile; fofile = Server.MapPath("PDF"); crefilename = FileId + ".Pdf"; string[] att = { crefilename }; string retemail, insertqry; retemail = ""; retemail = SendEmail(emailBody, emailSubject, emailFrom, emailToList, att, smtpServer, enableSSL, accountPassword, port); if (retemail == "True") { retval = retemail; } } catch { retval = "False"; } finally { } return retval; } public string SendEmail(string emailBody, string emailSubject, string emailFrom, string emailToList, string[] attachedFiles, string smtpIPAddress, bool enableSSL, string accountPassword, int port) { MailMessage mail = new MailMessage(); string retflg; retflg = "False"; try { mail.From = new MailAddress(emailFrom); if (emailToList.Contains(";")) { emailToList = emailToList.Replace(";", ","); } mail.To.Add(emailToList); mail.Subject = emailSubject; mail.IsBodyHtml = true; mail.Body = emailBody; SmtpClient smtp = new SmtpClient(); smtp.Host = "smtp.gmail.com"; smtp.EnableSsl = true; NetworkCredential NetworkCred = new NetworkCredential(emailFrom, accountPassword); smtp.UseDefaultCredentials = true; smtp.Credentials = NetworkCred; smtp.Port = 587; smtp.Send(mail); retflg = "True"; } catch { retflg = "False"; } finally { mail.Dispose(); } return retflg; }