Example #1
0
        public ActionResult SendEmail(string id)
        {
            try {
                if (string.IsNullOrEmpty(id))
                {
                    throw new Exception("Email send failed due to scale ID not found.");
                }

                ScaleLibrary scaleLib = new ScaleLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                Scale        scale    = scaleLib.GetByID(id, new string[] { "Party_ID" });

                if (scale == null || scale.Party_ID == null || scale.Ticket_Type != "Receiving Ticket" || scale.Ticket_Status != "Close")
                {
                    throw new Exception("Email send failed.");
                }

                IEnumerable <Contact> contacts = Helpers.ContactHelper.GetEmailContactsByPartyId(scale.Party_ID.ID);
                if (contacts.Count() <= 0)
                {
                    throw new Exception("There is no email contact exists.");
                }

                NotificationDefinition notDef = new NotificationDefinition();
                notDef.ToRecipients = new System.Net.Mail.MailAddressCollection();
                foreach (var item in contacts)
                {
                    notDef.ToRecipients.Add(new System.Net.Mail.MailAddress(item.Email, item.ListText));
                }

                EmployeeHelper employeeHelper = new EmployeeHelper();
                Employee       employee       = employeeHelper.GetEmployeeByUsername(System.Web.HttpContext.Current.User.Identity.Name);
                if (employee == null || string.IsNullOrEmpty(employee.Email) || string.IsNullOrEmpty(employee.Email_Password))
                {
                    throw new Exception("Sender email and password is required.");
                }


                string xslPath     = Path.Combine(ConfigurationHelper.GetsmARTXslPath(), "ScaleEmailBody.xslt");
                string smtpAddress = ConfigurationHelper.GetsmARTSMTPServer();

                Dictionary <string, object> parameter = new Dictionary <string, object>();
                parameter.Add("ScaleID", id);

                SSRSReport ssrsHelper = new SSRSReport("ScaleReceiveTicket.rdl", parameter);
                string     attachment = ssrsHelper.ExportReportToPDF();

                if (string.IsNullOrEmpty(attachment))
                {
                    throw new Exception("There is no attachment found.");
                }

                notDef.Attachments = new List <System.Net.Mail.Attachment>();
                notDef.Attachments.Add(new System.Net.Mail.Attachment(attachment));
                notDef.DeliveryType            = EnumNotificationDeliveryType.Email;
                notDef.FormatType              = EnumFormatType.HTML;
                notDef.Sender                  = new System.Net.Mail.MailAddress(employee.Email, employee.Emp_Name);
                notDef.SMTPServer              = smtpAddress;
                notDef.SMTPServerCredentialID  = employee.Email;
                notDef.SMTPServerCredentialPwd = employee.Email_Password;
                notDef.Subject                 = "Ticket#" + id.ToString();
                NotificationHelper.StartNotificationWF(id, scale.Party_Name, PartyHelper.GetOrganizationName(), employee.Emp_Name, notDef, xslPath, NotificationWFCompleted);

                //return Display(command, container);
                if (_sendMail == true)
                {
                    return(Json(new { Sucess = "Email send sucessfully." }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { Sucess = "Email send failed." }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex) {
                return(Json(new { Sucess = ex.Message }, JsonRequestBehavior.AllowGet));
            }
        }
        public ActionResult SendEmail(string id)
        {
            string      destinationFilePath = string.Empty;
            FilelHelper fileHelper          = new FilelHelper();

            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    throw new Exception("Email send failed due to Container# not found.");
                }

                ContainerLibrary containerLib = new ContainerLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                Container        container    = containerLib.GetByID(id, new string[] { "Booking.Sales_Order_No.Party" });

                if (container == null || container.Booking == null || container.Booking.Sales_Order_No == null || container.Booking.Sales_Order_No.Party == null)
                {
                    throw new Exception("Email send failed.");
                }

                IEnumerable <Contact> contacts = Helpers.ContactHelper.GetEmailContactsByPartyId(container.Booking.Sales_Order_No.Party.ID);
                if (contacts.Count() <= 0)
                {
                    throw new Exception("There is no email contact exists.");
                }

                NotificationDefinition notDef = new NotificationDefinition();
                notDef.ToRecipients = new System.Net.Mail.MailAddressCollection();
                foreach (var item in contacts)
                {
                    notDef.ToRecipients.Add(new System.Net.Mail.MailAddress(item.Email, item.ListText));
                }

                EmployeeHelper employeeHelper = new EmployeeHelper();
                Employee       employee       = employeeHelper.GetEmployeeByUsername(System.Web.HttpContext.Current.User.Identity.Name);
                if (employee == null || string.IsNullOrEmpty(employee.Email) || string.IsNullOrEmpty(employee.Email_Password))
                {
                    throw new Exception("Sender email and password is required.");
                }

                ScaleLibrary scaleLib = new ScaleLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                Scale        scale    = scaleLib.GetScalesByContainerId(container.ID);

                ScaleAttachmentsLibrary        scaleAttachLib   = new ScaleAttachmentsLibrary(ConfigurationHelper.GetsmARTDBContextConnectionString());
                IEnumerable <ScaleAttachments> scaleAttachments = scaleAttachLib.GetAllByParentID(scale.ID);
                if (scaleAttachments == null || scaleAttachments.Count() <= 0)
                {
                    throw new Exception("There is no attachment found.");
                }

                destinationFilePath = Path.Combine(Path.GetTempPath(), container.ID.ToString());
                fileHelper.CreateDirectory(destinationFilePath);
                fileHelper.DeleteFiles(destinationFilePath);
                string imageFilePath = Path.Combine(destinationFilePath, "Images");
                fileHelper.CreateDirectory(imageFilePath);

                string zipFilePath = Path.Combine(destinationFilePath, container.ID.ToString() + ".zip");

                foreach (var item in scaleAttachments)
                {
                    string sourceFilePath    = fileHelper.GetFilePathByFileRefId(item.Document_RefId.ToString());
                    string imageFileFullPath = Path.Combine(imageFilePath, item.Document_Name);
                    System.IO.File.Copy(sourceFilePath, imageFileFullPath);
                }
                new smART.Common.FilelHelper().CreateZip(imageFilePath, zipFilePath);

                if (!fileHelper.FileExits(zipFilePath))
                {
                    throw new Exception("There is no attachment found.");
                }

                string xslPath     = Path.Combine(ConfigurationHelper.GetsmARTXslPath(), "ContainerEmailBody.xslt");
                string smtpAddress = ConfigurationHelper.GetsmARTSMTPServer();

                notDef.Attachments = new List <System.Net.Mail.Attachment>();
                notDef.Attachments.Add(new System.Net.Mail.Attachment(zipFilePath));
                notDef.DeliveryType            = EnumNotificationDeliveryType.Email;
                notDef.FormatType              = EnumFormatType.HTML;
                notDef.Sender                  = new System.Net.Mail.MailAddress(employee.Email, employee.Emp_Name);
                notDef.SMTPServer              = smtpAddress;
                notDef.SMTPServerCredentialID  = employee.Email;
                notDef.SMTPServerCredentialPwd = employee.Email_Password;
                notDef.Subject                 = "Booking#/Container#: " + container.Booking.Booking_Ref_No + "/" + container.Container_No;
                NotificationHelper.StartNotificationWF(id, container.Booking.Sales_Order_No.Party.Party_Name, PartyHelper.GetOrganizationName(), employee.Emp_Name, notDef, xslPath, NotificationWFCompleted, container.Booking.Booking_Ref_No, container.Booking.Sales_Order_No.ID.ToString());
                //return Display(command, container);
                if (_sendMail == true)
                {
                    return(Json(new { Sucess = "Email send sucessfully." }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { Sucess = "Email send failed." }, JsonRequestBehavior.AllowGet));
                }
                //return Json(new { Sucess = "Email send sucessfully." }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                //var script = @"ShowAlertMessage(""Send mail failed"");";
                //return JavaScript(script);
                return(Json(new { Sucess = ex.Message }, JsonRequestBehavior.AllowGet));
            }
            finally
            {
                //fileHelper.DeleteDirectory(destinationFilePath);
            }
        }