public long CreateOrUpdateEmailLog(EmailLog el)
 {
     try
     {
         MastersBC documenttypeBC = new MastersBC();
         documenttypeBC.CreateOrUpdateEmailLog(el);
         return(el.Id);
     }
     catch (Exception ex)
     {
         throw;
     }
     finally { }
 }
Пример #2
0
        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;
            }
        }