private void EmailTemplateList() { try { //Retriving The MailTemplateIds which has scheduleNextdate from Today to Lastdate of the month. long[] MailTemplateIds = EC.GetMailTemplatesIdUsingQuery(); if (MailTemplateIds != null) { criteria.Clear(); //Takeing all the MailTemplate for 1 month criteria.Add("MailTemplateId", MailTemplateIds); Dictionary <long, IList <MailConfig> > MailTemplateList = EC.GetMailConfigListWithPagingAndCriteria(0, 9999, string.Empty, string.Empty, criteria); if (MailTemplateList != null && MailTemplateList.First().Key > 0) { IList <MailConfig> MailTempList = MailTemplateList.FirstOrDefault().Value.ToList(); foreach (var MailList in MailTempList) { MailColumn mc = EC.GetMailColumnDetailsById(MailList.MailColumnId); //Report For Attachment byte[] Attachement = GenerateReportUsingQueryString(MailList.QueryString, MailList.DestinationList, MailList.MailTemplateName); string MailBody = GetBodyofReportMail(); //IList < MailActivity > Ma = IList <MailActivity> MailActivityList = (from items in MailList.MailActivityList where items.IsActive == true select items).OrderBy(i => i.ActivityId).Distinct().ToList(); foreach (var MailActiveList in MailActivityList) { //new Task(() => { SendEmailReport(MailList, mc, MailActiveList, MailBody, Attachement); }).Start(); SendEmailReport(MailList, mc, MailActiveList, MailBody, Attachement); } } } } } catch (Exception ex) { throw ex; } }
private void SendEmailReport(MailConfig MailList, MailColumn mc, MailActivity MailActiveList, string MailBody, byte[] Attachment) { try { string[] EmailIds = MailList.EmailList.Split(','); string SendEmail = ConfigurationManager.AppSettings["SendEmailOption"]; string From = ConfigurationManager.AppSettings["From"]; if (SendEmail == "false") { return; } else { try { System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); switch (MailActiveList.MailOn) { case "Weekly": mail.Subject = MailList.ReportName + " For Week" + "(" + DateTime.Now + ")"; break; case "Daily": mail.Subject = MailList.ReportName + " For Today" + "(" + String.Format("{0:dd/MM/yy}", DateTime.Now) + ")"; break; case "Monthly": mail.Subject = MailList.ReportName + " For Month" + "(" + DateTime.Now.ToString("MMMM yyyy") + ")"; break; default: break; } string msg = ""; mail.To.Add(new MailAddress("*****@*****.**")); foreach (string s in EmailIds) { if (!string.IsNullOrWhiteSpace(s)) { string emailid = s.Trim(); mail.Bcc.Add(emailid); } } msg += "PFA for the " + MailActiveList.MailOn + " wise " + MailList.ReportName; MailBody = MailBody.Replace("{{DateTime}}", DateTime.Now.ToString()); MailBody = MailBody.Replace("{{Content}}", msg); //MailBody = MailBody.Replace("{{UserName}}", MailTempList[0].UserName); mail.Body = MailBody; Attachment MailAttach = null; MemoryStream memStream = new MemoryStream(Attachment); MailAttach = new Attachment(memStream, MailList.ReportName + ".xlsx"); mail.Attachments.Add(MailAttach); mail.IsBodyHtml = true; SmtpClient smtp = new SmtpClient("localhost", 25); smtp.Host = "smtp.gmail.com"; smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.EnableSsl = true; if (From == "live") { try { mail.From = new MailAddress("*****@*****.**"); smtp.Credentials = new System.Net.NetworkCredential ("*****@*****.**", "Spring@2k14"); //smtp.Send(mail); } catch (Exception ex) { if (ex.Message.Contains("quota")) { mail.From = new MailAddress("*****@*****.**"); smtp.Credentials = new System.Net.NetworkCredential ("*****@*****.**", "Ginger@27"); smtp.Send(mail); } else { mail.From = new MailAddress("*****@*****.**"); smtp.Credentials = new System.Net.NetworkCredential ("*****@*****.**", "JohnSGrasiasXcd"); smtp.Send(mail); } } } else if (From == "test") { mail.From = new MailAddress("*****@*****.**"); smtp.Credentials = new System.Net.NetworkCredential ("*****@*****.**", "Spring@2k14"); //this is to send email to test mail only to avoid mis communication to the parent // mail.To.Add("*****@*****.**"); smtp.Send(mail); } EmailLog el = new EmailLog(); el.EmailFrom = mail.From.ToString(); el.EmailTo = mail.To.ToString(); el.EmailCC = mail.CC.ToString(); if (mail.Bcc.ToString().Length < 3990) { el.EmailBCC = mail.Bcc.ToString(); } el.Subject = mail.Subject.ToString(); if (mail.Body.ToString().Length < 3990) { el.Message = msg; } DateTime dt = DateTime.Now; el.EmailDateTime = dt; el.BCC_Count = mail.Bcc.Count; el.Module = "ReportingMail"; el.IsSent = true; el.Message = msg; //log the email to the database MC.CreateOrUpdateEmailLog(el); UpdateMailActivity(MailActiveList); } catch (Exception) {} } } catch (Exception ex) { throw ex; } }