示例#1
0
        public static List <T> GetFileDataToList <T>(string filePath, CsvHelper.Configuration.CsvConfiguration configuration)
        {
            List <T> records = new List <T>();

            try
            {
                using (var fileReader = new StreamReader(filePath))
                    using (var csvReader = new CsvHelper.CsvReader(fileReader, configuration))
                    {
                        records = csvReader.GetRecords <T>().ToList();
                        ActivityLogger.Log("INFO", $"{new FileInfo(filePath).Name} has {records.Count}");
                    }
            }
            catch (Exception ex)
            {
                ActivityLogger.Log(ex);
            }

            return(records);
        }
示例#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 = message;
                }

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

                var htmlView = System.Net.Mail.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);
            }
        }