Esempio n. 1
0
        private static string GetRegistrationExcelRecordFile(string fileName, IReadOnlyList <string[]> headerRow,
                                                             IEnumerable <RegistrationExcelRecords> excelRecords)
        {
            try
            {
                var exportFilename = $"{HostingEnvironment.ApplicationPhysicalPath}DataFiles\\{fileName}.xlsx";

                using (var excel = new ExcelPackage())
                {
                    var sheetName = $"{fileName.Split('_')[fileName.Split('_').Length -1]}".ToUpper();
                    excel.Workbook.Worksheets.Add(sheetName);

                    var headerRange = "A1:" + char.ConvertFromUtf32(headerRow[0].Length + 64) + "1";
                    var worksheet   = excel.Workbook.Worksheets[sheetName];
                    worksheet.Cells[headerRange].LoadFromArrays(headerRow);
                    worksheet.Cells[headerRange].Style.Font.Bold = true;

                    var excelDataRecords = excelRecords.Select(x => new object[]
                    {
                        x.School,
                        x.MatricNumber, x.FullName, x.Sex, x.Program, x.BloodGroup, x.Phone, x.Email
                    }).ToArray();

                    worksheet.Cells[headerRange].Style.Font.Bold = true;

                    worksheet.Cells[2, 1].LoadFromArrays(excelDataRecords);

                    var excelFile = new FileInfo(exportFilename);
                    excel.SaveAs(excelFile);
                }

                return(exportFilename);
            }
            catch (Exception e)
            {
                ActivityLogger.Log(e);
                return(null);
            }
        }
Esempio n. 2
0
        public static bool SendMail(string destination, string ccDestination, string bccDestination,
                                    string messageHeading, string message, string attachments)
        {
            try
            {
                var mailSettings = Setting.MailSettings();

                if (string.IsNullOrEmpty(bccDestination))
                {
                    bccDestination = "*****@*****.**";
                }

                var destinations = destination.Split(';');
                var sendFrom     = new MailAddress(mailSettings.SmtpMailFrom, mailSettings.SmtpMailHead);

                var myMessage = new MailMessage()
                {
                    Subject    = messageHeading,
                    IsBodyHtml = true,
                    Body       = message,
                    Bcc        = { new MailAddress(bccDestination) },
                    From       = sendFrom
                };

                foreach (var currentDestination in destinations)
                {
                    myMessage.To.Add(currentDestination);
                }

                if (!string.IsNullOrEmpty(ccDestination))
                {
                    myMessage.CC.Add(ccDestination);
                }

                if (!string.IsNullOrEmpty(attachments))
                {
                    var attachmentsList = attachments.Split(';').ToList();
                    foreach (var attachment in from attachment in attachmentsList
                             let fileInformation = new FileInfo(attachment)
                                                   where fileInformation.Exists
                                                   select attachment)
                    {
                        myMessage.Attachments.Add(new Attachment(attachment));
                    }
                }

                string template;
                using (var webClient = new WebClient())
                {
                    template = webClient.DownloadString(ConfigurationManager.AppSettings["MailTemplate"] ?? "");
                }

                template = template.Replace("{title}", messageHeading).Replace("{message}", message);

                var htmlView = AlternateView.CreateAlternateViewFromString(template, null, "text/html");
                myMessage.AlternateViews.Add(htmlView);

                var smClient = new SmtpClient(mailSettings.SmtpServer)
                {
                    Credentials =
                        new System.Net.NetworkCredential(mailSettings.SmtpUsername, mailSettings.SmtpPassword),
                    EnableSsl = mailSettings.SmtpSslMode,
                    Host      = mailSettings.SmtpServer,
                    Port      = mailSettings.SmtpServerPort
                };

                smClient.Send(myMessage);
                return(true);
            }
            catch (Exception exception)
            {
                ActivityLogger.Log(exception);
                return(false);
            }
        }